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*<sub class="lower-index">*i*</sub>. In one move you can apply the following operation:
 1.  Select the subtree of the given tree that includes the vertex with number 1. 1.  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 *n* (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>). Each of the next *n* - 1 lines contains two integers *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub> ≤ *n*; *a*<sub class="lower-index">*i*</sub> ≠ *b*<sub class="lower-index">*i*</sub>) indicating there's an edge between vertices *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub>. 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*<sub class="lower-index">1</sub>, *v*<sub class="lower-index">2</sub>, ..., *v*<sub class="lower-index">*n*</sub> (|*v*<sub class="lower-index">*i*</sub>| ≤ 10<sup class="upper-index">9</sup>).
from sys import stdin, stdout,setrecursionlimit
from collections import defaultdict,deque,Counter,OrderedDict
from heapq import heappop,heappush
import threading

n = int(stdin.readline())

graph = [set() for x in range(n)]

for x in range(n-1):
    a,b = [int(x) for x in stdin.readline().split()]
    a -= 1
    b -= 1

    graph[a].add(b)
    graph[b].add(a)

vals = [int(x) for x in stdin.readline().split()]

bruh = [(0,-1)]

for x in range(n):
    num,p = bruh[x]
    for y in graph[num]:
        if y != p:
            bruh.append((y,num))

result = [-1 for x in range(n)]

for v,parent in bruh[::-1]:
    nP = 0
    nN = 0
    for x in graph[v]:
        if x != parent:
            p,n = result[x]
            nP = max(nP,p)
            nN = max(nN, n)
    nN = max(nN, nP+vals[v])
    nP = max(nP, nN-vals[v])
            
    result[v] = (nP,nN)

ng, ps = result[0]

vals[0] += ps - ng

stdout.write(str(ng+ps))
