Xor Tree

Time limit: 0.4s Memory limit: 256MB Input: Output:

You are given a tree with NN vertices labeled from 11 to NN rooted at 11. The root is not considered a leaf even if it has just one descendant. Each vertex has an associated cost and a state, the state may be active or inactive. Initially all nodes are considered active.

In one operation you may pick a leaf and switch the state from active to inactive or inactive to active for all vertices along the path from the leaf to the root. Print the maximum achievable sum of active vertices if you are allowed to make this operation as many times as you wish on any leaf.

Input

The first line will contain the integer NN (2N200 0002 \leq N \leq 200\ 000). The following N1N - 1 lines will contain two values aa and bb, and each pair (aa and bb) represents an edge in the tree. The last line will contain NN integers representing the cost of each vertex starting with 11 (109vali109-10^9 \leq val_i \leq 10^9).

Output

Only one integer will be printed, the maximum achievable sum.

Notes

For tests worth 10 points, 2N202 \leq N \leq 20.
For tests worth 10 extra points, 2N1 0002 \leq N \leq 1\ 000.

Example

stdin

5
1 2
1 3
2 4
2 5
1 2 4 -4 -5

stdout

7

Explanation

In the sample we may pick vertex 44 to apply the operation first, so the vertices on the path 4214 \rightarrow 2 \rightarrow 1 will be flipped from activated to deactivated.

We must also pick vertex 55 to flip 5215 \rightarrow 2 \rightarrow 1, so in the end 22 and 11 will keep being activated but 44 and 55 are deactivated. Lastly only vertices 11, 22 and 33 are active so their sum is 77.

Log in or sign up to be able to send submissions!