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 collections import defaultdict


def _n():
    return int(input())


def _nA():
    return list(map(int, input().split()))


def _sA():
    return input().split()

# https://codeforces.com/problemset/problem/274/B

# 输入 n(≤1e5)，表示一棵有 n 个节点的树。
# 然后输入 n-1 条边：这条边所连接的两点的编号（从 1 开始）。
# 最后输入 n 个数，表示每个节点的值 a[i](-1e9≤a[i]≤1e9)。

# 每次操作，你可以选择一个包含节点 1 的连通块，把所有点的值都 +1 或都 -1。
# 输出把树上所有节点的值都变为 0 的最少操作次数。

# 输入
# 3
# 1 2
# 1 3
# 1 -1 1
# 输出
# 3

def main():
    n = _n()
    edges = [_nA() for _ in range(n-1)]
    a=_nA()
    g=defaultdict(list)
    g[0]=[-1]
    for x,y in edges:
        g[x-1].append(y-1)
        g[y-1].append(x-1)

    sys.setrecursionlimit(5 * 10 ** 4)

    def f(i,fa):
        cur=a[i]
        if len(g[i])==1:
            return min(cur,0),max(cur,0)

        mn, mp = 0, 0

        for nei in g[i]:
            if nei==fa:
                continue
            n,p=f(nei,i)
            mn = min(mn, n)
            mp = max(mp, p)
        # -mn 表示 需要 + abs(mn) 个 1
        cur-=mn
        # -mp 表示 需要 - mp 个 1
        cur-=mp


        if cur<0:
            # 当前值仍然需要跟 根节点 一起执行 abs(mn+cur) 次 +1 操作
            mn+=cur
        else:
            # 当前值仍然需要跟 根节点 一起执行 mp+cur 次 -1 操作
            mp+=cur
        # 将当前节点所需要的操作数向上传递
        return mn,mp
    mn,mp=f(0,-1)
    print(mp-mn)

sys.setrecursionlimit(0x0FFFFFFF)
import threading

threading.stack_size(0x04000000)
threading.Thread(target=main).start()
