CCCGuide

Problem S5: Good Influencers

2022 · S5 · Original problem · DMOJ

Problem Description

There are NN (N≥2N \ge 2) students in a computer science class, with distinct student IDs ranging from 11 to NN. There are N−1N-1 friendships amongst the students, with the iith being between students AiA_i and BiB_i (Ai≠BiA_i \not= B_i, 1≤Ai≤N1 \le A_i \le N and 1≤Bi≤N1 \le B_i \le N). Each pair of students in the class are either friends or socially connected. A pair of students aa and bb are socially connected if there exists a set of students m1,m2,…,mkm_1, m_2, \ldots, m_k such that

Initially, each student ii either intends to write the CCC (if PiP_i is Y) or does not intend to write the CCC (if PiP_i 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 ii who intends to write the CCC, pay them CiC_i 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 Specification

The first line contains the integer NN.

The next N−1N-1 lines each contain two space-separated integers, AiA_i and BiB_i (1≤i≤N−11 \leq i \leq N-1).

The next line contains NN characters, P1…PNP_{1}\ldots P_{N}, each of which is either Y or N.

The next line contains NN space-separated integers, C1…CNC_{1}\ldots C_{N}.

The following table shows how the available 15 marks are distributed.

Marks AwardedNumber of studentsPaymentAdditional Constraints
5 marks2≤N≤2 0002 \le N \leq 2\ 0001≤Ci≤1 0001 \le C_i \le 1\ 000Ai=iA_i = i and Bi=i+1B_i = i+1 for each ii
7 marks2≤N≤2 0002 \le N \leq 2\ 0001≤Ci≤1 0001 \le C_i \le 1\ 000None
3 marks2≤N≤200 0002 \le N \leq 200\ 0001≤Ci≤1 0001 \le C_i \le 1\ 000None

Output Specification

Output the minimum integer number of dollars required to have all of the students to intend to write the CCC.

Sample Input 1

4 
1 2 
2 3 
3 4 
YNYN 
4 3 6 2

Output for Sample Input 1

6

Explanation of Output for Sample Input 1

The CCC should pay $6 to student 3 to deliver a seminar to their friends (students 2 and 4), after which all 4 students will intend to write the CCC.

Sample Input 2

15 
1 5 
5 2 
2 15 
15 4 
2 10 
8 3 
3 1 
1 6 
11 6 
12 6 
11 9 
11 14 
12 7 
13 7 
NNYYYNYYNNNNNNN 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Output for Sample Input 2

6

Explanation of Output for Sample Input 2

One optimal strategy is for the CCC to ask students 5, 1, 6, 11, 7, and 2 to deliver seminars, in that order, paying them $1 each.