시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB199947.368%

문제

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.

입력

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 the minimum integer number of dollars required to have all of the students to intend to write the CCC.

서브태스크

번호배점제한
15

2 ≤ N ≤ 2 000, 1 ≤ Ci ≤ 1 000, Ai = i and Bi = i + 1 for each i

27

2 ≤ N ≤ 2 000, 1 ≤ Ci ≤ 1 000

33

2 ≤ N ≤ 200 000, 1 ≤ Ci ≤ 1 000

예제 입력 1

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

예제 출력 1

6

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.

예제 입력 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

예제 출력 2

6

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.

채점 및 기타 정보

  • 예제는 채점하지 않는다.