#P8916. [DMOI-R2] 暗号

[DMOI-R2] 暗号

Background

Too many people and too many things roar between us.
Too much noise and the signal is weak, even the wind can interfere.
But you do not want to keep walking in a dark underground passage.
You want to feel the wind and be free, to hold hands and go see the sea, and wander around the world.
—— "Cipher"

Continuing from the previous story, every army has received supplies. However, to go to the battlefield, the preparation is still not sufficient. Only if JF’s armies form a group army can they produce strong fighting power, and only then can they win in the warlord chaos.

Problem Description

It is known that JF has nn armies, which are connected by n1n-1 edges. Army 11 is the root. Each army has its own black-or-white "cipher" for communication, as well as its combat power value and morale value. Initially, an army’s morale value equals its combat power value.

We start changing morale values from the deepest army (largest depth). For the current army uu, among the armies directly connected to uu that have a greater depth than uu, if there exists an army vv whose cipher is the same as uu’s, then uu can contact vv. Then uu’s morale value must add in full the sum of combat power values of the nodes in the subtree of vv whose cipher color is the same as vv’s. (You can understand this as: when the morale update of uu is finished, the morale of uu’s subtree has also been fully updated.)

Now, you may modify the ciphers of these armies arbitrarily. You need to find the maximum possible value of the sum of morale values of all armies.

Input Format

The first line contains an integer nn, as described.

Lines 22 to nn each contain two integers, representing an edge between uu and vv.

Line n+1n+1 contains nn integers. The ii-th integer represents the initial combat power value wiw_i of the ii-th army.

Output Format

Output one integer in one line, representing the maximum sum of morale values.

4
1 2 
1 3 
2 4 
6 -2 4 -7 
5
10
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
4 10
-3 9 4 -3 -2 5 -1 -3 -9 7
42
20
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
4 10
6 11
6 12
7 13
7 14
7 15
7 16
8 17
9 18
11 19
15 20
1 2 -1 -4 9 -3 -5 8 9 -10 -13 15 11 -6 17 -1 -19 20 -5 -9
266
6
1 2
1 3
1 4
2 5
2 6
-1 5 -3 -4 -5 -7
-10

Hint

Sample Explanation #1.

We change the ciphers of armies 1,3,41,3,4 to black, and change the cipher of army 22 to white. Then the final morale values of armies 1,2,3,41,2,3,4 become 10,2,4,710,-2,4,-7, and the total sum is 55. It can be proven that there is no plan that yields a larger final sum of morale values.

Sample Explanation #2.

We use 11 to represent the black cipher and 22 to represent the white cipher. Then the cipher colors of the nn armies are as follows: 1 1 1 1 2 1 2 2 2 1. The sum of morale values of the entire army is 4242. It can be proven that there is no plan that yields a larger sum of morale values.

Constraints and Notes.

Test Point ID nn \le Special Condition
121\sim2 2020 None
363 \sim 6 5050
7107 \sim 10 300300 v=u+1v=u+1
111211\sim12 1wi10001 \le w_i \le 1000
131413\sim14 u=1u=1
152015 \sim 20 None

For 100%100\% of the testdata, 1n3001 \le n \le 300, 1000wi1000-1000 \le w_i \le 1000.

Translated by ChatGPT 5