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>).
# stub from https://codeforces.com/blog/entry/71884
import sys

from functools import reduce

input = sys.stdin.readline

############ ---- Input Functions ---- ############
def inp():
    return int(input())


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


def insr():
    s = input()
    return list(s[: len(s) - 1])


def invr():
    return map(int, input().split())


class Node:
    def __init__(self, val):
        self.val = val
        self.children = []
        self.parent = None
        self.acc = 0


############ ---- Solution ---- ############
def solve():
    lines = inp()
    flat = [Node(0)]  # qute offset
    for l in range(lines):
        flat.append(Node(0))

    for l in range(lines - 1):
        parent, child = inlt()
        flat[parent].children.append(flat[child])
        flat[child].parent = flat[parent]
    vals = inlt()
    for i, val in enumerate(vals, 1):
        flat[i].val = val

    king = flat[1]
    while king.parent != None:
        king = king.parent

    greed_sum = abs(king.val)
    target = king.val
    layer = [king]
    while layer:
        new_layer = []
        for parent in layer:
            for child in parent.children:
                t_val = parent.acc + child.val
                new_acc = t_val - target
                greed_sum += abs(new_acc)
                child.acc = new_acc
                new_layer.append(child)
        layer = new_layer
    print(greed_sum)


solve()
