#P8399. [CCC 2022 S5] Good Influencers

[CCC 2022 S5] Good Influencers

Problem Description

Given a tree, node ii has a weight cic_i. Some nodes are blue Y, and some nodes are white N. In each operation, you can choose a blue node and recolor all directly adjacent white nodes to blue. The cost of this operation is the weight of the chosen blue node.

Find the minimum total cost to recolor the entire tree to blue.

It is guaranteed that there is at least one white node and at least one blue node.

Input Format

The first line contains an integer nn, the size of the tree.

The next n1n-1 lines each contain two integers aia_i, bib_i, meaning that aia_i and bib_i are directly connected.

The next line contains nn characters indicating the initial color of each node.

The next line contains nn integers indicating the weight of each node.

Output Format

One line containing the minimum total cost.

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

6
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
6

Hint

For 30%30\% of the testdata: 2n20002 \le n \le 2000, 1ci10001 \le c_i \le 1000, and ai=i,bi=i+1a_i=i, b_i=i+1.

For another 50%50\% of the testdata: 2n20002 \le n \le 2000, 1ci10001 \le c_i \le 1000.

For 100%100\% of the testdata: 2n2×1052 \le n \le 2 \times 10^5, 1ci10001 \le c_i \le 1000.

Translated by ChatGPT 5