#include<bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define enddl '\n'
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define full(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define PI 3.14159265

const int MAXN = 1e5+5;

const int MOD = 1e9+7;

using namespace std;

int n;
vector<int> g[MAXN];
ll val[MAXN];
ll incr[MAXN];
ll decr[MAXN];

void dfs(int root, int par) {
	bool leaf = true;
	for(auto next : g[root]) {
		if(next != par) {
			leaf = false;
			dfs(next, root);
			incr[root] = max(incr[root], incr[next]);
			decr[root] = max(decr[root], decr[next]);
		}
	}
	ll value = val[root] - decr[root] + incr[root];
	if(value <= 0) {
		incr[root]-=value;
	}
	else {
		decr[root]+=value;
	}
}

int main() {
	cin >> n;
	for(int i=1;i<=n-1;i++) {
		int x,y;
		cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i=1;i<=n;i++) {
		cin >> val[i];
	}
	if(n == 1) {
		cout << abs(val[1]) << endl;
		return 0;
	}
	dfs(1, -1);
	cout << incr[1] + decr[1] << endl;

}


#include <bits/stdc++.h>
#define ll long long int
#define printcont(v) for(auto i : v) cout << i << " "; cout << endl;
using namespace std;
ll findmax(ll a, ll b)
{
	if(a>b) return a;
	return b;
}
class Graph
{
	map<ll,vector<ll>>adjList ;
    public :
    Graph()
    {

    }
    void addEdge(ll source, ll destination, bool bidr = true)
    {
        adjList[source].push_back(destination);
        if(bidr)
            adjList[destination].push_back(source);
    }

    void printAdjList()
    {

        for(auto i: adjList)
        {
            cout << i.first << "->";
            for(auto entry: i.second)
                cout << entry << ", ";
            cout << endl;
        }
    }
    void DFS(ll node, vector<bool> &visited, vector<ll> &val, vector<pair<ll,ll>> &addsub)
    {
        visited[node] = true;
        for(auto &neighbour : adjList[node])
            if(!visited[neighbour])
            {
                DFS(neighbour,visited,val,addsub);
                addsub[node].first = findmax(addsub[node].first,addsub[neighbour].first);
                addsub[node].second = findmax(addsub[node].second,addsub[neighbour].second);
            }
        ll valuehere = val[node] + addsub[node].first - addsub[node].second;
        if(valuehere < 0)
        	addsub[node].first += -valuehere;
        else
        	addsub[node].second += valuehere;
    }

};

int main()
{
    Graph g;
    ll n;
    cin >> n;
    for(ll i = 0; i < n-1; i++)
    {
    	ll u, v;
    	cin >> u >> v;
    	g.addEdge(u,v);
    }
   	vector<ll> val(n+1);
   	vector<pair<ll,ll> > addsub(n+1,{0,0});
   	vector<bool> visited(n+1,false);
   	for(ll i = 1; i <= n; i++)
   		cin >> val[i];
    g.DFS(1,visited,val,addsub);
    cout << addsub[1].first + addsub[1].second;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

#define ADD first
#define SUB second
typedef long long ll;

ll val[100001]; vector<int> edges[100001];

pair<ll, ll> dfs(int cur, int par) {
	pair<ll, ll> tmp, ans = {0, 0};

	for (int child : edges[cur]) {
		if (child != par) {
			tmp = dfs(child, cur);
			ans.ADD = max(ans.ADD, tmp.ADD);
			ans.SUB = max(ans.SUB, tmp.SUB);
		}
	}

	if ((val[cur] += ans.ADD - ans.SUB) > 0)
		ans.SUB += val[cur];
	else
		ans.ADD -= val[cur];

	return ans;
}

int main() {
	int n, u, v, i;
	scanf("%d", &n);

	for (i = 2; i <= n; ++i)
		scanf("%d %d", &u, &v),
		edges[u].push_back(v),
		edges[v].push_back(u);

	for (i = 1; i <= n; ++i)
		scanf("%lld", val + i);

	auto ans = dfs(1, 0);

	printf("%lld\n", ans.ADD + ans.SUB);
	return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

#define pb push_back
#define ll long long
const int MX = 1e5 + 5;
int n, v[MX];
vector<int> adj[MX];
ll res = 0;

pair<ll, ll> solve (int u, int fa) {
    ll add = 0, sub = 0;
    for (int i=0; i<adj[u].size(); i++) {
        int v = adj[u][i];
        if (v != fa) {
            pair<ll, ll> cur = solve (v, u);
            add = max(add, cur.first);
            sub = max(sub, cur.second);
        }
    }
    v[u] -= add - sub;
    if (v[u] > 0) add += v[u];
    else sub += v[u] * -1;
    return make_pair(add, sub);
}

int main () {
    ios_base::sync_with_stdio(false);
    cin>>n;
    for (int i=0; i<n-1; i++) {
        int u, v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for (int i=1; i<=n; i++) cin>>v[i];
    pair<ll, ll> res = solve(1, -1);
    cout<<res.first + res.second<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+7;
vector < int > graph[N];
long long x[N], add[N], sub[N];
int n;

void dfs(int u, int p) {
        for(int v : graph[u]) {
                if(v != p) {
                        dfs(v, u);
                        sub[u] = max(sub[u],sub[v]);
                        add[u] = max(add[u],add[v]);
                }
        }

        long long curr = x[u] + add[u] - sub[u];
        if(curr > 0) sub[u] += curr;
        else add[u] += -curr;
}

int main() {
        cin >> n;
        for(int i = 1 ; i < n ; i++) {
                        int x, y;
                        cin >> x >> y;
                        graph[x].push_back(y);
                        graph[y].push_back(x);
        }

        for(int i = 1 ; i <= n ; i++) cin >> x[i];
        dfs(1, -1);
        cout <<  add[1] + sub[1] << endl;
        return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

vector<vector<int>> g;
vector<long long> val, pls, mins;
void dfs(int u, int p) {
    long long pl = 0, mi = 0;
    for (int v: g[u]) if (v != p) {
        dfs(v, u);
        chmax(pl, pls[v]);
        chmax(mi, mins[v]);
    }
    long long cur = val[u] + pl - mi;
    pls[u] = pl - cur * (cur < 0);
    mins[u] = mi + cur * (cur > 0);
}

int main() {
    int n; cin >> n;
    g.resize(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b; a--, b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    val.resize(n);
    for (auto &vi: val) cin >> vi;
    pls.resize(n), mins.resize(n);
    dfs(0, -1);
    cout << pls[0] + mins[0] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a,int l, int md){a%=md; int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
int invmod(int a, int md){return powmod(a,md-2,md);}
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector< ii > vii;
#define pb push_back
int __set(int b, int i) {return b|(1LL<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1LL<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return (((a%md)*(b%md))%md+md)%md;}
int addmod(int a, int b, int md) {return ((a%md+b%md)%md+md)%md;}
int submod(int a, int b, int md) {return (((a%md-b%md)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<int, vector<int>, greater<int> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
vector<vi> g(100010);
int vv[100010], sub[100010][2], seen[100010];

void dfs(int u) {
	seen[u]=1;
	int plus=0,minus=0;
	for(int v: g[u]) {
		if(!seen[v]) {
			dfs(v);
			plus=max(plus,sub[v][0]);
			minus=max(minus,sub[v][1]);
		}
	}
	int val=vv[u]+plus-minus;
	if(val<0) plus+=-val;
	else minus+=val;
	sub[u][0]=plus;
	sub[u][1]=minus;
}
signed main(void)
{
	IOS;
	cin>>n;
	for(int i = 0; i < n-1; i++) {
		int u,v; cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
	for(int i = 0; i < n; i++)
		cin>>vv[i];
	dfs(0);
	cout<<sub[0][0]+sub[0][1]<<endl;
	return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <valarray>
#include <utility>
#include <numeric>
#include <memory>
#include <functional>
#include <stdexcept>
#include <typeinfo>
#include <limits>
#include <complex>
#include <locale>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define setbit(n, b) ((n)|((n)>>1<<(b)))
#define removebit(n, b) ((n)&~((n)>>1<<(b)))
#define flipbit(n, b) ((n)^((n)>>1<<(b)))
#define checkbit(n,b) (((n)>>(b))&1)
#define rep(i,n) for (int i=0;i<(n);i++)
#define Rep(i,a,b) for (int i=(a);i<=(b);i++)
#define repit(it,a) for (__typeof(a.begin()) it=a.begin();it!=a.end();++it)
#define pb(...) push_back(__VA_ARGS__)
#define pf(...) push_front(__VA_ARGS__)
#define sz(a) (int)a.size()
#define X first
#define Y second
#define eprintf(...) __builtin_fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define all(a) (a).begin(),(a).end()
#define reunique(v) v.erase(unique(all(v)),v.end())
#define eaq(n,x) __typeof(x) n=x;
#define RET(p,x) return p<<x<<endl,0;
#define Debug(x) cerr<<#x<<" = "<<x<<endl
#define Debug2(x,y) cerr<<#x<<" = "<<x<<" , "<<#y<<" = "<<y<<endl
#define debugarr2(a,n,m,x) cerr<<"-----------------------LET'S DEBUGGING OF "<<#a<<"!-------------------------\n";\
rep(i,n){cerr<<"LINE("<<setw(3)<<i<<") "<<": ";rep(j,m){cerr<<setw(x)<<a[i][j]<<" ";}cerr<<endl;}\
cerr<<"-----------------------FINISH DEBUGGING OF "<<#a<<"!-------------------------\n";
#define debug_arr(a,n,x) cerr<<"------------------------LET'S DEBUGGING OF "<<#a<<"!--------------------------\n";\
cerr<<#a<<" : ";rep(i,n)cerr<<setw(x)<<a[i][j]<<" ";cerr<<endl;\
cerr<<"------------------------FINISH DEBUGGING OF "<<#a<<"!--------------------------\n";
vector<int>vt[100001];
long long a[1000001];
long long dfh1[1000001],dfh2[1000001];
long long s=0;
int n;
int dfs(int k,int p)
{
	for(int i=0;i<vt[k].size();i++)
	{
		if(vt[k][i]==p)continue;
		dfs(vt[k][i],k);
		dfh1[k]=min(dfh1[k],dfh1[vt[k][i]]);
		dfh2[k]=max(dfh2[k],dfh2[vt[k][i]]);
	}
	a[k]-=dfh1[k]+dfh2[k];
	if(a[k]>0)
	{
		dfh2[k]+=a[k];
	}
	else
	{
		dfh1[k]+=a[k];
	}
	return 27;
}
int main()
{
	scanf("%I64d",&n);
	long long x,y;
	for(int i=1;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		vt[x].push_back(y);
		vt[y].push_back(x);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
	}
	dfs(1,0);
	printf("%I64d",dfh2[1]-dfh1[1]);
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>

#ifdef PRINTERS
#include "printers.hpp"
using namespace printers;
#define tr(a)		cerr<<#a<<" : "<<a<<endl;
#else
#define tr(a)
#endif
#define int 		long long
#define ll          long long
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)	for(int i=a;i<b;i++)
using namespace std;
vi adj[100005];
int val[100005];
pii ans[100005];
void dfs(int u,int p){
	for(auto i:adj[u]){
		if(i!=p){
			dfs(i,u);
			ans[u].F=max(ans[u].F,ans[i].F);
			ans[u].S=max(ans[u].S,ans[i].S);
		}
	}
	int newval=val[u]+ans[u].F-ans[u].S;
	if(newval>0)ans[u].S+=newval;
	else ans[u].F+=-newval;
}
void solve(){
	int n;
	cin>>n;
	rep(i,0,n-1){
		int a,b;
		cin>>a>>b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	rep(i,1,n+1)cin>>val[i];
	dfs(1,0);
	cout<<ans[1].F+ans[1].S<<endl;
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)	cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
#define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1005;
const long double epsilon = 1e-9;
ll binexp(ll a, ll b, ll m) {
	a %= m;
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}
ll modinvfermat(ll a, ll m)
{
	return binexp(a, m - 2, m);
}
void task(bool flag)
{
	if (flag)
		cout << "YES\n";
	else
		cout << "NO\n";
}
ll lcm(ll a, ll b)
{
	return ((1LL * a * b) / (__gcd(a, b)));
}
vvl adj(MAXN);
vl vis(MAXN), a(MAXN), dp(MAXN);
ll n, ans;
ll dfs(ll node)
{
	vis[node] = 1;
	dp[node] = a[node];
	ll posmax = 0, negmax = 0;
	for (auto child : adj[node])
	{
		if (vis[child] == 0)
		{
			ll curr = dfs(child);
			if (curr > 0)
				posmax = max(posmax, curr);
			else
				negmax = min(negmax, curr);
		}
	}
	ans += posmax + abs(negmax);
	dp[node] += posmax + negmax;
	return -dp[node];
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	cin >> n;
	loop(i, n - 1)
	{
		ll a, b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	FOR(i, 1, n)	cin >> a[i];
	dfs(1);
	cout << ans + abs(dp[1]) << END;

#ifndef ONLINE_JUDGE
	cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
	return 0;
}


// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)	cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
#define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1005;
const long double epsilon = 1e-9;
ll binexp(ll a, ll b, ll m) {
	a %= m;
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}
ll modinvfermat(ll a, ll m)
{
	return binexp(a, m - 2, m);
}
void task(bool flag)
{
	if (flag)
		cout << "YES\n";
	else
		cout << "NO\n";
}
ll lcm(ll a, ll b)
{
	return ((1LL * a * b) / (__gcd(a, b)));
}
vvl adj(MAXN);
vl vis(MAXN), a(MAXN), dp(MAXN), posdp(MAXN), negdp(MAXN);
ll n, ans;
void dfs(ll node)
{
	vis[node] = 1;
	dp[node] = a[node];
	for (auto child : adj[node])
	{
		if (vis[child] == 0)
		{
			dfs(child);
			posdp[node] = max(posdp[node], posdp[child]);
			negdp[node] = min(negdp[node], negdp[child]);
		}
	}
	dp[node] += posdp[node] + negdp[node];
	if (dp[node] > 0)
		negdp[node] -= dp[node];
	else
		posdp[node] += (-dp[node]);
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	cin >> n;
	loop(i, n - 1)
	{
		ll a, b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	FOR(i, 1, n)	cin >> a[i];
	dfs(1);
	ans = posdp[1] + abs(negdp[1]);
	cout << ans << END;

#ifndef ONLINE_JUDGE
	cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
	return 0;
}


// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)	cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
#define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1005;
const long double epsilon = 1e-9;
ll binexp(ll a, ll b, ll m) {
	a %= m;
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}
ll modinvfermat(ll a, ll m)
{
	return binexp(a, m - 2, m);
}
void task(bool flag)
{
	if (flag)
		cout << "YES\n";
	else
		cout << "NO\n";
}
ll lcm(ll a, ll b)
{
	return ((1LL * a * b) / (__gcd(a, b)));
}
vvl adj(MAXN);
vl vis(MAXN), a(MAXN), dp(MAXN), posdp(MAXN), negdp(MAXN);
ll n, ans;
void dfs(ll node)
{
	vis[node] = 1;
	dp[node] = a[node];
	for (auto child : adj[node])
	{
		if (vis[child] == 0)
		{
			dfs(child);
			posdp[node] = max(posdp[node], posdp[child]);
			negdp[node] = min(negdp[node], negdp[child]);
		}
	}
	dp[node] += posdp[node] + negdp[node];
	ans += abs(dp[node]);
	if (dp[node] > 0)
		negdp[node] -= dp[node];
	else
		posdp[node] += (-dp[node]);
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	cin >> n;
	loop(i, n - 1)
	{
		ll a, b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	FOR(i, 1, n)	cin >> a[i];
	dfs(1);
	cout << ans << END;

#ifndef ONLINE_JUDGE
	cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long int

vector < ll > ad[100005];

ll ara[100005],incr[100005],decr[100005],ans=0;

void dfs(ll src, ll prev)
{
    for(ll i=0; i<ad[src].size(); i++)
    {
        ll v=ad[src][i];

        if(v==prev) continue;

        dfs(v,src);
    }

    ll in=0,de=0;

    for(ll i=0; i<ad[src].size(); i++)
    {
        ll v=ad[src][i];

        if(v==prev) continue;

        in=max(in,incr[v]);

        de=max(de,decr[v]);

    }

    incr[src]=in,decr[src]=de;

    ll new_val=ara[src]+in-de;

    if(new_val>0)
    {
        decr[src]+=new_val;
    }

    else if(new_val<0)
    {
        incr[src]+=-new_val;
    }
}

int main()
{
    ll n,i,j;

    scanf("%I64d",&n);

    for(i=1; i<=n-1; i++)
    {
        ll u,v;

        scanf("%I64d %I64d",&u,&v);

        ad[u].push_back(v);

        ad[v].push_back(u);
    }

    for(i=1; i<=n; i++)
    {
        scanf("%I64d",&ara[i]);
    }

    dfs(1,0);

    ans=incr[1]+decr[1];

    printf("%I64d\n",ans);

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long int

vector < ll > ad[100005];

ll color[100005],inc[100005],decr[100005];

void dfs(int src, int prev)
{
    for(int i=0; i<ad[src].size(); i++)
    {
        int v=ad[src][i];

        if(v!=prev)
        {
            dfs(v,src);

            inc[src]+=inc[v];

            decr[src]+=decr[v];

        }
    }

    color[src]+=inc[src]-decr[src];

    if(color[src]<0)
    {
        inc[src]-=color[src];
    }

    else
    {
        decr[src]+=color[src];
    }
}

int main()
{
    ll n,i,j;

    scanf("%I64d",&n);

    for(i=1; i<=n-1; i++)
    {
        ll u,v;

        scanf("%I64d %I64d",&u,&v);

        ad[u].push_back(v);

        ad[v].push_back(u);
    }

    for(i=1; i<=n; i++)
    {
        scanf("%I64d",&color[i]);
    }

    dfs(1,0);

    /*for(i=1; i<=n; i++)
    {
        cout<<"ok: "<<inc[i]<<' '<<decr[i]<<endl;
    }*/

    ll ans=inc[1]+decr[1];

    printf("%I64d\n",ans);

    return 0;

}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second

using namespace std ;
const long long maxn = 1e5 + 5 ;
const long long MOD = 1e9 + 7 ;
const long long MAXX = 4e18 ;

long long n, x, y, s, k, z, l, p, r, f[maxn], a[maxn];
vector <int> g[maxn];

void dfs(int v, int p = -1){
    for(auto u : g[v])
	if(u != p)
	    f[u] = v, dfs(u, v);
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 0; i < n - 1; i++){
	cin >> x >> y, x--, y--;
	g[x].pb(y), g[y].pb(x);
    }
    for(int i = 0; i < n; i++)
	cin >> a[i];
    dfs(0);
    for(int i = 1; i < n; i++){
	k = a[f[i]] - a[i];
	if(k < 0) k = k * -1;
	s = s + k;
    }
    if(a[0] < 0) a[0] *= -1;
    s = s + a[0];
    cout << s;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second

using namespace std ;
const long long maxn = 1e5 + 5 ;
const long long MOD = 1e9 + 7 ;
const long long MAXX = 4e18 ;

long long n, x, y, s, k, z, l, p, r, f[maxn], a[maxn];
vector <int> g[maxn];

void dfs(int v, int p = -1){
    for(auto u : g[v])
	if(u != p)
	    f[u] = v, dfs(u, v);
    s = s + abs(a[v]);
    a[f[v]] -= a[v];
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 0; i < n - 1; i++){
	cin >> x >> y, x--, y--;
	g[x].pb(y), g[y].pb(x);
    }
    for(int i = 0; i < n; i++)
	cin >> a[i];
    dfs(0);
    cout << s;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second

using namespace std ;
const long long maxn = 1e5 + 5 ;
const long long MOD = 1e9 + 7 ;
const long long MAXX = 4e18 ;

long long n, x, y, s, k, z, l, r, dp1[maxn], dp2[maxn], a[maxn];
pair <long long, long long> pa[maxn];
vector <int> g[maxn];
bool mark[maxn];

pair<long long, long long> dfs(int v, int p = -1){
    long long manfi = 0, mosbat = 0;
    if(mark[v])
	return pa[v];
    mark[v] = 1;
    for(auto u : g[v])
	if(u != p){
	    manfi = max(manfi, dfs(u, v).X);
	    mosbat = max(mosbat, dfs(u, v).Y);
	}
    a[v] -= manfi, a[v] += mosbat;
    if(a[v] > 0)
	return pa[v] = {manfi + a[v], mosbat};
    else
	return pa[v] = {manfi, mosbat - a[v]};
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 0; i < n - 1; i++){
	cin >> x >> y, x--, y--;
	g[x].pb(y), g[y].pb(x);
    }
    for(int i = 0; i < n; i++)
	cin >> a[i];
    cout << dfs(0).X + dfs(0).Y << endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100002];
long long c[100002];
int v[100002];
long long dp[100002][2];
long long ans =0;
long long d = 0;
void dfs(int s) {
v[s] = 1;
long long mx = 0;
long long mn = 0;
if(c[s] >0) mn = c[s];
else mx = -c[s];
for(int i =0;i<g[s].size();i++){
    int o = g[s][i];
    if(!v[o]) {
        dfs(o);
        mx = max(mx,dp[o][0]);
        mn = max(mn,dp[o][1]);
    }
}
dp[s][0] += mx;
dp[s][1] +=mn;
c[s]+= mx -mn;
if(c[s] > 0) dp[s][1] +=c[s];
else dp[s][0] += -c[s];
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >>n;
    for(int i =0;i<n-1;i++){
     int a,b;
     cin >> a >> b;
     g[a-1].push_back(b-1);
     g[b-1].push_back(a-1);
    }
    for(int i =0;i<n;i++) cin >> c[i];
    dfs(0);
    cout << dp[0][1] + dp[0][0] << endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100002];
long long c[100002];
int v[100002];
long long dp[100002];
long long ans =0;
long long d = 0;
void dfs(int s) {
v[s] = 1;
dp[s] = c[s];
long long mx = -2e18;
long long mn = 2e18;
for(int i =0;i<g[s].size();i++){
    int o = g[s][i];
    if(!v[o]) {
        dfs(o);
        mx = max(dp[o],mx);
        mn = min(dp[o],mn);
    }
}
if(mn!=2e18) {
if(mx >=0 && mn >=0) {
        ans+=mx;
        d-=mx;
}
else if(mx<=0 && mn <=0) {
        ans-=mn;
        d-=mn;
}
else {
        ans += mx - mn;
        d+= -mx-mn;
}
}
dp[s]+=d;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >>n;
    for(int i =0;i<n-1;i++){
     int a,b;
     cin >> a >> b;
     g[a-1].push_back(b-1);
     g[b-1].push_back(a-1);
    }
    for(int i =0;i<n;i++) cin >> c[i];
    dfs(0);
    cout << ans+abs(dp[0]) << endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100002];
long long c[100002];
int v[100002];
long long dp[100002];
long long ans =0;
long long d = 0;
void dfs(int s) {
v[s] = 1;
dp[s] = c[s];
bool x = false;
long long mx,mn;
for(int i =0;i<g[s].size();i++){
    int o = g[s][i];
    if(!v[o]) {
        dfs(o);
        if(!x) {
            mx = dp[o];
            mn = dp[o];
            x = true;
        }
        else {
        mx = max(dp[o],mx);
        mn = min(dp[o],mn);
        }
    }
}
if(x) {
if(mx >=0 && mn >=0) {
        ans+=mx;
        d-=mx;
}
else if(mx<=0 && mn <=0) {
        ans-=mn;
        d-=mn;
}
else {
        ans += mx - mn;
        d+= -mx-mn;
}
}
dp[s]+=d;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >>n;
    for(int i =0;i<n-1;i++){
     int a,b;
     cin >> a >> b;
     g[a-1].push_back(b-1);
     g[b-1].push_back(a-1);
    }
    for(int i =0;i<n;i++) cin >> c[i];
    dfs(0);
    cout << ans+abs(dp[0]) << endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100002];
long long c[100002];
int v[100002];
long long dp[100002];
long long ans =0;
long long d = 0;
long long dfs(int s) {
v[s] = 1;
dp[s] = c[s];

bool x = false;
long long mx,mn;
long long f = 0;
for(int i =0;i<g[s].size();i++){
    int o = g[s][i];
    if(!v[o]) {
        f+=dfs(o);
        if(!x) {
            mx = dp[o];
            mn = dp[o];
            x = true;
        }
        else {
        mx = max(dp[o],mx);
        mn = min(dp[o],mn);
        }
    }
}
if(x) {
if(mx >=0 && mn >=0) {
        ans+=mx;
        f-=mx;
}
else if(mx<=0 && mn <=0) {
        ans-=mn;
        f-=mn;
}
else {
        ans += mx - mn;
        f+= -mx-mn;
}
}
dp[s]+=f;
return f;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >>n;
    for(int i =0;i<n-1;i++){
     int a,b;
     cin >> a >> b;
     g[a-1].push_back(b-1);
     g[b-1].push_back(a-1);
    }
    for(int i =0;i<n;i++) cin >> c[i];
    dfs(0);
    cout << ans+abs(dp[0]) << endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100002];
long long c[100002];
int v[100002];
long long dp[100002];
long long ans =0;
long long d = 0;
int dfs(int s) {
v[s] = 1;
dp[s] = c[s];
long long mx = -1;
long long mn = INT_MAX;
for(int i =0;i<g[s].size();i++){
    int o = g[s][i];
    if(!v[o]) {
        dfs(o);
        mx = max(dp[o],mx);
        mn = min(dp[o],mn);
    }
}
if(mn !=INT_MAX) {
if(mx >=0 && mn >=0) {
        ans+=mx;
        d-=mx;
}
else if(mx<=0 && mn <=0) {
        ans-=mn;
        d-=mn;
}
else {
        ans += mx - mn;
        d+= -mx-mn;
}
}
dp[s]+=d;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >>n;
    for(int i =0;i<n-1;i++){
     int a,b;
     cin >> a >> b;
     g[a-1].push_back(b-1);
     g[b-1].push_back(a-1);
    }
    for(int i =0;i<n;i++) cin >> c[i];
    dfs(0);
    cout << ans+abs(dp[0]) << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
const int mxn=1e5+69;
vector<int> adj[mxn];
ll v[mxn];

ll dp1[mxn]={};
ll dp2[mxn]={};
void dfs(int cur,int prev)
{
	if(v[cur]>0)
	{
		dp1[cur]=0;
		dp2[cur]=v[cur];
	}
	else
	{
		dp1[cur]=-v[cur];
		dp2[cur]=0;
	}

	for(int i:adj[cur])
	{
		if(i==prev)continue;
		dfs(i,cur);
		dp1[cur]=max(dp1[cur],dp1[i]);
		dp2[cur]=max(dp2[cur],dp2[i]);
	}

	ll diff=dp1[cur]-dp2[cur];

	//cout<<cur<<" "<<diff<<endl;
	if(v[cur]+diff>0 && diff>=0)
	{
	    dp2[cur]=dp1[cur]+v[cur];
	}
	else if(v[cur]+diff>0 && diff<0)
	{
	    dp2[cur]=dp1[cur]-v[cur];
	}
	else if(v[cur]+diff<0 && diff>=0)
	{
	    dp1[cur]=dp2[cur]+v[cur];
	}
	else if(v[cur]+diff<0 && diff<=0)
	{
	    dp1[cur]=dp2[cur]-v[cur];
	}

}
int main()
{
	int n;
	cin>>n;

	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1;i<=n;i++)cin>>v[i];

	dfs(1,0);
    cout<<dp1[1]+dp2[1]<<endl;

	return 0;
}


#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
const int mxn=1e5+69;
vector<int> adj[mxn];
ll v[mxn];

ll dp1[mxn]={};
ll dp2[mxn]={};
void dfs(int cur,int prev)
{
	if(v[cur]>0)
	{
		dp1[cur]=0;
		dp2[cur]=v[cur];
	}
	else
	{
		dp1[cur]=-v[cur];
		dp2[cur]=0;
	}

	for(int i:adj[cur])
	{
		if(i==prev)continue;
		dfs(i,cur);
		dp1[cur]=max(dp1[cur],dp1[i]);
		dp2[cur]=max(dp2[cur],dp2[i]);
	}
	if(dp1[cur]>=dp2[cur])
	{
	    if(v[cur]>=0)
	    {
	        dp2[cur]=dp1[cur]+v[cur];
	    }
	    else
	    {
	        if(dp1[cur]-dp2[cur]>=abs(v[cur]))
	        {
	            dp2[cur]=dp1[cur]-abs(v[cur]);
	        }
	        else
	           dp1[cur]=dp2[cur]+abs(v[cur]);
	    }
	}
	else
	{
	    if(v[cur]>=0)
	    {
	        if(dp2[cur]-dp1[cur]>=v[cur])
	        {
	            dp1[cur]=dp2[cur]-v[cur];
	        }
	        else
	        {
	            dp2[cur]=dp1[cur]+v[cur];
	        }
	    }
	    else
	    {
	        dp1[cur]=dp2[cur]+abs(v[cur]);
	    }
	}

}
int main()
{
	int n;
	cin>>n;

	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1;i<=n;i++)cin>>v[i];

	dfs(1,0);
    cout<<dp1[1]+dp2[1]<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define speed ios::sync_with_stdio(false)
#define ll long long int
#define ld float
#define mset(x,val) memset(x,val,sizeof(x))
#define pb push_back
#define in insert
#define mp make_pair
#define F first
#define S second
#define ifl(i,a,b) for(ll i=a;i<b;i++)
#define dfl(i,a,b) for(ll i=a-1;i>=b;i--)
#define get_string() getline(cin,s)     //for inputting string with spacebars in between
//#define gcd(a,b) __gcd(a,b)
#define vi vector<ll>
#define vb vector<bool>
#define vit vi::iterator
#define PL pair<ll,ll>
#define PLL pair<ll,pair<ll,ll> >
#define PLLL pair<ll,pair<ll,pair<ll,ll> > >
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define present(container,element) (container.find(element)!=container.end())   //for sets
#define cpresent(container,element) (find(all(container),element)!=container.end())     //for vectors
#define tr(c,i) for(__typeof((c).begin() i=(c).begin();i!=(c).end();i++)
#define infi INT_MAX
#define cases() ll t;cin>>t;while(t--)

const ll N = 2e5+10;
const ll mod = 1e9+7;
const ll level = 20;
// ll dx[] = {1,-1,0,0,1,1,-1,-1};
// ll dy[] = {0,0,1,-1,1,-1,1,-1};
//const ld E = 2.71828;
//const ld PI = 3.14159265;

ll cntMinus[N],cntPlus[N],arr[N];
vi v[N];

void dfs(ll cur,ll par)
{
    vi sons;
    tr (v[cur],it)
    {
        ll x = *it;
        if (x != par)
        {
            sons.pb(x);
            dfs(x,cur);
        }
    }
    ll tempMinus = 0;
    ll tempPlus = 0;

    ifl (i,0,sons.size())
    {
        tempMinus = max(tempMinus,cntMinus[sons[i]]);
        tempPlus = max(tempPlus,cntPlus[sons[i]]);
    }

    ll cost = tempPlus + tempMinus + arr[cur];

//    cout << cur << " " << cost << " " << tempPlus << " " << tempMinus << endl;

    if (cost > 0)
        cntPlus[cur] = cost;
    else
        cntMinus[cur] = abs(cost);

}

int main()
{
    speed;
    ll n;
    cin >> n;
    ifl (i,1,n)
    {
        ll x,y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    ifl (i,1,n+1)
        cin >> arr[i];
    dfs(1,-1);
    cout << cntMinus[1] + cntPlus[1] << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define speed ios::sync_with_stdio(false)
#define ll long long int
#define ld float
#define mset(x,val) memset(x,val,sizeof(x))
#define pb push_back
#define in insert
#define mp make_pair
#define F first
#define S second
#define ifl(i,a,b) for(ll i=a;i<b;i++)
#define dfl(i,a,b) for(ll i=a-1;i>=b;i--)
#define get_string() getline(cin,s)     //for inputting string with spacebars in between
//#define gcd(a,b) __gcd(a,b)
#define vi vector<ll>
#define vb vector<bool>
#define vit vi::iterator
#define PL pair<ll,ll>
#define PLL pair<ll,pair<ll,ll> >
#define PLLL pair<ll,pair<ll,pair<ll,ll> > >
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define present(container,element) (container.find(element)!=container.end())   //for sets
#define cpresent(container,element) (find(all(container),element)!=container.end())     //for vectors
#define tr(c,i) for(__typeof((c).begin() i=(c).begin();i!=(c).end();i++)
#define infi INT_MAX
#define cases() ll t;cin>>t;while(t--)

const ll N = 2e5+10;
const ll mod = 1e9+7;
const ll level = 20;
// ll dx[] = {1,-1,0,0,1,1,-1,-1};
// ll dy[] = {0,0,1,-1,1,-1,1,-1};
//const ld E = 2.71828;
//const ld PI = 3.14159265;

ll cntMinus[N],cntPlus[N],arr[N];
vi v[N];

void dfs(ll cur,ll par)
{
    vi sons;
    tr (v[cur],it)
    {
        ll x = *it;
        if (x != par)
        {
            sons.pb(x);
            dfs(x,cur);
        }
    }
    ll tempMinus = 0;
    ll tempPlus = 0;

    ifl (i,0,sons.size())
    {
        tempMinus = max(tempMinus,cntMinus[sons[i]]);
        tempPlus = max(tempPlus,cntPlus[sons[i]]);
    }

    ll cost = tempPlus - tempMinus + arr[cur];

    cntPlus[cur] = tempPlus;
    cntMinus[cur] = tempMinus;

    if (cost < 0)
        cntPlus[cur] += abs(cost);
    else
        cntMinus[cur] += abs(cost);

}

int main()
{
    speed;
    ll n;
    cin >> n;
    ifl (i,1,n)
    {
        ll x,y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    ifl (i,1,n+1)
        cin >> arr[i];
    dfs(1,-1);
    cout << cntMinus[1] + cntPlus[1] << endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file freopen("out.out","w",stdout);
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

int n;
vi g[111111];
ll val[111111];
int f[111111];

void mtree(int x){
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(f[to]==-1){
						f[to]=x;
						mtree(to);
				}
		}
}

ll dfs(int x){
		if(g[x].size()==1)return val[x];
		ll p=0,n=0;
		FOR(i,0,g[x].size()){
				ll haha=dfs(g[x][i]);
				if(haha<0)n=min(n,haha);
				else p=max(p,haha);
		}
		return p+n+val[x];
}

int main() {
		fastio
		memset(f,-1,sizeof(f));
		cin>>n;
		FOR(i,1,n){
				int a,b;
				cin>>a>>b;
				a--;
				b--;
				g[a].push_back(b);
				g[b].push_back(a);
		}
		FOR(i,0,n){
				cin>>val[i];
		}
		f[0]=0;
		mtree(0);
		ll ans=0;
		FOR(i,0,g[0].size()){
				ans+=abs(dfs(g[0][i]));
		}
		cout<<ans+abs(val[0])<<endl;

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file freopen("out.out","w",stdout);
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

int n;
vi g[111111];
ll val[111111];
ll add[111111];
ll mns[111111];
int f[111111];

void mtree(int x){
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(f[to]==-1){
						f[to]=x;
						mtree(to);
				}
		}
}
void dfs(int x){
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(to==f[x])continue;
				dfs(to);
				add[i]=max(add[i],add[to]);
				mns[i]=max(mns[i],mns[to]);
		}
		val[x]+=add[x];
		val[x]-=mns[x];
		if(val[x]>0)mns[x]+=abs(val[x]);
		else add[x]+=abs(val[x]);
}

int main() {
		fastio
		memset(f,-1,sizeof(f));
		cin>>n;
		FOR(i,1,n){
				int a,b;
				cin>>a>>b;
				a--;
				b--;
				g[a].push_back(b);
				g[b].push_back(a);
		}
		FOR(i,0,n){
				cin>>val[i];
		}
		f[0]=0;
		mtree(0);
		dfs(0);
		cout<<add[0]+mns[0]<<endl;

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file freopen("out.out","w",stdout);
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

int n;
vi g[111111];
ll val[111111];
ll add[111111];
ll mns[111111];
int f[111111];

void mtree(int x,int p=0){
		f[x]=p;
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(to!=p){
						mtree(to,x);
				}
		}
}
void dfs(int x){
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(to==f[x])continue;
				dfs(to);
				add[x]=max(add[x],add[to]);
				mns[x]=max(mns[x],mns[to]);
		}
		val[x]+=add[x];
		val[x]-=mns[x];
		if(val[x]>0)mns[x]+=abs(val[x]);
		else add[x]+=abs(val[x]);
}

int main() {
		fastio
		memset(f,-1,sizeof(f));
		cin>>n;
		FOR(i,1,n){
				int a,b;
				cin>>a>>b;
				a--;
				b--;
				g[a].push_back(b);
				g[b].push_back(a);
		}
		FOR(i,0,n){
				cin>>val[i];
		}
		f[0]=0;
		mtree(0);
		dfs(0);
		cout<<add[0]+mns[0]<<endl;

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file freopen("out.out","w",stdout);
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

int n;
vi g[111111];
ll val[111111];
int f[111111];

void mtree(int x){
		FOR(i,0,g[x].size()){
				int to=g[x][i];
				if(f[to]==-1){
						f[to]=x;
						mtree(to);
				}
		}
}

ll dfs(int x){
		if(g[x].size()==1)return val[x];
		ll p=0,n=0;
		FOR(i,0,g[x].size()){
				if(g[x][i]==f[x])continue;
				ll haha=dfs(g[x][i]);
				if(haha<0)n=min(n,haha);
				else p=max(p,haha);
		}
		return p+n+val[x];
}

int main() {
		fastio
		memset(f,-1,sizeof(f));
		cin>>n;
		FOR(i,1,n){
				int a,b;
				cin>>a>>b;
				a--;
				b--;
				g[a].push_back(b);
				g[b].push_back(a);
		}
		FOR(i,0,n){
				cin>>val[i];
		}
		f[0]=0;
		mtree(0);
		ll ans=0;
		FOR(i,0,g[0].size()){
				ans+=abs(dfs(g[0][i]));
		}
		cout<<ans+abs(val[0])<<endl;

		return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const int N=1e5+5;

int dp[N][2],a[N];

vector <int> adj[N];

void dfs(int k,int par)
{
     for(auto &itr:adj[k])
     {
          if(itr==par)
               continue;
          dfs(itr,k);
          dp[k][0]=max(dp[itr][0],dp[k][0]);
          dp[k][1]=max(dp[itr][1],dp[k][1]);
     }
     a[k]-=dp[k][1];
     a[k]+=dp[k][0];
     if(a[k]<0)
          dp[k][0]+=abs(a[k]);
     else
          dp[k][1]+=a[k];
}

int32_t main()
{
     IOS;

     int n;
     cin>>n;

     for(int i=1;i<n;i++)
     {
          int x,y;
          cin>>x>>y;
          adj[x].pb(y);
          adj[y].pb(x);
     }

     for(int i=1;i<=n;i++)
          cin>>a[i];

     dfs(1,-1);

     cout<<dp[1][1]+dp[1][0];

     return 0;
}


/*Always success does not build on success,success build on failure and hardwork*/
#include <bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vec         vector<ll>
#define map         map<ll,ll>
#define scn(t)      scanf("%d",&t);
#define lscn(t)      scanf("%lld",&t);
#define mod         1000000007
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define endl        "\n"
using namespace std;
const ll N=1e6+5;
vec v[N];
ll val[N],add[N],subtract[N];
void dfs(int node,int par)
{
    for(auto it: v[node])
    {
        if(it!=par)
        {
            dfs(it,node);
            add[node]=max(add[node],add[it]);
            subtract[node]=max(subtract[node],subtract[it]);
        }
    }

    ll z=(add[node]-subtract[node]);

    val[node]+=z;

    if(val[node]<0)
    {
        add[node]-=val[node];
    }
    else
    {
        subtract[node]+=val[node];
    }
}
int main()
{
    int node;
    scn(node);

    for(int i=1;i<node;i++)
    {
        int a,b;
        scn(a);
        scn(b);

        v[a].pb(b);
        v[b].pb(a);
    }

    for(int i=1;i<=node;i++)
    lscn(val[i]);

    dfs(1,0);

    cout<<add[1]+subtract[1]<<endl;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <iomanip>
//#include <windows.h>  //Should be deleted when using AtCoder&POJ
using namespace std;

#define ll long long
#define pii pair<int,int>
/**==Info==
*Program:3
*Problem:Zero Tree
*Date:2018-2-6
*Algorithm:Teacher's Solution
*Stats:Unknown*/

bool debug=false;

int n;
vector<int> nei[100005];
int c[100005];
int a[100005],b[100005];

int dfs(int v){
	for(int i=0;i<nei[v].size();i++){
		dfs(nei[v][i]);
		a[v]=max(a[v],a[nei[v][i]]);
		b[v]=max(b[v],b[nei[v][i]]);
	}

	c[v]=c[v]+a[v]-b[v];
	if(c[v]<0) a[v]-=c[v]; else b[v]+=c[v];
		//cout<<a[v]<<" "<<b[v]<<endl;
}

int main(int argc,char* argv[]){
	cin>>n;
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		nei[a].push_back(b);
	}
	for(int i=0;i<n;i++){
		cin>>c[i];
	}

	dfs(0);
	cout<<a[0]+b[0];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <iomanip>
//#include <windows.h>  //Should be deleted when using AtCoder&POJ
using namespace std;

#define ll long long
#define pii pair<int,int>
/**==Info==
*Program:3
*Problem:Zero Tree
*Date:2018-2-6
*Algorithm:Teacher's Solution
*Stats:Unknown*/

bool debug=false;

ll n;
vector<ll> nei[100005];
ll c[100005];
ll a[100005],b[100005];

void dfs(ll v,ll p){
	for(int i=0;i<nei[v].size();i++){
		if(nei[v][i]==p) continue;
		dfs(nei[v][i],v);
		a[v]=max(a[v],a[nei[v][i]]);
		b[v]=max(b[v],b[nei[v][i]]);
	}

	c[v]=c[v]+a[v]-b[v];
	if(c[v]<0) a[v]-=c[v]; else b[v]+=c[v];
		//cout<<a[v]<<" "<<b[v]<<endl;
}

int main(int argc,char* argv[]){
	cin>>n;
	for(int i=1;i<=n-1;i++){
		int a,b;
		cin>>a>>b;
		nei[b].push_back(a);
		nei[a].push_back(b);
	}
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}

	dfs(1,0);
	cout<<a[1]+b[1];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <iomanip>
//#include <windows.h>  //Should be deleted when using AtCoder&POJ
using namespace std;

#define ll long long
#define pii pair<int,int>
/**==Info==
*Program:3
*Problem:Zero Tree
*Date:2018-2-6
*Algorithm:Teacher's Solution
*Stats:Unknown*/

bool debug=false;

int n;
vector<int> nei[100005];
int c[100005];
int a[100005],b[100005];

void dfs(int v,int p){
	for(int i=0;i<nei[v].size();i++){
		if(nei[v][i]==p) continue;
		dfs(nei[v][i],v);
		a[v]=max(a[v],a[nei[v][i]]);
		b[v]=max(b[v],b[nei[v][i]]);
	}

	c[v]=c[v]+a[v]-b[v];
	if(c[v]<0) a[v]-=c[v]; else b[v]+=c[v];
		//cout<<a[v]<<" "<<b[v]<<endl;
}

int main(int argc,char* argv[]){
	cin>>n;
	for(int i=1;i<=n-1;i++){
		int a,b;
		cin>>a>>b;
		nei[b].push_back(a);
		nei[a].push_back(b);
	}
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}

	dfs(1,0);
	cout<<a[1]+b[1];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define INF 1e9
#define all(v) v.begin(),v.end()
#define CHAL_BAAP_KO_MT_SIKHA ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007;
/*
    Damn Fast:TheFuckinMastermind
*/
int moves;
int vis[200005];
vector<int> v[200005];
int a[200005];
pair<int,int> dfs(int node,int inc,int dec){
    vis[node]=1;
    int inc1=inc,dec1=dec;
    for(auto i:v[node])
    {
        if(vis[i]==0)
        {
            pair<int,int> p1=dfs(i,inc,dec);
            inc1=max(inc1,p1.fr);
            dec1=max(dec1,p1.se);
        }
    }
    a[node]+=inc1;
    a[node]-=dec1;
    if(a[node]<0){
        inc1+=abs(a[node]);
    }
    else{
        dec1+=a[node];
    }
    return {inc1,dec1};
}
signed main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    CHAL_BAAP_KO_MT_SIKHA
    int n;
    cin>>n;
    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1;i<=n;i++)
        cin>>a[i];
    pair<int,int> p1=dfs(1,0,0);
    cout<<p1.fr+p1.se<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int c[MAXN];
long long up[MAXN], down[MAXN];
vector<int> g[MAXN];
int n;
long long dfs(int x, int p=0) {
	for (int v : g[x]) {
		if (v == p) continue;
		dfs(v, x);
		up[x] = max(up[x], up[v]);
		down[x] = max(down[x], down[v]);
	}
	long long ans = c[x] + up[x] - down[x];
	if (ans > 0) down[x] += ans;
	else up[x] -= ans;
	return up[x] + down[x];
}

int main() {

	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%d", c+i);
	}
	printf("%lld\n", dfs(1));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include<string.h>
#define inf 9000000000000000000
#define maxn 200006
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
#define endl '\n'
#define mapint_iterator map<int,int> :: iterator
#define mapll_iterator map<ll,ll> :: iterator
#define setint_iterator set<int> :: iterator
#define setll_iterator set<ll> :: iterator
#define setpint_iterator set<pint> :: iterator
#define setpll_iterator set<pll> :: iterator
#define trace(x) cout<<#x<<": "<<x<<" "<<endl
#define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define cout1(a) cout<<a<<endl
#define cout2(a,b) cout<<a<<' '<<b<<endl
#define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define vcout(v,i) cout<<v[i].fi<<" "<<v[i].se<<endl
typedef priority_queue<ll,vector<ll>,greater<ll> > pqset;
typedef priority_queue<pint,vector<pint>,greater<pint> > pqset_ll;
#define pb push_back
#define fi first
#define se second
#include<ext/pb_ds/assoc_container.hpp>
ll dxk[]={0,0,1,-1,1,1,-1,-1};
ll dyk[]={1,-1,0,0,1,-1,1,-1};
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
#define acc (ios::sync_with_stdio(false),cin.tie(0))
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(ll i=n;i>0;i--)
#define repeat(i,start,n) for(ll i=start;i<n;i++)
#define print(a,n) for(ll i=0;i<n;i++){ cout<<a[i]<<" ";}
ll ll_max(ll a,ll b,ll c){return max(a,max(b,c));}
int int_max(int a,int b,int c){return max(a,max(b,c));}
ll ll_min(ll a,ll b,ll c){return min(a,min(b,c));}
int int_min(int a,int b,int c){return min(a,min(b,c));}
ll max(int a,ll b){ return max((ll)a,b);}
ll min(int a,ll b){ return min((ll)a,b);}
ll min(ll a,int b){ return min(a,(ll)b);}
ll max(ll a,int b){ return max(a,(ll)b);}
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll power(ll a,ll b){
	if(a==1)
		return 1;
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	ll res=1;
	if(b%2){
		res=(c*c)%mod;
		res*=a;
		res%=mod;
	}
	else
		res=((c*c)%mod);
	return res;
}
//ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
//Chronology: Binary Search,Greedy,dp,dfs and similar
//Use (<<) for multiplication
//Use (>>) for division
// In dp, first try top-down,then bottom-up
//ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
// find_by_order -> value at index
// order_of_key -> index of value
// while using (1<<i) use ((ll)1<<(ll)i)
// if global declared initialise per testcase
ll a[100001];
vector<int> v[100001];
pll dfs(int i,int p){
	pll maxx;
	maxx.first=0;
	maxx.se=0;
	rep(j,v[i].size()){
		if(v[i][j]==p)
			continue;
		pll a1=dfs(v[i][j],i);
		maxx.first=max(maxx.first,a1.first);
		maxx.se=max(maxx.se,a1.se);
	}
	ll k=maxx.second-maxx.first;
	a[i]-=k;
	if(a[i]>0)
		maxx.se+=a[i];
	else
		maxx.first+=abs(a[i]);
	return maxx;
}
void solve(int countu){
	int n;
	cin>>n;
	rep(i,n-1){
		int x,y;
		cin>>x>>y;
		v[x].pb(y);
		v[y].pb(x);
	}
	repeat(i,1,n+1)
		cin>>a[i];
	pll maxx=dfs(1,-1);
	cout1(maxx.fi+maxx.se);
}
int main(){
	//ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);cout.precision(10);
    int t=1;
	//scanf("%d",&t);
	int countu=1;
    while(t--){
    	solve(countu);
    	countu++;
    }
}


                                      /*  ***  In The Name of God ... ***  */
                                      /* Do Not Keep Calm , It Is Time To WakeUP*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bug() cout << "BUG" << endl ;
#define X first
#define Y second
#define Size(x) (int)x.size()
typedef long long ll;
const ll INF = 1e18 ;

const ll MAXN = 1e5 + 100 ;
ll n , a[MAXN] , dp[2][MAXN] ;
vector < ll > adj[MAXN] ;

void dfs(ll x , ll pr = -1)
{
	if(a[x] < 0)
		dp[0][x] = abs(a[x]) ;
	else if(a[x] > 0)
		dp[1][x] = a[x] ;
	ll mx1 = 0 , mx2 = 0 ;
	for(auto u : adj[x])
	{
		if(u == pr)
			continue ;
		dfs(u , x) ;
//		if(x == 4)
//			cout << dp[0][u] << " " << dp[1][u] << endl ;
		mx1 = max(dp[0][u] , mx1) , mx2 = max(dp[1][u] , mx2) ;
	}
	if(adj[x].size() == 1 && x != 0)
		return ;
	dp[0][x] = mx1 , dp[1][x] = mx2 ;
	if(a[x] + mx1 - mx2 < 0)
		dp[0][x] += -(a[x] + mx1 - mx2) ;
	else
		dp[1][x] += a[x] + mx1 - mx2 ;
	return ;
}

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n ;
//	for(ll i = 0 ; i < n ; i++)
//		for(ll j = 0 ; j <= 1 ; j++)
//			dp[j][i] = -1 ;
	for(ll i = 0 ; i < n - 1 ;i++)
	{
		ll u , v ;
		cin >> u >> v ;
		u-- , v-- ;
		adj[u].pb(v) , adj[v].pb(u) ;
	}
	for(ll i = 0 ; i < n ; i++)
		cin >> a[i] ;
	dfs(0) ;
	cout << dp[0][0] + dp[1][0] ;
}


#include <bits/stdc++.h>

using namespace std;

#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define gold 0

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
//#define freopen(x) freopen(x".in", "r", stdin), freopen (x".out", "w", stdout);
#define ex exit(0)

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

typedef long long ll;
typedef pair<int, int> pii;

const int N = (int) 1e6 + 7;
const int MOD = (int) 1e9 + 7;
const int INF = 2e9;
const ll BIG = 1e18;
const int P = 1000;

vector<vector<int> > g(N);
ll inc[N], dc[N], a[N];
int n, root = 1;

void dfs(int v, int p = -1) {
	for (auto to : g[v]) {
		if (to == p) continue;
		dfs(to, v);
		inc[v] = max(inc[v], inc[to]);
		dc[v] = max(dc[v], dc[to]);
	}
	ll cur = a[v] + (inc[v] - dc[v]);
	if (cur > 0)
		dc[v] += cur;
	else
		inc[v] -= cur;
}

int main() {
	fast_io;
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		g[u].pb(v), g[v].pb(u);
	}
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	dfs(root);
	cout << inc[root] + dc[root];
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
using namespace std;
vector < long > a[100005];
long n,i,x,y,v[100005];
long visi[100005]={0},ans=0;
pair < long , long > dfs(long node)
{
    long i,mn=0,mx=0,d=0;
    pair < long ,long > u;
    u={0,0};
    visi[node]=1;
    for(i=0;i<a[node].size();++i)
    if(!visi[a[node][i]])
    {
        u=dfs(a[node][i]);
        if(u.first<0) mn=min(mn,u.first);
        if(u.first>0) mx=max(mx,u.first);
        v[node]-=u.second;
        d+=u.second;
    }
    ans+=abs(mn)+mx;
    return {-(mn+mx)+v[node],d+(mn+mx)};
}
int main()
{
    cin>>n;
    for(i=0;i<n-1;++i)
    {
        cin>>x>>y;
        a[x].pb(y);
        a[y].pb(x);
    }
    for(i=1;i<=n;++i)
    cin>>v[i];
    ans+=abs(dfs(1).first);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
using namespace std;
vector < long > a[100005];
long n,i,x,y,v[100005];
long visi[100005]={0};
pair < long , long > dfs(long node)
{
    long i,mxp=0,mnm=0;
    pair < long ,long > u;
    visi[node]=1;
    for(i=0;i<a[node].size();++i)
    if(!visi[a[node][i]])
    {
        u=dfs(a[node][i]);
        mxp=max(mxp,u.first);
        mnm=max(mnm,u.second);
    }
    v[node]+=mxp-mnm;
    if(v[node]>0)
    return {mxp,mnm+v[node]};
    else return {mxp+abs(v[node]),mnm};
}
int main()
{
    pair < long ,long > u;
    cin>>n;
    for(i=0;i<n-1;++i)
    {
        cin>>x>>y;
        a[x].pb(y);
        a[y].pb(x);
    }
    for(i=1;i<=n;++i)
    cin>>v[i];
    u=dfs(1);
    cout<<u.first+u.second<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
using namespace std;
vector < long > a[100005];
long n,i,x,y,v[100005];
long visi[100005]={0},ans=0;
pair < long , long > dfs(long node)
{
    long i,mn=0,mx=0;
    pair < long ,long > u;
    u={0,0};
    visi[node]=1;
    for(i=0;i<a[node].size();++i)
    if(!visi[a[node][i]])
    {
        u=dfs(a[node][i]);
        if(u.first<0) mn=min(mn,u.first);
        if(u.first>0) mx=max(mx,u.first);
        v[node]+=u.second;
    }
    ans+=abs(mn)+mx;
    return {-(mn+mx)+v[node],u.second-(mn+mx)};
}
int main()
{
    cin>>n;
    for(i=0;i<n-1;++i)
    {
        cin>>x>>y;
        a[x].pb(y);
        a[y].pb(x);
    }
    for(i=1;i<=n;++i)
    cin>>v[i];
    dfs(1);
    ans+=v[1];
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define PI 3.14159265358979
const int N=200111;
ll arr[N],a,b,n;
vector<int>v[N];
pair<ll,ll>solve(int node,int p){
    pair<ll,ll> ret={(ll)0,arr[node]};
    ll mxI=0,mxD=0;
    for(auto u : v[node]){
        if(u!=p){
            auto tmp=solve(u,node);
            mxI=max(mxI,tmp.F);
            mxD=max(mxD,tmp.S);
        }
    }
    ret.F=mxI;
    ll sum=arr[node]+ret.F;
    if(sum<mxD){
        ret.F+=(mxD-sum);
        ret.S=mxD;
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n;
    for(int i=0 ; i<n-1 ; i++){
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1 ; i<=n ; i++){
        cin>>arr[i];
    }
    auto ans=solve(1,0);
    cout<<ans.F+ans.S+arr[1]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define PI 3.14159265358979
const int N=200111;
ll arr[N],a,b,n;
vector<int>v[N];
pair<ll,ll>solve(int node,int p){
    pair<ll,ll> ret;
    if(arr[node]>=0)
        ret={0,arr[node]};
    else    ret={-arr[node],0};
    ll mxI=LLONG_MAX,mxD=-LLONG_MAX;
    bool lf=1;
    for(auto u : v[node]){
        if(u!=p){
            auto tmp=solve(u,node);
            mxI=min(mxI,tmp.F);
            mxD=max(mxD,tmp.S);
            lf=0;
        }
    }
    if(lf){
        if(arr[node]>=0)
            return {0,arr[node]};
        else    return {-arr[node],0};
    }
    ret.F=mxI;
    ll sum=arr[node]+ret.F;
    if(sum<mxD){
        ret.F+=(mxD-sum);
        ret.S=mxD;
    }
    else{
        ret.S=arr[node]+ret.F;
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n;
    for(int i=0 ; i<n-1 ; i++){
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1 ; i<=n ; i++){
        cin>>arr[i];
    }
    auto ans=solve(1,0);
    cout<<ans.F+ans.S+arr[1]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define PI 3.14159265358979
const int N=200111;
ll arr[N],a,b,n;
vector<int>v[N];
pair<ll,ll>solve(int node,int p){
    pair<ll,ll> ret;
    if(arr[node]>=0)
        ret={0,arr[node]};
    else    ret={-arr[node],0};
    ll mxI=-LLONG_MAX,mxD=-LLONG_MAX;
    bool lf=1;
    for(auto u : v[node]){
        if(u!=p){
            auto tmp=solve(u,node);
            mxI=max(mxI,tmp.F);
            mxD=max(mxD,tmp.S);
            lf=0;
        }
    }
    if(lf){
        if(arr[node]>=0)
            return {0,arr[node]};
        else    return {-arr[node],0};
    }
    ret.F=mxI;
    ll sum=arr[node]+ret.F;
    if(sum<mxD){
        ret.F+=(mxD-sum);
        ret.S=mxD;
    }
    else{
        ret.S=arr[node]+ret.F;
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n;
    for(int i=0 ; i<n-1 ; i++){
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1 ; i<=n ; i++){
        cin>>arr[i];
    }
    auto ans=solve(1,0);
    cout<<ans.F+ans.S<<endl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long       ll;
const int MAX=1e5+9;
int n;
ll val[MAX], add[MAX], sub[MAX];
vector<int> adj[MAX];

void dfs(int cur, int par=-1) {
    for (int i=0; i<adj[cur].size(); ++i) {
        int child=adj[cur][i];
        if (child!=par) {
            dfs(child, cur);
            add[cur]=max(add[cur], add[child]);
            sub[cur]=max(sub[cur], sub[child]);
        }
    }

    val[cur]+=add[cur]-sub[cur];
    if (val[cur]<0) {
        add[cur]+=-val[cur];
    }
    else {
        sub[cur]+=val[cur];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for (int i=0; i<n-1; ++i) {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    for (int i=1; i<=n; ++i) {
        cin >> val[i];
    }

    dfs(1);
    cout << add[1]+sub[1];

    return 0;
}


/*
गिरते हैं शहसवार ही मैदान-ए-जंग में ।
वो तिफ़्ल क्या गिरे जो घुटनों के बल चले ।।
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int     long long int
#define ull     unsigned int
#define ld      long double
#define pb      push_back
#define pa      pair<int,int>
#define ppa     pair<pair<int,int>,int>
#define pap     pair<int,pair<int,int>>
#define v1d     vector<int>
#define v2d     vector<vector<int> >
#define vpa     vector<pair<int,int>>
#define M       1000000007
#define MN      LLONG_MIN
#define MX      LLONG_MAX
#define ff      first
#define ss      second
#define endl    "\n"
#define v1s     vector<string>
#define all(v)  v.begin(),v.end()
#define S(v)    sort(v.begin(),v.end())
#define RS(v)   sort(v.rbegin(),v.rend())
#define R(v)    reverse(v.begin(),v.end())
#define mxpq(T)    priority_queue <T>
#define mnpq(T)    priority_queue<T,vector<T>,greater<T>>
#define T       int tc;cin>>tc;while(tc--)
#define p2d(v)     for(auto a:v){for(auto b:a)cout<<b<<" ";cout<<endl;}
#define p1d(v)      for(auto a:v)cout<<a<<" ";cout<<endl;
#define ppd(v)      for(auto a:v)cout<<a.ff<<" "<<a.ss<<endl;
#define sz(v)       (int)(v.size())
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define qqq 100005
v2d v(qqq);
v1d c(qqq),hunt(qqq);
int ans=0;
int fun(int x,int p){
    int mx=0,mn=0;
    for(auto a:v[x]){
        if(a!=p){
            int z=fun(a,x);
            if(z>0)mx=max(mx,z);
            if(z<0)mn=max(mn,-z);
        }
    }
    ans+=mx+mn;
    hunt[p]+=hunt[x]-mx+mn;
    return c[x]-mx+mn+hunt[x];
}
void Weapons19(){
    int n;cin>>n;
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1;i<=n;i++){cin>>c[i];}
    int x=fun(1,0);
    cout<<ans+abs(x);

}

int32_t main(){

    /*
    त्यक्तवाधर्मं च लोभं च
    मोहंचो द्यमास्थिता।
    युद्ध्यध्वमनहंकारा
    यतो धर्मस्ततो जयः।।
    */

    cout<<fixed<<setprecision(16);
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    #ifndef ONLINE_JUDGE
     freopen("input.txt",  "r",  stdin);
     freopen("output.txt", "w", stdout);
    #endif

    Weapons19();

}


/*
गिरते हैं शहसवार ही मैदान-ए-जंग में ।
वो तिफ़्ल क्या गिरे जो घुटनों के बल चले ।।
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int     long long int
#define ull     unsigned int
#define ld      long double
#define pb      push_back
#define pa      pair<int,int>
#define ppa     pair<pair<int,int>,int>
#define pap     pair<int,pair<int,int>>
#define v1d     vector<int>
#define v2d     vector<vector<int> >
#define vpa     vector<pair<int,int>>
#define M       1000000007
#define MN      LLONG_MIN
#define MX      LLONG_MAX
#define ff      first
#define ss      second
#define endl    "\n"
#define v1s     vector<string>
#define all(v)  v.begin(),v.end()
#define S(v)    sort(v.begin(),v.end())
#define RS(v)   sort(v.rbegin(),v.rend())
#define R(v)    reverse(v.begin(),v.end())
#define mxpq(T)    priority_queue <T>
#define mnpq(T)    priority_queue<T,vector<T>,greater<T>>
#define T       int tc;cin>>tc;while(tc--)
#define p2d(v)     for(auto a:v){for(auto b:a)cout<<b<<" ";cout<<endl;}
#define p1d(v)      for(auto a:v)cout<<a<<" ";cout<<endl;
#define ppd(v)      for(auto a:v)cout<<a.ff<<" "<<a.ss<<endl;
#define sz(v)       (int)(v.size())
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define qqq 100005
v2d v(qqq);
v1d c(qqq),pos(qqq),neg(qqq);
int ans=0;
void fun(int x,int p){
    int mx=0,mn=0;

    for(auto a:v[x]){
        if(a!=p){
            fun(a,x);
            pos[x]=max(pos[x],pos[a]);
            neg[x]=max(neg[x],neg[a]);
        }
    }
    c[x]+=neg[x]-pos[x];
    if(c[x]>0)pos[x]+=c[x];
    else neg[x]+=abs(c[x]);

}
void Weapons19(){
    int n;cin>>n;
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=1;i<=n;i++){cin>>c[i];}
    fun(1,0);
    cout<<pos[1]+neg[1];

}

int32_t main(){

    /*
    त्यक्तवाधर्मं च लोभं च
    मोहंचो द्यमास्थिता।
    युद्ध्यध्वमनहंकारा
    यतो धर्मस्ततो जयः।।
    */

    cout<<fixed<<setprecision(16);
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    #ifndef ONLINE_JUDGE
     freopen("input.txt",  "r",  stdin);
     freopen("output.txt", "w", stdout);
    #endif

    Weapons19();

}


#include <bits/stdc++.h>
using namespace std;
#define sc(x)            scanf("%d",&x)
#define scc(x)           scanf("%c",&x)
#define scl(x)           scanf("%lld",&x)
#define sz(v)	     	(v.size())
#define mem(v, d)		memset(v, d, sizeof(v))
#define oo				2000000100
#define OO				4000000000000000100
#define PI 3.14159265
#define S second
#define F first
#define Ceil(x,y) ((x+y-1)/y)
#define EPS 1e-10
typedef unsigned int uint;
typedef long long ll;

int dX[] = {0,0,1,-1};
int dY[] = {1,-1,0,0};
string alpha = "abcdefghijklmnopqrstuvwxyz";
int MOD = 1000000007;
const int N = 100100;

//-----------------------------------------------------
//ll GCD(ll num,ll ones) { return !ones ? num : GCD(ones,num%ones); }
//ll lcm(ll a, ll b) { return (a / GCD(a, b)) * b; }
//double Len(ll x,ll y,ll X,ll Y) { return sqrt((X-x)*(X-x)+(Y-y)*(Y-y)); }
/*ll fastP(ll b,ll p)
{
    if(p==0) return 1;
    else if(p==1) return b;
    ll a = fastP(b,p/2)%MOD;
    if(p%2) return (((a*a)%MOD)*b)%MOD;
    else return (a*a)%MOD;
}*/
//-----------------------------------
int n;
ll lazy[N],we[N],ans;
vector<int>adj[N];
pair<ll,ll> dfs(int cur,int par = 0)
{
    ll mn = 0,mx = 0;
    for(auto x:adj[cur])
        if(x!=par){
            pair<ll,ll>tmp = dfs(x,cur);
            mn = min(mn,tmp.first);
            mx = max(mx,tmp.second);
        }
    we[cur] -= (mx+mn);
    if(we[cur] > 0) mx+=we[cur];
    else mn+=we[cur];

    return {mn,mx};
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("num.txt","w",stdout);
    sc(n);
    for(int i = 1;i<n;++i)
    {
        int x,y;
        sc(x); sc(y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    for(int i = 1;i<=n;++i)
        scl(we[i]);

    pair<ll,ll>ans = dfs(1);
    cout << ans.second - ans.first << endl;

    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

int u[100001];
vector <int> g[100001];
int a[100001];
int add[100001];
int del[100001];

void dfs (int v) {
	u[v] = 1;
	int add1 = 0;
	int del1 = 0;
	for (int i = 0;i < g[v].size ();i ++) {
		if (u[g[v][i]]) continue;
		dfs (g[v][i]);
		add1 = max (add1, add[g[v][i]]);
		del1 = max (del1, del[g[v][i]]);
	}
	a[v] += add1 - del1;
	del[v] = del1;
	add[v] = add1;
	if (a[v] < 0) {
		add[v] += abs (a[v]);
	}
	else del[v] += a[v];
}

 main () {
	int n;
	cin >> n;
	for (int i = 1;i < n;i ++) {
		int x , y;
		cin >> x >> y ;
		g[x].push_back (y);
		g[y].push_back (x);
	}
	for (int i = 1;i <= n;i ++) {
		cin >> a[i];
	}
	dfs (1);
	cout << add[1] + del[1];
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false)
#define mod 1000000007
#define mod1 mod
#define mod2 100000009
#define li long long int
#define ll long long int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000000000100
#define MINN -10000000000000
#define pii pair<ll,ll>
#define pic pair<int,char>
#define N 100010
#define N2 5010
#define lgn 20
#define ddouble long double
#define minus minu
#define INTMAX 10000000
#define si short int
using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<pair<ll,pii> , vector<pair<ll , pii> > > max_pq;
typedef priority_queue<pii , vector<pii > ,greater<pii > > min_pq;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST;
vector<list<ll> > adjlist(N),level(N);
bool visited[N];
ll lma[N],lmi[N],ar[N];
ll ans=0;
ll n;
void dfs( ll i )
{
	ll ma= -MAXN ,mi = MAXN;
	for ( list<ll> ::iterator it = adjlist[i].begin() ; it != adjlist[i].end() ;it++)
	{
		if ( !visited[*it])
		{
			visited[*it]=1;
			dfs(*it);
			ma=max( ma , lma[*it] );
			mi=min( mi , lmi[*it] );
		}
	}
	ll val = ar[i];
	if ( ma > 0 )
		val-=ma;
	if ( mi < 0 )
		val-=mi;
	if ( val > 0 )
	{
		if ( ma > 0)
			lma[i] = ma + val;
		else
			lma[i] = val;

	}
	else
	{
		if ( ma > 0)
			lma[i] = ma;
		else
			lma[i] = 0;
	}
	if ( val < 0 )
	{
		if ( mi < 0)
			lmi[i] = mi + val;
		else
			lmi[i] = val;

	}
	else
	{
		if ( mi < 0)
			lmi[i] = mi;
		else
			lmi[i] = 0;
	}

}
int main()
{

	ios::sync_with_stdio(false);
	cin >> n;

	for ( ll i=1 ;i<=n-1 ;i++)
	{
		ll a,b;
		cin>>a>>b;
		adjlist[a].pb(b);
		adjlist[b].pb(a);
	}

	for ( ll i=1 ;i<=n; i++)
	{
		cin>>ar[i];
	}

	visited[1]=1;
	dfs(1);
	// cout << ar[1] << lma[1] << lmi[1] <<endl;
	cout <<  lma[1] - lmi[1] ;

}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <map>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define pb push_back
#define mod 1000000007
#define MAX 100005
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
vector <int> node[MAX];
LL val[MAX];
bool check[MAX];
LL cnt;
void sol(int v, LL n){
    int l = node[v].size();
    cnt += abs(val[v]);
    //printf("%d %lld %lld\n", v, n, cnt);
    rep(i, l){
        val[ node[v][i] ] -= (-n+val[v]);
        sol(node[v][i], n - val[v]);
    }
    return ;
}
int main(void){
    int n; scanf("%d", &n);
    rep(i, n-1){
        int v1, v2; scanf("%d %d", &v1, &v2);
        check[v2] = 1;
        node[v1].pb(v2);
    }
    int s;
    rep1(i, 1, n+1) if(!check[i]){ s = i; break; }
    rep1(i, 1, n+1) scanf("%lld", val + i);
    //build

    sol(s, 0);
    cout << cnt << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
const ll mod=1e9+7;
vector<int> e[N],tmp,t;
ll n,a[N],deg[N],fa[N];
ll sum[N];
ll dp[N][2];
void dfs(int u,int par)
{
	dp[u][1]=dp[u][0]=0;
	for(int i=0;i<e[u].size();i++)
	{
		int v=e[u][i];
		if(v==par)	continue;
		dfs(v,u);
		dp[u][1]=max(dp[u][1],dp[v][1]);
		dp[u][0]=max(dp[u][0],dp[v][0]);
	}
	a[u]=a[u]+dp[u][0]-dp[u][1];
	if(a[u]>0)
		dp[u][1]+=a[u];
	else
		dp[u][0]+=abs(a[u]);

}
int main()
{
	while(cin>>n)
	{
		int u,v;
		memset(deg,0,sizeof(deg));
		for(int i=1;i<=n-1;i++)
		{
			scanf("%d%d",&u,&v);
			e[u].push_back(v);
			e[v].push_back(u);
			deg[u]++,deg[v]++;
		}
		for(int i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		dfs(1,0);
		cout<<dp[1][0]+dp[1][1]<<endl;
		for(int i=1;i<=n;i++)
			e[i].clear();
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <map>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define pb push_back
#define mod 1000000007
#define MAX 100005
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
vector <int> node[MAX];
LL v[MAX];
LL dp[MAX][2];
void dfs(int v1, int v2){
    int l = node[v1].size();
    rep(i, l){
        if(node[v1][i] == v2) continue;
        int tv = node[v1][i];
        dfs(tv, v1);
        dp[v1][0] = max(dp[tv][0], dp[v1][0]);
        dp[v1][1] = max(dp[tv][1], dp[v1][1]);
    }
    v[v1] += dp[v1][0] - dp[v1][1];
    if(v[v1] > 0) dp[v1][1] += v[v1];
    else dp[v1][0] -= v[v1];
    return;
}
int main(void){
    int n; scanf("%d", &n);
    rep(i, n-1){
        int v1, v2; scanf("%d %d", &v1, &v2);
        node[v1].pb(v2);
        node[v2].pb(v1);
    }
    rep1(i, 1, n+1) scanf("%lld", v + i);
    dfs(1, 0);
    printf("%lld", dp[1][0] + dp[1][1]);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <map>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define pb push_back
#define mod 1000000007
#define MAX 100005
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
vector <int> node[MAX];
LL val[MAX];
bool check[MAX];
LL cnt;
void sol(int v, LL n){
    int l = node[v].size();
    cnt += abs(val[v]);
    //printf("%d %lld %lld\n", v, n, cnt);
    rep(i, l){
        val[ node[v][i] ] -= (n+val[v]);
        sol(node[v][i], n - val[v]);
    }
    return ;
}
int main(void){
    int n; scanf("%d", &n);
    rep(i, n-1){
        int v1, v2; scanf("%d %d", &v1, &v2);
        check[v2] = 1;
        node[v1].pb(v2);
    }
    int s;
    rep1(i, 1, n+1) if(!check[i]){ s = i; break; }
    rep1(i, 1, n+1) scanf("%lld", val + i);
    //build

    sol(s, 0);
    cout << cnt << endl;
    return 0;
}


#include<stdio.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<ctime>
#include<complex>
#include<list>
using namespace std;

int n;
long long a[100005],b[100005],c[100005];
vector<int> nei[100005];

void dfs(int x,int from){
	for(int i=0;i<nei[x].size();i++){
		int to=nei[x][i];
		if(to!=from){
			dfs(to,x);
			a[x]=max(a[x],a[to]);
			b[x]=max(b[x],b[to]);
		}
	}
	c[x]+=a[x];
	c[x]-=b[x];
	if(c[x]<0) a[x]-=c[x];else b[x]+=c[x];
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		x--;y--;
		nei[x].push_back(y);
		nei[y].push_back(x);
	}
	for(int i=0;i<n;i++) scanf("%I64d",c+i);
	dfs(0,-1);
	cout<<a[0]+b[0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define N 100005

vector<int>G[N];
lli v[N],op[N],mx[N],mn[N], ans;
bool vis[N];

void dfs(int x){

    vis[x] = true;

    for(auto u : G[x]){

        if(vis[u]) continue;

        dfs(u);

        if(op[u]>=0) mx[x] = max(mx[x],op[u]);
        else mn[x] = min(mn[x],op[u]);
    }

    op[x] = (v[x]+mn[x]+mx[x])*(-1);
    ans += abs(op[x]);
    op[x] += mx[x]+mn[x];
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

     ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;

    for(int i=1; i<n; i++){
        int x,y;
        cin>>x>>y;
        G[x].pb(y);
        G[y].pb(x);
    }

    for(int i=1; i<=n; i++) cin>>v[i];

    dfs(1);

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define N 100005

vector<int>G[N];
lli v[N],mx[N],mn[N], ans;
bool vis[N];

void dfs(int x){

    vis[x] = true;

    for(auto u : G[x]){

        if(vis[u]) continue;

        dfs(u);

        mx[x] = max(mx[x],mx[u]);
        mn[x] = min(mn[x],mn[u]);
    }

    lli temp = -(v[x]+mn[x]+mx[x]);
    if(temp>=0) mx[x] += temp;
    else mn[x] += temp;
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

     ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;

    for(int i=1; i<n; i++){
        int x,y;
        cin>>x>>y;
        G[x].pb(y);
        G[y].pb(x);
    }

    for(int i=1; i<=n; i++) cin>>v[i];

    dfs(1);
    ans = mx[1]-mn[1];
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define N 100005

vector<int>G[N];
lli v[N],op[N],mx[N],mn[N], ans;
bool vis[N];

void dfs(int x){

    vis[x] = true;

    for(auto u : G[x]){

        if(vis[u]) continue;

        dfs(u);

        if(op[u]>=0) mx[x] = max(mx[x],op[u]);
        else mn[x] = min(mn[x],op[u]);
    }

    op[x] = mx[x] + mn[x] + (v[x]+mn[x]+mx[x])*(-1);
    ans += abs(op[x]);
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

     ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;

    for(int i=1; i<n; i++){
        int x,y;
        cin>>x>>y;
        G[x].pb(y);
        G[y].pb(x);
    }

    for(int i=1; i<=n; i++) cin>>v[i];

    dfs(1);

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define N 100005

vector<int>G[N];
lli v[N],op[N],mx[N],mn[N], ans;
bool vis[N];

void dfs(int x){

    vis[x] = true;

    for(auto u : G[x]){

        if(vis[u]) continue;

        dfs(u);

        if(op[u]>=0) mx[x] = max(mx[x],op[u]);
        else mn[x] = min(mn[x],op[u]);
    }

    op[x] = -(v[x]+mn[x]+mx[x]) + mx[x]+mn[x];
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

     ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;

    for(int i=1; i<n; i++){
        int x,y;
        cin>>x>>y;
        G[x].pb(y);
        G[y].pb(x);
    }

    for(int i=1; i<=n; i++) cin>>v[i];

    dfs(1);
    ans = mx[1] - mn[1] + abs(v[1]+mn[1]+mx[1]);
    cout<<ans;

    return 0;
}


#include <iostream>
#include <vector>
#include <stack>

using namespace std;

typedef long long ll;

void dfs(int akt, vector< vector<int> >& T, vector<int>& ojciec, vector< vector<ll> >& dp, vector<ll>& v)
{
    for (int i = 0; i < T[akt].size(); ++i)
        if (T[akt][i] != ojciec[akt])
            dfs(T[akt][i], T, ojciec, dp, v);
    if (T[akt].size() == 1 && akt != 0)
    {
        if (v[akt] < 0)
            dp[1][akt] = -v[akt];
        else
            dp[0][akt] = v[akt];
    }
    else
    {
        dp[0][akt] = 0;
        dp[1][akt] = 0;
        for (int i = 0; i < T[akt].size(); ++i)
        {
            if (T[akt][i] != ojciec[akt])
            {
                dp[0][akt] = max(dp[0][akt], dp[0][T[akt][i]]);
                dp[1][akt] = max(dp[1][akt], dp[1][T[akt][i]]);
            }
        }
        v[akt] -= dp[0][akt];
        v[akt] += dp[1][akt];
        if (v[akt] < 0)
            dp[1][akt] += -v[akt];
        else
            dp[0][akt] += v[akt];
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector< vector<int> > T(n);
    for (int i = 0; i < n - 1; ++i)
    {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        T[a].push_back(b);
        T[b].push_back(a);
    }
    vector<ll> v(n);
    for (int i = 0; i < n; ++i)
        cin >> v[i];
    vector<int> ojciec(n);
    stack<int> S;
    vector<bool> odw(n, false);
    S.push(0);
    odw[0] = true;
    ojciec[0] = -1;
    while (!S.empty())
    {
        int akt = S.top();
        S.pop();
        for (int i = 0; i < T[akt].size(); ++i)
        {
            if (!odw[T[akt][i]])
            {
                odw[T[akt][i]] = true;
                S.push(T[akt][i]);
                ojciec[T[akt][i]] = akt;
            }
        }
    }
    vector<vector<ll> > dp(2, vector<ll>(n));
    dfs(0, T, ojciec, dp, v);
    cout << dp[0][0] + dp[1][0];
    return 0;
}


/*input
3
1 2
1 3
1 -1 1
*/
// Name = Eshan Balachandar
// Handle = MisterMediocre
// Problem Statement = "http://codeforces.com/problemset/problem/275/D"

#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define trace1(x) cout<<#x<<": "<<x<<endl
#define trace2(x, y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cout<<#x<<": " <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl

typedef long long ll;

#define PI 3.141592653589793
#define MOD 1000000007
#define INF 1e15
template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; a=a%m; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }
/*prime m*/ template<typename T> T modInv(T a, T m){return pow(a,m-2,m);}

#define MAX 100005
ll n;
vector<ll> adjList[MAX];
ll v[MAX];

bool visited[MAX];
pair<ll,ll> dfs(ll node){
	visited[node]=true;
	ll up=0,down=0;
	for(ll i=0; i<adjList[node].size(); i++){
		ll child = adjList[node][i];
		if(visited[child]) continue;
		pair<ll,ll> data = dfs(child);
		down = max(down, data.second);
		up = max(up, data.first);
	}
	ll val = v[node];
	val = val + up - down;
	if(val>0) down+=val; else up-=val;
	return make_pair(up,down);
}

int main() {
	IOS
 	cin>>n;
 	for(int i=1; i<n; i++){
 		ll u,v; cin>>u>>v;
 		adjList[u].push_back(v);
 		adjList[v].push_back(u);
 	}
 	for(int i=1; i<=n; i++) cin>>v[i], visited[i]=false;

	pair<ll,ll> out= dfs(1);
	//trace2(out.first,out.second);
	cout<<out.first+out.second<<endl;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

vector<vector<int>> tree;
vector<int> value , decr , incr;

void dfs(int curr = 0, int parent = -1){
    int increase = 0 , decrease = 0;
    for(const int &i : tree[curr]){
        if(i != parent){
            dfs(i,curr);
            increase = max(incr[i],increase);
            decrease = max(decr[i],decrease);
        }
    }

    int remaining = value[curr] + increase - decrease;
    if(remaining >=0)  decrease += remaining;
    else increase += abs(remaining);
    decr[curr] = decrease;
    incr[curr] = increase;
}

int32_t main(){

    int n;cin>>n;

    tree.resize(n); value.resize(n); decr.resize(n); incr.resize(n);

    for(int i=0; i<n-1; i++){
        int u,v;cin>>u>>v;
        u--;v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }

    for(int &i : value) cin>>i;

    dfs();

    cout<< incr[0] + decr[0] <<"\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N ((ll)101*1000)

ll n,c[N],dp[2][N];
vector <ll> e[N];

void dfs(ll x,ll par=0)
{
	for(auto v:e[x])
		if(v!=par)
			dfs(v,x),
			dp[0][x]=max(dp[0][x],dp[0][v]),
			dp[1][x]=max(dp[1][x],dp[1][v]);
	ll p=dp[1][x]-dp[0][x]+c[x];
	dp[1][x]+=max(0ll,-p);
	dp[0][x]+=max(0ll,p);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=0;i<n-1;i++)
	{
		ll v,u;
		cin>>v>>u;
		e[v].push_back(u);
		e[u].push_back(v);
	}
	for(int i=1;i<=n;i++)cin>>c[i];
	dfs(1);
	cout<<dp[0][1]+dp[1][1];
	return 0;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define ld long double
// #define int long long
#define F first
#define S second
#define pb push_back
using namespace std;
void null(){return ;}
// const int INF=3e18+9237;
const int maxn=1e5+7;
const int mod=1e9+7;
const int sqN=320;
const int lgN=20;
int num[maxn];
long long inc[maxn],red[maxn];
vector<int>g[maxn];

void dfs(int v,int p){
	for(auto u:g[v])
		if(u!=p)dfs(u,v),inc[v]=max(inc[v],inc[u]),red[v]=max(red[v],red[u]);
	long long tmp=num[v]+inc[v]-red[v];
	if(tmp<0)inc[v]-=tmp;
	else red[v]+=tmp;
}

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	int start;
	for(int i=1,u,v;i<n;i++)
		cin>>u>>v,g[v].pb(u),g[u].pb(v),start=u;
	for(int i=1;i<=n;i++)cin>>num[i];
	// cout<<centroid(start,-1)<<" ----- "<<endl;
	dfs(1,-1);
	cout<<inc[1]+red[1];
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define ld long double
// #define int long long
#define F first
#define S second
#define pb push_back
using namespace std;
void null(){return ;}
const int INF=3e18+9237;
const int maxn=1e5+7;
const int mod=1e9+7;
const int sqN=320;
const int lgN=20;
int num[maxn],inc[maxn],red[maxn];
vector<int>g[maxn];

void dfs(int v,int p){
	for(auto u:g[v])
		if(u!=p)dfs(u,v),inc[v]=max(inc[v],inc[u]),red[v]=max(red[v],red[u]);
	int tmp=num[v]+inc[v]-red[v];
	if(tmp<0)inc[v]-=tmp;
	else red[v]+=tmp;
}

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	int start;
	for(int i=1,u,v;i<n;i++)
		cin>>u>>v,g[v].pb(u),g[u].pb(v),start=u;
	for(int i=1;i<=n;i++)cin>>num[i];
	// cout<<centroid(start,-1)<<" ----- "<<endl;
	dfs(1,-1);
	cout<<inc[1]+red[1];
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=1e5+7;
int num[maxn];
long long inc[maxn],red[maxn];
vector<int>g[maxn];

void dfs(int v,int p){
	for(auto u:g[v])
		if(u!=p)dfs(u,v),inc[v]=max(inc[v],inc[u]),red[v]=max(red[v],red[u]);
	long long tmp=num[v]+inc[v]-red[v];
	if(tmp<0)inc[v]-=tmp;
	else red[v]+=tmp;
}

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	for(int i=1,u,v;i<n;i++)
		cin>>u>>v,g[v].pb(u),g[u].pb(v);
	for(int i=1;i<=n;i++)cin>>num[i];
	dfs(1,-1);
	cout<<inc[1]+red[1];
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

#include <bits/stdc++.h>
using namespace std;

//Optimizations
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//save time
#define endl '\n'
#define db(x) cout << "> " << #x << ": " << x << endl;
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007LL
#define EPS  0.000000001
#define INF  0X3f3f3f3f

//loops
#define REP(i,n) 	    for(ll i=0;i<(n);++i)
#define FOR(i,a,b)      for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b)     for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL);cout.tie(NULL)

//queue
#define di deque<int>
#define dll deque<ll>
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Declare all variables and methods needed between this comment and the next one(OCD lol)
vi adj[100010];
ll dpInc[100010],dpDec[100010];
ll arr[100010];
void dfs(int cur,int par){
    for(int ch : adj[cur])
        if(ch!=par){
            dfs(ch,cur);
            dpInc[cur]=max(dpInc[cur],dpInc[ch]);
            dpDec[cur]=max(dpDec[cur],dpDec[ch]);
        }

    if(arr[cur]+dpInc[cur]-dpDec[cur]>0)
        dpDec[cur]+=(arr[cur]+dpInc[cur]-dpDec[cur]);
    else
        dpInc[cur]-=(arr[cur]+dpInc[cur]-dpDec[cur]);
}

//Main function
int main(){
    IOS;
    TIE;

    int n;
    cin>>n;

    REP(i,n-1){
        int v1,v2;
        cin>>v1>>v2;
        adj[v1].pb(v2);
        adj[v2].pb(v1);
    }

    FOR(i,1,n+1)
        cin>>arr[i];

    dfs(1,0);

    cout<<dpInc[1]+dpDec[1]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;

LL n,x,y,val[100100],a[100100],b[100100];
vector<LL> graph[100100];

void dfs(LL u,LL p){
	LL mx=-1e18,mn=-1e18;
	for(int i=0;i<graph[u].size();i++){
		LL v = graph[u][i];
		if(v==p) continue;
		dfs(v,u);
		mx=max(mx,a[v]);
		mn=max(mn,b[v]);
	}
	if(mx==-1e18) mx=0;
	if(mn==-1e18) mn=0;
	a[u]=mx;
	b[u]=mn;
	val[u]+=(a[u]-b[u]);
	if(val[u]>0) b[u]+=val[u];
	else a[u]+=val[u]*-1;
}

int main() {
	scanf("%lld",&n);
	for(int i=0;i<n-1;i++){
		scanf("%lld %lld",&x,&y);
		graph[x].pb(y);
		graph[y].pb(x);
	}
	for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
	dfs(1,0);
// 	for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
// 	for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl;
// 	for(int i=1;i<=n;i++) cout<<val[i]<<" ";cout<<endl;
	printf("%lld\n",a[1]+b[1]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct _{_(){ ios_base::sync_with_stdio(0); cin.tie(0); }}_;

const int base = 311, mod = 1e9 + 7, N = 1e6 + 11, inf = INT_MAX;

int n, a[N], dp[N][2];
vector <int> adj[N];

void dfs(int v, int p) {
	int max0 = 0, max1 = 0;
	for(auto u : adj[v]) {
		if(u == p) {
			continue;
		}
		dfs(u, v);
		max0 = max(max0, dp[u][0]);
		max1 = max(max1, dp[u][1]);
	}
	dp[v][0] += max0;
	dp[v][1] += max1;
	a[v] += max1 - max0;
	if(a[v] > 0) {
		dp[v][0] += a[v];
	} else {
		dp[v][1] -= a[v];
	}
}

signed main() {
	cin >> n;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(1, 0);
	cout << dp[1][0] + dp[1][1];
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 111111;
vector<int> adj[N];
int dp[N][2]; // dp[i][1] = min increase operation needed for node i
int val[N];  // dp[i][0] = min decrease operation needed for node i

void dfs(int node, int par){

    for(int child : adj[node]){
        if(child == par)
            continue;

        dfs(child, node);

        dp[node][1] = max(dp[node][1], dp[child][1]);
        dp[node][0] = max(dp[node][0], dp[child][0]);
    }

    val[node] = val[node] + dp[node][1] - dp[node][0];

    dp[node][1] += max(0LL, -val[node]);
    dp[node][0] += max(0LL, val[node]);

}
int32_t main()
{
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  #define int long long
  int n;
  cin >> n;

  for(int i = 1; i < n; i++){
     int u, v;
     cin >> u >> v;
     adj[u].push_back(v);
     adj[v].push_back(u);
  }

  for(int i = 1; i <= n; i++)
     cin >> val[i];

  dfs(1, 0);

  cout << dp[1][0] + dp[1][1];

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;

vector< vector<int> > childs;
vector<ll> w;

ii get(int from, int root) {
    // cout << from << ' ' << root << endl;
    vector< ii > child_results;
    for (int i = 0; i < childs[root].size(); ++i) {
        int child = childs[root][i];
        if (child != from) {
            child_results.push_back(get(root, child));
        }
    }
    if (child_results.empty()) {
        ii res(0, 0);
        if (w[root] < 0) {
            res.first = -w[root];
        } else {
            res.second = w[root];
        }
        return res;
    } else {
        ii res(0, 0);
        for (int i = 0; i < child_results.size(); ++i) {
            ii& t = child_results[i];
            res.first = max(res.first, t.first);
            res.second = max(res.second, t.second);
        }
        int off = w[root] + res.first - res.second;
        if (off < 0) {
            res.first += -off;
        } else {
            res.second += off;
        }
        return res;
    }
}

int main() {
    int n;
    cin >> n;

    childs.resize(n);
    w.resize(n);

    for (int i = 0; i + 1 < n; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        childs[a].push_back(b);
        childs[b].push_back(a);
    }

    for (int i = 0; i < n; ++i) {
        cin >> w[i];
    }

    ii res = get(-1, 0);
    cout << res.first + res.second << endl;

    return 0;
}