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 ll long long int
#define ull unsigned long long int
#define Max 1000005
#define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ALL(x) x.begin(),x.end()
#define pi acos(-1.0)
#define pb push_back
#define load(array,size)  for(ll i=0 ; i<size ; i++) cin>>array[i] ;
#define endl '\n'

#define sc(n) scanf("%d",&n)
#define scan(n) scanf("%lld",&n)
/*        speed up       */
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define sci(n) scanf("%d",&n);
#define scl(n) scanf("%lld",&n);
#define load1(array,size)  for(ll i=1 ; i<=size ; i++) cin>>array[i] ;
#define INF    1LL<<19
#define pll pair<ll,ll>
#define mst(arr,value) memset(arr,value,sizeof(arr));
#define tst() _fastio;int testsalam;cin >> testsalam;while(testsalam--)
#define Mod 100000007
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;

//void inversemod(ll n, ll mod, ll invmod[],ll MOD){for(int i=2; i<=n; ++i) invmod[i]=MOD-MOD/i*invmod[MOD%i]%MOD;}
//void bigmod( ll a, ll b ,ll mod){ll r = 1;a = a%mod;while(b){if( b%2 ) r = (r*a)%mod;b = b/2;a = (a*a)%mod; }return (ll)r;}
string inttostr(ll n){std::stringstream ss;ss << n;string out=ss.str();return out;}
ll stringtoint(string s){istringstream iss (s);ll n;iss >> n;return n;}
template <typename T> T gcd(T a,T b){if(a < 0)return gcd(-a,b);if(b < 0)return gcd(a,-b);return (b == 0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a,T b) {if(a < 0)return lcm(-a,b);if(b < 0)return lcm(a,-b);return a*(b/gcd(a,b));}
typedef vector<ll>vec;
typedef pair<ll,ll> pii;
typedef map<ll,ll>mpll;
typedef set<ll>seet;
typedef tuple<ll,ll,ll>tpl;

int arr[Max+10];
int decrese[Max],increase[Max];
vector<int>inp[Max];
int vis[Max];
void dfs(int src, int pre)
{
    vis[src]=1;
	for(int x:inp[src])
	{
		if(vis[x])continue;
		dfs(x,src);
		decrese[src]=max(decrese[src],decrese[x]);
		increase[src]=max(increase[src],increase[x]);
	
	}
	int val= arr[src] + increase[src] - decrese[src];
	if(val>=0)
		decrese[src]+=val;
	else increase[src]-=val;
    // cout<<"now src "<<src<<endl;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n-1; ++i)
    {
    	int u,v;
    	cin >> u >> v;
    	inp[u].push_back(v);inp[v].push_back(u);
    }
    for(int i=1;i<=n;i++)cin >> arr[i];
    dfs(1,0);
    cout<<increase[1]+decrese[1]<<endl;
}
/*
     remember:
     ll vs int
     array size
     case for 1 2 3 
*/


	  	 	 		 		  	 		    					 	