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>).
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"
#define sorted(x) sort(x.begin(),x.end())
#define sortedr(x) sort(x.rbegin(),x.rend())
#define sum(x) accumulate(x.begin(),x.end(),0LL)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define maxe(x) *max_element(x.begin(),x.end())
#define mine(x) *min_element(x.begin(),x.end())
#define maxi(x) max_element(x.begin(),x.end())-x.begin()
#define mini(x) min_element(x.begin(),x.end())-x.begin()
#define print(arr) for(auto i:arr)cout<<i<<" ";cout<<endl;
#define mod 1000000007

#define pb emplace_back
#define pr make_pair
#define ff first
#define ss second
struct DSU{
int n;
vector<int>parent;
vector<int>size;
DSU(int n){
    size.assign(n+1,1);
    parent.resize(n+1);
    for(int i=1;i<n+1;i++)parent[i]=i;
}
int find(int x){
    if(parent[x]==x)return x;
    else{
        return find(parent[x]);
    }
}
void merge(int a,int b){
    a=find(a);
    b=find(b);
    if(size[a]<size[b])
        swap(a,b);
    parent[b]=a;
    size[a]+=size[b];
}
};
class Graph
{
public:
    vector<vector<int>> adj;
    // vector<vector<pair<int, int>>> adj;
    vector<bool> visited;
    vector<int> dist;
    int n;
    int ans;
    vector<int> a;
    Graph(int n)
    {
        this->n = n;
        adj.resize(n);
        visited.resize(n, false);
        dist.resize(n, 0);
        ans=0;
    }
    // void addEdge(int u, int v, int w)
    // {
    //	adj[u].pb(pr(v, w));
    //	adj[v].pb(pr(u, w));
    // }
    void addEdge(int u, int v)
    {
        adj[u].pb(v);
        adj[v].pb(u);
    }
    void bfs(int s)
    {
        queue<int> q;
        q.push(s);
        visited[s] = true;
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            for (auto v : adj[u])
            {
                if (!visited[v])
                {
                    visited[v] = true;
                    q.push(v);
                }
            }
        }

    }
    int dfs(int s)
    {
        visited[s] = true;
        int op=0;
        for (auto v : adj[s])
        {
            if (!visited[v])
            {
                op+=dfs(v);
            }
        }
        ans+=abs(a[s]+op);
        // cout<<s<<" "<<-(a[s]+op)<<endl;
        return -(a[s]+op);
    }
};
void solve()
{
    int n,m=0;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        g.addEdge(u-1,v-1);
    }
    vector<int>a(n);
    for (int i = 0; i < n; i++)
    {
        cin>>a[i];
    }
    g.a=a;
    g.dfs(0);
    cout<<g.ans<<endl;
    
}
signed main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
    return 0;
}