跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F2546. Zero Tree

    ID: 2552 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>搜索动态规划贪心算法思想数据结构进阶难度共享题库Codeforces英文题面题目来源题面语言

Zero Tree

题目描述

B. Zero Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 vi. 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≤105). Each of the next n-1 lines contains two integers ai and bi (1≤ai,bin;aibi) indicating there's an edge between vertices ai and bi. It's guaranteed that the input graph is a tree.

The last line of the input contains a list of n space-separated integers v1,v2,...,vn (|vi|≤109).

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 C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
Input
3
1 2
1 3
1 -1 1
Output
3

题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 2192 题。保留原作者与原赛事署名。