#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 armies, which are connected by edges. Army 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 , among the armies directly connected to that have a greater depth than , if there exists an army whose cipher is the same as ’s, then can contact . Then ’s morale value must add in full the sum of combat power values of the nodes in the subtree of whose cipher color is the same as ’s. (You can understand this as: when the morale update of is finished, the morale of ’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 , as described.
Lines to each contain two integers, representing an edge between and .
Line contains integers. The -th integer represents the initial combat power value of the -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 to black, and change the cipher of army to white. Then the final morale values of armies become , and the total sum is . It can be proven that there is no plan that yields a larger final sum of morale values.
Sample Explanation #2.
We use to represent the black cipher and to represent the white cipher. Then the cipher colors of the armies are as follows: 1 1 1 1 2 1 2 2 2 1. The sum of morale values of the entire army is . It can be proven that there is no plan that yields a larger sum of morale values.
Constraints and Notes.
| Test Point ID | Special Condition | |
|---|---|---|
| None | ||
| None |
For of the testdata, , .
Translated by ChatGPT 5