CF274B.Zero Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A tree is a graph with nn vertices and exactly n1n-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 TT is a tree with both vertices and edges as subsets of vertices and edges of TT .

You're given a tree with nn vertices. Consider its vertices numbered with integers from 1 to nn . Additionally an integer is written on every vertex of this tree. Initially the integer written on the ii -th vertex is equal to viv_{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.

输入格式

The first line of the input contains nn ( 1<=n<=1051<=n<=10^{5} ). Each of the next n1n-1 lines contains two integers aia_{i} and bib_{i} ( 1<=ai,bi<=n; aibi1<=a_{i},b_{i}<=n; a_{i}≠b_{i} ) indicating there's an edge between vertices aia_{i} and bib_{i} . It's guaranteed that the input graph is a tree.

The last line of the input contains a list of nn space-separated integers v1,v2,...,vnv_{1},v_{2},...,v_{n} ( vi<=109|v_{i}|<=10^{9} ).

输出格式

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.

输入输出样例

  • 输入#1

    3
    1 2
    1 3
    1 -1 1
    

    输出#1

    3
    
首页