Good Influencers
InterviewTime limit1sMemory limit1024 MB
Given a tree where each node is initially willing or not and has a cost, repeatedly pay a willing node to convert all its neighbors, and find the minimum total cost so that everyone ends up willing.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
There are N (N ≥ 2) students in a computer science class, with distinct student IDs ranging from 1 to N. There are N − 1 friendships amongst the students, with the ith being between students Ai and Bi (Ai ≠ Bi, 1 ≤ Ai ≤ N and 1 ≤ Bi ≤ N). Each pair of students in the class are either friends or socially connected. A pair of students a and b are socially connected if there exists a set of students m1, m2, . . . , mk such that
- a and m1 are friends,
- mi and mi+1 are friends (for 1 ≤ i ≤ k − 1), and
- mk and b are friends.
Initially, each student i either intends to write the CCC (if Pi is Y) or does not intend to write the CCC (if Pi is N). Initially, at least one student intends to write the CCC, and at least one student does not intend to write the CCC.
The CCC has allocated some funds to pay some students to be influencers for the CCC. The CCC will repeatedly choose one student i who intends to write the CCC, pay them Ci dollars, and ask them to deliver a seminar to all of their friends, and then all of their friends will intend to write the CCC.
Help the CCC determine the minimum cost required to have all of the students intend to write the CCC.
Input
The first line contains the integer N.
The next N − 1 lines each contain two space-separated integers, Ai and Bi (1 ≤ i ≤ N − 1).
The next line contains N characters, P1 . . . PN, each of which is either Y or N.
The next line contains N space-separated integers, C1 . . . CN.
Output
Output the minimum integer number of dollars required to have all of the students to intend to write the CCC.