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>).
"""
usefull snippets:
    - map(int, input().split())
    - map(int, sys.stdin.readline().split()))
    - int(input())
    - int(sys.stdin.readline().strip())

    - sys.stdout.write()
    - sys.stdout.write(" ".join(map(str, c) # writes c - collection of ints
"""
# import collections
import sys
from bisect import bisect_left


# recursion increase
# sys.setrecursionlimit(10000)

# number with big precision
# from decimal
# getcontext().prec = 10_000


# input parser
# n, d, li, ld, ls, s - int, float, list of ints, list of floats, list of string, string
# lists are lines with ' '(space) delimiters
def get_from_input(format: str, wrap_lists=True):
    result = []
    selected_types = format.split()
    for type in selected_types:
        input = sys.stdin.readline().strip()
        if type == 'n':
            result.append(int(input))
        elif type == 'd':
            result.append(float(input))
        elif type == 's':
            result.append(input)
        elif type == 'li':
            mapped = map(int, input.split())
            result.append(list(mapped) if wrap_lists else mapped)
        elif type == 'ld':
            mapped = map(float, input.split())
            result.append(list(mapped) if wrap_lists else mapped)
        elif type == 'ls':
            result.append(list(input.split()))

    if len(result) == 1:
        return result[0]
    return result


# splits aaasfffsga into (aaa, s, fff, s, g, a) blocks with signature (startPos, length, type)
def split_into_blocks(s):
    blocks = []

    i = 0
    curr_type = s[0]
    curr_len = 0
    while i < len(s):
        if s[i] == curr_type:
            curr_len += 1
            i += 1
        else:
            blocks.append((i - curr_len, curr_len, curr_type))
            curr_type = s[i]
            curr_len = 0

    blocks.append((i - curr_len, curr_len, curr_type))

    return blocks


# longest common prefix
def get_lcp(s, suffix_array):
    s = s + "$"
    n = len(s)
    lcp = [0] * (n)
    pos = [0] * (n)
    for i in range(n - 1):
        pos[suffix_array[i]] = i
    k = 0
    for i in range(n - 1):
        if k > 0:
            k -= 1
        if pos[i] == n - 1:
            lcp[n - 1] = -1
            k = 0
            continue
        else:
            j = suffix_array[pos[i] + 1]
            while max([i + k, j + k]) < n and s[i + k] == s[j + k]:
                k += 1
            lcp[pos[i]] = k

    return lcp


def get_suffix_array(word):
    suffix_array = [("", len(word))]

    for position in range(len(word)):
        sliced = word[len(word) - position - 1:]
        suffix_array.append((sliced, len(word) - position - 1))

    suffix_array.sort(key=lambda x: x[0])
    return [item[1] for item in suffix_array]


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def bin_search(collection, element):
    i = bisect_left(collection, element)
    if i != len(collection) and collection[i] == element:
        return i
    else:
        return -1


def dfs(v, g, last, mh, md, c, visited):
    visited.add(v)
    for e in g[v]:
        if e not in visited:
            dfs(e, g, v, mh, md, c, visited)

    curr = c[v] + mh[v] - md[v]

    if curr >= 0:
        md[v] = md[v] + curr
    else:
        mh[v] = mh[v] - curr

    if last != -1:
        md[last] = max(md[v], md[last])
        mh[last] = max(mh[v], mh[last])


def main():
    n = int(input())
    g = dict()
    md = [0]*n
    mh = [0]*n
    for i in range(n - 1):
        a, b = get_ints()
        a -= 1
        b -= 1
        if a not in g.keys():
            g[a] = list()
        if b not in g.keys():
            g[b] = list()

        g[a].append(b)
        g[b].append(a)

    c = get_from_input('li')
    if n == 1:
        print(abs(c[0]))
        exit()

    visited = set()

    dfs(0, g, -1, mh, md, c, visited)
    print(md[0] + mh[0])


if __name__ == "__main__":
    main()
