Codeforces Round 168 (Div. 2)
D
Zero Tree
Codeforces
未评级
原题
← CE →
翻译中…

A tree is a graph with $*n*$ vertices and exactly $*n* - 1$ edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.

翻译中…

A subtree of a tree $*T*$ is a tree with both vertices and edges as subsets of vertices and edges of $*T*$.

翻译中…

You're given a tree with $*n*$ vertices. Consider its vertices numbered with integers from 1 to $*n*$. Additionally an integer is written on every vertex of this tree. Initially the integer written on the $*i*$-th vertex is equal to $*v*_{*i*}$. In one move you can apply the following operation:

翻译中…
  1. Select the subtree of the given tree that includes the vertex with number 1.
  2. Increase (or decrease) by one all the integers which are written on the vertices of that subtree.
翻译中…

Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero.

翻译中…

Input

翻译中…

The first line of the input contains $*n*$ ($1 ≤ *n* ≤ 10^{5}$). Each of the next $*n* - 1$ lines contains two integers $*a*_{*i*}$ and $*b*_{*i*}$ ($1 ≤ *a*_{*i*}, *b*_{*i*} ≤ *n*; *a*_{*i*} ≠ *b*_{*i*}$) indicating there's an edge between vertices $*a*_{*i*}$ and $*b*_{*i*}$. It's guaranteed that the input graph is a tree.

翻译中…

The last line of the input contains a list of $*n*$ space-separated integers $*v*_{1}, *v*_{2}, ..., *v*_{*n*}$ ($|*v*_{*i*}| ≤ 10^{9}$).

翻译中…

Output

翻译中…

Print the minimum number of operations needed to solve the task.

翻译中…

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

翻译中…

样例

Input #1
3
1 2
1 3
1 -1 1
Output #1
3
InputCopy OutputCopy
题目信息
题号
275D
时间限制
2 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。