#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
ll ans = 0;
vb vis;

int solve(int root){
	if(root!=0 && graph[root].size()==1){
		ans += abs(v[root]);
		return v[root];
	}
	vis[root] = true;
	int maxv=0,minv = 0;
	for(int i=0;i<graph[root].size();i++){
		int x = graph[root][i];
		if(!vis[x]){
			int k = solve(x);
			maxv = max(maxv,k);
			minv = min(minv,k);
		}
	}
	int val = maxv-minv;
	ans += abs(v[root] - val);
	return v[root];
}

int main(){
	int n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(int i=0;i<n;i++) cin>>v[i];
	solve(0);
	cout<<ans<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
vb vis;

ii solve(ll root){
	vis[root] = true;

	ll inc = 0,dec = 0;
	for(ll i=0;i<graph[root].size();i++){
		ll x = graph[root][i];
		if(!vis[x]){
			ii k = solve(x);
			inc = max(inc,k.first);
			dec = min(dec,k.second);
		}
	}

	ll k = v[root] + inc - dec;
	if(k<=0) inc += (-k);
	else dec += k;

	return make_pair(inc,dec);
}

int main(){
	ll n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(ll i=0;i<n;i++) cin>>v[i];
	ii k = solve(0);
	cout<<k.first+k.second<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
ll ans = 0;
vb vis;

int solve(int root){
	if(root!=0 && graph[root].size()==1){
		ans += abs(v[root]);
		return v[root];
	}
	vis[root] = true;
	int maxv=0,minv = 0;
	for(int i=0;i<graph[root].size();i++){
		int x = graph[root][i];
		if(!vis[x]){
			int k = solve(x);
			maxv = max(maxv,k);
			minv = min(minv,k);
		}
	}
	int val = maxv+minv;
	ans += abs(v[root] - val);
	return v[root];
}

int main(){
	int n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(int i=0;i<n;i++) cin>>v[i];
	solve(0);
	cout<<ans<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
ll ans = 0;
vb vis;

int solve(int root){
	if(root!=0 && graph[root].size()==1){
		ans += v[root];
		return v[root];
	}
	vis[root] = true;
	int maxv=0,minv = 0;
	for(int i=0;i<graph[root].size();i++){
		int x = graph[root][i];
		if(!vis[x]){
			int k = solve(x);
			maxv = max(maxv,k);
			minv = min(minv,k);
		}
	}
	int val = maxv-minv;
	ans += abs(v[root] - val);
	return v[root];
}

int main(){
	int n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(int i=0;i<n;i++) cin>>v[i];
	solve(0);
	cout<<ans<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
vb vis;

ii solve(ll root){
	vis[root] = true;

	ll inc = 0,dec = 0;
	for(ll i=0;i<graph[root].size();i++){
		ll x = graph[root][i];
		if(!vis[x]){
			ii k = solve(x);
			inc = max(inc,k.first);
			dec = max(dec,k.second);
		}
	}

	ll k = v[root] + inc - dec;
	if(k<=0) inc += (-k);
	else dec += k;
	return make_pair(inc,dec);
}

int main(){
	ll n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(ll i=0;i<n;i++) cin>>v[i];
	ii k = solve(0);
	cout<<k.first+k.second<<endl;

}


#include<bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back

const int N = 100005;
int a[N], res[N];
P dp[N];
vector<int> gr[N];

void dfs(int cur, int par) {
	dp[cur] = {a[cur], a[cur]}; // minimum negative value
	//and maximum positive value in subtree
	for (auto x : gr[cur]) {
		if (x != par) dfs(x, cur);
		dp[cur].F = min(dp[cur].F, dp[x].F);
		dp[cur].S = max(dp[cur].S, dp[x].S);
	}
	res[cur] = dp[cur].S - dp[cur].F + abs(a[cur] - (dp[cur].S - dp[cur].F));
}

int32_t main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	// int t;cin>>t;while(t--)
	{
		int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
		cin >> n;
		for (i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			gr[x].pb(y);
			gr[y].pb(x);
		}
		for (i = 0; i < n; i++) {
			cin >> a[i + 1];
		}
		dfs(1, 0);
		cout << res[1];
	}
}


#include<bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back

const int N = 100005;
int a[N], res[N];
P dp[N];
vector<int> gr[N];

void dfs(int cur, int par) {
	dp[cur] = {0, 0}; // minimum negative value
	//and maximum positive value in subtree
	for (auto x : gr[cur]) {
		if (x != par) dfs(x, cur);
		dp[cur].F = min(dp[cur].F, dp[x].F);
		dp[cur].S = max(dp[cur].S, dp[x].S);
	}
	if (dp[cur].F + dp[cur].S > a[cur]) {
		dp[cur].F -= abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
	if (dp[cur].F + dp[cur].S < a[cur]) {
		dp[cur].S += abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
}

int32_t main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	// int t;cin>>t;while(t--)
	{
		int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
		cin >> n;
		for (i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			gr[x].pb(y);
			gr[y].pb(x);
		}
		for (i = 0; i < n; i++) {
			cin >> a[i + 1];
		}
		dfs(1, 0);
		cout << dp[1].S - dp[1].F;
	}
}


#include<bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back

const int N = 100005;
int a[N], res[N];
P dp[N];
vector<int> gr[N];

void dfs(int cur, int par) {
	dp[cur] = {0, 0}; // minimum negative value
	//and maximum positive value in subtree
	for (auto x : gr[cur]) {
		if (x != par) {
			dfs(x, cur);
			dp[cur].F = min(dp[cur].F, dp[x].F);
			dp[cur].S = max(dp[cur].S, dp[x].S);
		}
	}
	if (dp[cur].F + dp[cur].S > a[cur]) {
		dp[cur].F -= abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
	else if (dp[cur].F + dp[cur].S < a[cur]) {
		dp[cur].S += abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
}

int32_t main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	// int t;cin>>t;while(t--)
	{
		int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
		cin >> n;
		for (i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			gr[x].pb(y);
			gr[y].pb(x);
		}
		for (i = 0; i < n; i++) {
			cin >> a[i + 1];
		}
		dfs(1, 0);
		cout << dp[1].S - dp[1].F;
	}
}


#include<bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back

const int N = 100005;
int a[N], res[N];
P dp[N];
vector<int> gr[N];

void dfs(int cur, int par) {
	dp[cur] = {0, 0}; // minimum negative value
	//and maximum positive value in subtree
	for (auto x : gr[cur]) {
		if (x != par) dfs(x, cur);
		dp[cur].F = min(dp[cur].F, dp[x].F);
		dp[cur].S = max(dp[cur].S, dp[x].S);
	}
	// int val = dp[cur].F + dp[cur].S + a[cur];
	if (dp[cur].F + dp[cur].S > a[cur]) {
		dp[cur].F -= abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
	else if (dp[cur].F + dp[cur].S < a[cur]) {
		dp[cur].S += abs(a[cur] - (dp[cur].F + dp[cur].S));
	}
}

int32_t main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	// int t;cin>>t;while(t--)
	{
		int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
		cin >> n;
		for (i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			gr[x].pb(y);
			gr[y].pb(x);
		}
		for (i = 0; i < n; i++) {
			cin >> a[i + 1];
		}
		dfs(1, 0);
		cout << dp[1].S - dp[1].F;
	}
}


#include<bits/stdc++.h>

using namespace std;

#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back

const int N = 100005;
int a[N], res[N];
P dp[N];
vector<int> gr[N];

void dfs(int cur, int par) {
	dp[cur] = {0, 0}; // minimum negative value
	//and maximum positive value in subtree
	for (auto x : gr[cur]) {
		if (x != par) dfs(x, cur);
		dp[cur].F = min(dp[cur].F, dp[x].F);
		dp[cur].S = max(dp[cur].S, dp[x].S);
	}
	int val = dp[cur].F + dp[cur].S + a[cur];
	if (val > 0) dp[cur].F -= abs(val);
	if (val < 0) dp[cur].S += abs(val);
}

int32_t main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	// int t;cin>>t;while(t--)
	{
		int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
		cin >> n;
		for (i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			gr[x].pb(y);
			gr[y].pb(x);
		}
		for (i = 0; i < n; i++) {
			cin >> a[i + 1];
		}
		dfs(1, 0);
		cout << dp[1].S - dp[1].F;
	}
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <bits/stdc++.h>
#include <string.h>
#include <queue>
#include <map>
#define ll long long int
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
const ll inf = (ll)10e16;
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define d(x) cout<<x<<endl;
#define fni(x,y,z) for(int i=x;i>=y;i-=z)
#define fnj(x,y,z) for(int j=x;j>=y;j-=z)
#define fpi(x,y,z) for(int i=x;i<=y;i+=z)
#define fpj(x,y,z) for(int j=x;j<=y;j+=z)
#define fpk(x,y,z) for(int k=x;k<=y;k+=z)
#define fnk(x,y,z) for(int k=x;k>=y;j-=z)
#define pb push_back
#define mp make_pair
#define mod 1000000007
std::vector<int> v[100009];
ll dp[100009],ans=0,q[100009];
void dfs(int node,int p)
{
    int u,i;ll ma=0,mi=0;
    dp[node] = 0;
    for (i=0;i<v[node].size();i++)
    {
        u=v[node][i];
        if (u!=p)
        {
            dfs(u,node);
            dp[node] += dp[u];
            if(q[u]>=0) ma = max(ma,q[u]);
            else mi = min(mi,q[u]);
        }
    }
    dp[node] += -1*(ma+mi);
    q[node] += dp[node];
    ans += ma + (-1*mi);
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int a,b,n;
    cin>>n;
    fpi(1,n-1,1) {cin>>a>>b;v[a].pb(b);v[b].pb(a);}
    fpi(1,n-1,1) cin>>q[i];
    dfs(1,-1);
    if(q[1]>=0) cout<<ans+q[1]; else cout<<ans-q[1];
    #ifndef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <bits/stdc++.h>
#include <string.h>
#include <queue>
#include <map>
#define ll long long int
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
const ll inf = (ll)10e16;
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define d(x) cout<<x<<endl;
#define fni(x,y,z) for(int i=x;i>=y;i-=z)
#define fnj(x,y,z) for(int j=x;j>=y;j-=z)
#define fpi(x,y,z) for(int i=x;i<=y;i+=z)
#define fpj(x,y,z) for(int j=x;j<=y;j+=z)
#define fpk(x,y,z) for(int k=x;k<=y;k+=z)
#define fnk(x,y,z) for(int k=x;k>=y;j-=z)
#define pb push_back
#define mp make_pair
#define mod 1000000007
std::vector<int> v[100009];
ll dp1[100009],dp2[100009],ans=0,q[100009];
void dfs(int node,int p)
{
    int u,i;ll ma=0,mi=0;
    dp1[node] = 0;dp2[node] = 0;
    for (i=0;i<v[node].size();i++)
    {
        u=v[node][i];
        if (u!=p)
        {
            dfs(u,node);
            dp1[node] = max(dp1[u],dp1[node]);
            dp2[node] = max(dp2[u],dp2[node]);
        }
    }
    q[node] += (dp2[node]-dp1[node]);
    if(q[node]>=0) dp1[node]+=q[node]; else dp2[node]-=q[node];
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int a,b,n;
    cin>>n;
    fpi(1,n-1,1) {cin>>a>>b;v[a].pb(b);v[b].pb(a);}
    fpi(1,n,1) cin>>q[i];
    dfs(1,-1);
    cout<<(dp1[1]+dp2[1]);
    #ifndef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <bits/stdc++.h>
#include <string.h>
#include <queue>
#include <map>
#define ll long long int
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
const ll inf = (ll)10e16;
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define d(x) cout<<x<<endl;
#define fni(x,y,z) for(int i=x;i>=y;i-=z)
#define fnj(x,y,z) for(int j=x;j>=y;j-=z)
#define fpi(x,y,z) for(int i=x;i<=y;i+=z)
#define fpj(x,y,z) for(int j=x;j<=y;j+=z)
#define fpk(x,y,z) for(int k=x;k<=y;k+=z)
#define fnk(x,y,z) for(int k=x;k>=y;j-=z)
#define pb push_back
#define mp make_pair
#define mod 1000000007
std::vector<int> v[100009];
ll dp1[100009],dp2[100009],ans=0,q[100009];
void dfs(int node,int p)
{
    int u,i;ll ma=0,mi=0;
    dp1[node] = 0;dp2[node] = 0;
    for (i=0;i<v[node].size();i++)
    {
        u=v[node][i];
        if (u!=p)
        {
            dfs(u,node);
            dp1[node] = max(dp1[u],dp1[node]);
            dp2[node] = min(dp2[u],dp2[node]);
            ma = max(q[u],ma);
            mi = min(q[u],mi);
        }
    }
    dp1[node]-=mi;
    dp2[node]-=ma;
    q[node] += dp1[node] + dp2[node];
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int a,b,n;
    cin>>n;
    fpi(1,n-1,1) {cin>>a>>b;v[a].pb(b);v[b].pb(a);}
    fpi(1,n,1) cin>>q[i];
    dfs(1,-1);
    if(q[1]>=0) cout<<dp1[1]+-1*dp2[1]+q[1]; else cout<<dp1[1]+-1*dp2[1]-q[1];
    #ifndef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <bits/stdc++.h>
#include <string.h>
#include <queue>
#include <map>
#define ll long long int
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
const ll inf = (ll)10e16;
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define d(x) cout<<x<<endl;
#define fni(x,y,z) for(int i=x;i>=y;i-=z)
#define fnj(x,y,z) for(int j=x;j>=y;j-=z)
#define fpi(x,y,z) for(int i=x;i<=y;i+=z)
#define fpj(x,y,z) for(int j=x;j<=y;j+=z)
#define fpk(x,y,z) for(int k=x;k<=y;k+=z)
#define fnk(x,y,z) for(int k=x;k>=y;j-=z)
#define pb push_back
#define mp make_pair
#define mod 1000000007
std::vector<int> v[100009];
ll dp[100009],ans=0,q[100009];
void dfs(int node,int p)
{
    int u,i;ll ma=0,mi=0;
    dp[node] = 0;
    for (i=0;i<v[node].size();i++)
    {
        u=v[node][i];
        if (u!=p)
        {
            dfs(u,node);
            dp[node] += dp[u];
            if(q[u]>=0) ma = max(ma,q[u]);
            else mi = min(mi,q[u]);
        }
    }
    dp[node] += -1*(ma+mi);
    q[node] += dp[node];
    ans += (ma + (-1*mi));
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int a,b,n;
    cin>>n;
    fpi(1,n-1,1) {cin>>a>>b;v[a].pb(b);v[b].pb(a);}
    fpi(1,n,1) cin>>q[i];
    dfs(1,-1);
    if(q[1]>=0) cout<<ans+q[1]; else cout<<ans-q[1];
    #ifndef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


/*
    Mbak Rose Dong
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> vi[100100];
long long int arr[100100];
long long int rose;
void dfs(int now, int prev, long long int sum)
{
    arr[now] += sum;
    long long int diff = -arr[now];
    rose += abs(arr[now]);
    for(auto x : vi[now])
    {
        if(x == prev) continue;
        dfs(x, now, sum+diff);
    }
}
int main()
{
    int n,i,j,x,y,ans,curr,temp,m;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        vi[x].push_back(y);
        vi[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        cin >> arr[i];
    rose = 0;
    dfs(1, -1, 0LL);
    cout << rose << "\n";
    return 0;
}


/*
    Mbak Rose Dong
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> vi[100100];
long long int arr[100100];
long long int rose;
void dfs(int now, int prev, long long int sum)
{
    arr[now] += sum;
    long long int diff = -arr[now];
    rose += abs(arr[now]);
    for(auto x : vi[now])
    {
        if(x == prev) continue;
        dfs(x, now, sum+diff);
    }
}
int main()
{
    int n,i,j,x,y,ans,curr,temp,m;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        vi[x].push_back(y);
        vi[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        scanf("%llld",&arr[i]);
    rose = 0;
    dfs(1, -1, 0);
    printf("%lld\n",rose);
    return 0;
}


/*
    Mbak Rose Dong
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> vi[100100];
long long int arr[100100];
long long int rose;
pair<long long int, long long int> p;
pair<long long int, long long int> dfs(int now, int prev)
{
    long long int sum = 0, pos = 0, neg = 0;
    for(auto x : vi[now])
    {
        if(x == prev) continue;
        p = dfs(x, now);
        pos = max(pos, p.first);
        neg = min(neg, p.second);
    }
    arr[now] += pos+neg;
    long long int need = -arr[now];
    if(need > 0) pos += need;
    else neg += need;
    return make_pair(pos, neg);
}
int main()
{
    int n,i,j,x,y,ans,curr,temp,m;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        vi[x].push_back(y);
        vi[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        cin >> arr[i];
    rose = 0;
    p = dfs(1, -1);
    rose = p.first - p.second;
    cout << rose << "\n";
    return 0;
}


/*
    Mbak Rose Dong
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> vi[100100];
long long int arr[100100];
long long int rose;
void dfs(int now, int prev, long long int sum)
{
    arr[now] += sum;
    long long int diff = -arr[now];
    rose += abs(arr[now]);
    for(auto x : vi[now])
    {
        if(x == prev) continue;
        dfs(x, now, sum+diff);
    }
}
int main()
{
    int n,i,j,x,y,ans,curr,temp,m;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        vi[x].push_back(y);
        vi[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        scanf("%llld",&arr[i]);
    rose = 0;
    dfs(1, -1, 0LL);
    cout << rose << "\n";
    return 0;
}


/*
    Mbak Rose Dong
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> vi[100100];
long long int arr[100100];
long long int rose;
long long int dfs(int now, int prev)
{
    long long int sum = 0;
    for(auto x : vi[now])
    {
        if(x == prev) continue;
        sum += dfs(x, now);
    }
    arr[now] += sum;
    rose += abs(arr[now]);
    return -arr[now] + sum;
}
int main()
{
    int n,i,j,x,y,ans,curr,temp,m;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        vi[x].push_back(y);
        vi[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        cin >> arr[i];
    rose = 0;
    dfs(1, -1);
    cout << rose << "\n";
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
int Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[v]=max(Sub[v],Sub[u]);
        }
    }
    int tot=Cos[u]-Add[u]-Sub[u];
    tot >=0 ? Sub[u]+=abs(tot) : Add[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
int Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[u]=max(Sub[v],Sub[u]);
        }
    }
    int tot=Cos[u]+Add[u]-Sub[u];
    tot >=0 ? Sub[u]+=abs(tot) : Add[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[100010];
LL Cost[100010];
LL dp[100010],pos[100010],neg[100010];

void dfs(int u,int par){
    dp[u]=abs((LL)Cost[u]);
    LL A=0,B=0;
    for(auto child : G[u]){
        if(child==par)continue;
        dfs(child,u);
        if(Cost[child]>=0)A+=Cost[child];
        else B+=Cost[child];
    }
    dp[u]+=(abs(A)+abs(B));
}

int main(){
    int n;
    sI(n);
    for(int i=0; i<n-1; i++){
        int u,v;
        sI2(u,v);
        G[u].pb(v);
        G[v].pb(u);
    }
    for(int i=1; i<=n; i++)sL(Cost[i]);
    dfs(1,0);
    pL(dp[1]);
    pN;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
LL Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[u]=max(Sub[v],Sub[u]);
        }
    }
    LL tot=Cos[u]+Add[u]-Sub[u];
    tot >=0 ? Sub[u]+=abs(tot) : Add[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
LL check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
int Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[u]=max(Sub[v],Sub[u]);
        }
    }
    LL tot=Cos[u]+Add[u]-Sub[u];
    tot >=0 ? Sub[u]+=abs(tot) : Add[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
int Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[v]=max(Sub[v],Sub[u]);
        }
    }
    int tot=Cos[u]-Add[u]-Sub[u];
    tot >=0 ? Add[u]+=abs(tot) : Sub[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


/**Bismillah-ir-Rahman-ir-Rahim**/
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,n) for(LL i=0; i<n; i++)
#define RFOR(i,n) for(LL i=n-1; i>=0; i--)
#define FILL(A,value) memset(A,value,sizeof(A))
#define LL long long int
#define sI(a) scanf("%d",&a)
#define pI(a) printf("%d",a)
#define sI2(a,b) sI(a),sI(b)
#define pI2(a,b) pI(a) , pI(b)
#define sL(a) scanf("%lld",&a)
#define pL(a) printf("%lld",a)
#define sL2(a,b) sL(a),sL(b)
#define pL2(a,b) pL(a) , pL(b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pN printf("\n")
#define pS printf(" ");
#define MAX 2000100
#define MX 200001
#define PI 3.14159
#define pb push_back
#define MOD 1000000007
inline int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}
inline int lcm(int a, int b){return (a * (b / gcd(a, b))); }
inline LL bigmod(LL a,LL b,LL m){if(b==0)return 1%m;LL x=bigmod(a,b/2,m);x=(x*x)%m;if(b%2==1){x=(x*a)%m;}return x;}
inline LL phi(LL n) {LL ret=n;for(LL i=2; i*i<=n; i++){if(n%i==0){while(n%i==0)n/=i;}ret-=ret/i;}if(n>1){ret-=ret/n;}return ret;}
LL missing_number(LL A[],LL n){LL x1=A[0],x2=1;for(int i=1; i<n; i++)x1^=A[i];for(int i=2; i<=n+1; i++)x2^=i;return (x1^x2);}
bool isFibo(LL n){LL A=(5*n*n+4);LL B=(5*n*n-4); LL C=sqrt(A); LL D=sqrt(B); return (A==(C*C)||B==(D*D));}
LL classini(LL n){return (n&1)? -1: 1;} // f(n-1)*f(n+1) - f(n)*f(n)
LL factorial(int n){return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;}
LL catanal(LL n) {return (factorial(2*n)/((factorial(n+1)*factorial(n))));}//Dyck path// //1,1,2,5,14,42,132,429,1430,4862,16796,58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452
string orientation_3_ordered_point(vector< pair<int,int> >V){LL value=(V[1].second-V[0].second)*(V[2].first-V[1].first)-(V[1].first*V[0].first)*(V[2].second-V[1].second);string res;if(value==0)res="colinear";value ==1 ? res="clockwise" : res="counterclockwise";return res;}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};
int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};

string make_string(LL num){stringstream ss;ss<<num;string s;ss>>s;return s;}
LL make_num(string s){stringstream ss;LL tmp;ss<<s;ss>>tmp;return tmp;}

int SET(int mask,int pos){return (mask|(1<<pos));}
int check(int mask,int pos){return (mask&(1<<pos));}

vector<int>G[101010];
int Add[101010],Sub[1010101],Cos[1011010];

void dfs(int u,int par){
    for(auto v : G[u]){
        if(v!=par){
            dfs(v,u);
            Add[u]=max(Add[u],Add[v]);
            Sub[u]=max(Sub[v],Sub[u]);
        }
    }
    int tot=Cos[u]-Add[u]-Sub[u];
    tot >=0 ? Sub[u]+=abs(tot) : Add[u]+=abs(tot);
}

int main(){
    int n;
    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>>Cos[i];
    dfs(1,-1);
    cout<<Add[1]+Sub[1]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD=1000000007;
#define endl "\n"
#define fst first
#define snd second
const ll UNDEF = -1;
const ll INF=1e18;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define DEBUG_CAT
#ifdef DEBUG_CAT

#define dbg(...)   printf( __VA_ARGS__ )
#else
#define dbg(...)   /****nothing****/
#endif
static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof (stdinBuffer);
static const char* stdinPos = stdinDataEnd;

void readAhead(size_t amount)
{
    size_t remaining = stdinDataEnd - stdinPos;
    if (remaining < amount) {
       memmove(stdinBuffer, stdinPos, remaining);
       size_t sz = fread(stdinBuffer + remaining, 1, sizeof (stdinBuffer) - remaining, stdin);
       stdinPos = stdinBuffer;
       stdinDataEnd = stdinBuffer + remaining + sz;
       if (stdinDataEnd != stdinBuffer + sizeof (stdinBuffer))
         *stdinDataEnd = 0;
    }
}

int rint()
{
    readAhead(16);

    int x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}
char rch()
{
    readAhead(16);
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    char ans=*stdinPos;
    ++stdinPos;
    return ans;
}
long long rlong()
{
    readAhead(32);

    long long x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}
const int mn=1e5+4;
vector<int> g[mn];
int v[mn];
pll dfs(int x, int p) {
  pll now=MP(0ll,0ll);
  for (auto &y:g[x]) {
    if (y==p) continue;
    pll got=dfs(y,x);
    chkmax(now.fst,got.fst);
    chkmax(now.snd,got.snd);
  }
  ll newval=v[x]+now.fst-now.snd;
  if (newval>0) now.snd+=abs(newval);
  else now.fst+=abs(newval);
  return now;
}
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n=rint();
  for (int i=1;i<n;i++) {
    int x=rint(),y=rint();
    g[x].PB(y); g[y].PB(x);
  }
  for (int x=1;x<=n;x++) v[x]=rint();
  pll ans=dfs(1,-1);
  ll final=ans.fst+ans.snd;
  printf("%lld\n",final);
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,n) for(int i=a;i<(n);++i)
#define rfl(i,a,n) for(int i=(n-1);i>a-1;i--)
#define rep(i,n) for(int i=1;i<=n;i++)
#define rrep(i,n) for(int i=n;i>0;i--)
#define srt(a,n) sort(a+1,a+n+1)
#define li long
#define ll long long
#define ull unsigned long long
#define clr(a) memset(a,0,sizeof(a))
#define vi vector <int>
#define vl vector <long>
#define pii pair <int,int>
#define mod 1000000007
#define pb push_back
#define fr first
#define sc second
#define loop(i,n) for(int i=0;i<n;i++)
#define pi 3.1415926535
vi graph[100009];
bool vis[100009];
ll inc[100009],a[100009],decrease[100009];
void dfs(int i)
{
	vis[i] = true;
	int imax = 0,dmax = 0;
	loop(j,graph[i].size())
	{
		if(!vis[graph[i][j]])
		{
			dfs(graph[i][j]);
			if(inc[graph[i][j]]>imax) imax = inc[graph[i][j]];
			if(decrease[graph[i][j]]>dmax) dmax = decrease[graph[i][j]];
		}
	}
	inc[i] = imax;
	decrease[i] = dmax;
	a[i] += -inc[i] + decrease[i];
	if(a[i]>0) inc[i]+=a[i];
	else decrease[i] += -a[i];
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,x,y;
	cin>>n;
	rep(i,n-1)
	{
		cin>>x>>y;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	rep(i,n)
	{
		cin>>a[i];
	}
	dfs(1);
	// rep(i,n)
	// {
	// 	cout<<a[i]<<" "<<inc[i]<<" "<<decrease[i]<<endl;
	// }
	cout<<inc[1]+decrease[1]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,n) for(int i=a;i<(n);++i)
#define rfl(i,a,n) for(int i=(n-1);i>a-1;i--)
#define rep(i,n) for(int i=1;i<=n;i++)
#define rrep(i,n) for(int i=n;i>0;i--)
#define srt(a,n) sort(a+1,a+n+1)
#define li long
#define ll long long
#define ull unsigned long long
#define clr(a) memset(a,0,sizeof(a))
#define vi vector <int>
#define vl vector <long>
#define pii pair <int,int>
#define mod 1000000007
#define pb push_back
#define fr first
#define sc second
#define loop(i,n) for(int i=0;i<n;i++)
#define pi 3.1415926535
vi graph[100009];
bool vis[100009];
int moves[100009];
int a[100009];
void dfs(int i)
{
	vis[i] = true;
	loop(j,graph[i].size())
	{
		if(!vis[graph[i][j]])
		{
			dfs(graph[i][j]);
			moves[i]+=moves[graph[i][j]];
			moves[i]+=abs(a[graph[i][j]]);
			a[i]-=a[graph[i][j]];
			//cout<<i<<" "<<graph[i][j]<<endl;
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,x,b;
	cin>>n;
	rep(i,n-1)
	{
		cin>>x>>b;
		graph[x].push_back(b);
		graph[b].push_back(x);
	}
	rep(i,n)
	{
		cin>>a[i];
	}
	dfs(1);
	cout<<moves[1]+abs(a[1])<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,n) for(int i=a;i<(n);++i)
#define rfl(i,a,n) for(int i=(n-1);i>a-1;i--)
#define rep(i,n) for(int i=1;i<=n;i++)
#define rrep(i,n) for(int i=n;i>0;i--)
#define srt(a,n) sort(a+1,a+n+1)
#define li long
#define ll long long
#define ull unsigned long long
#define clr(a) memset(a,0,sizeof(a))
#define vi vector <int>
#define vl vector <long>
#define pii pair <int,int>
#define mod 1000000007
#define pb push_back
#define fr first
#define sc second
#define loop(i,n) for(int i=0;i<n;i++)
#define pi 3.1415926535
vi graph[100009];
bool vis[100009];
int inc[100009],a[100009],decrease[100009];
void dfs(int i)
{
	vis[i] = true;
	int imax = 0,dmax = 0;
	loop(j,graph[i].size())
	{
		if(!vis[graph[i][j]])
		{
			dfs(graph[i][j]);
			if(inc[graph[i][j]]>imax) imax = inc[graph[i][j]];
			if(decrease[graph[i][j]]>dmax) dmax = decrease[graph[i][j]];
		}
	}
	inc[i] = imax;
	decrease[i] = dmax;
	a[i] += -inc[i] + decrease[i];
	if(a[i]>0) inc[i]+=a[i];
	else decrease[i] += -a[i];
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,x,y;
	cin>>n;
	rep(i,n-1)
	{
		cin>>x>>y;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	rep(i,n)
	{
		cin>>a[i];
	}
	dfs(1);
	// rep(i,n)
	// {
	// 	cout<<a[i]<<" "<<inc[i]<<" "<<decrease[i]<<endl;
	// }
	cout<<inc[1]+decrease[1]<<endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
int dx[] = {1 , 0 , -1 ,0};
int dy[] = {0 , 1 , 0 , -1};
const ll mod =1e9 + 7;
const ll inf =1e16 + 7;
const int nax = 2e5 + 7;
ll exp(ll a , ll b){
	if(b == 0)
		return 1LL;
	if(b % 2 == 1)
		return (a%mod*exp(a , b - 1)%mod)%mod;
	else
		return (exp(a , b / 2)%mod * exp(a , b / 2)%mod)%mod;
}
int n;
vector< ll > g[nax];
ll a[nax] ,inc[nax] , dc[nax];
void dfs(int u , int par){
	for(auto v : g[u]){
		if(v!= par)
			dfs(v, u);
	}
	ll maxi = 0 , mini = 0;
	for(auto v : g[u]){
		maxi = max(maxi , a[v]);
		mini = min(mini  , a[v]);
	}
	ll temp = a[u] + maxi - mini;
	if(temp > 0)
		dc[u] += temp;
	else
		inc[u] += abs(temp);

}
int main() {
	fast;
	cin >> n;
	for(int i = 0, x , y ; i < n - 1 ; i++){
		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 , -1);
	cout << inc[1] + dc[1] << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
int dx[] = {1 , 0 , -1 ,0};
int dy[] = {0 , 1 , 0 , -1};
const ll mod =1e9 + 7;
const ll inf =1e16 + 7;
const int nax = 2e5 + 7;
ll exp(ll a , ll b){
	if(b == 0)
		return 1LL;
	if(b % 2 == 1)
		return (a%mod*exp(a , b - 1)%mod)%mod;
	else
		return (exp(a , b / 2)%mod * exp(a , b / 2)%mod)%mod;
}
int n;
vector< ll > g[nax];
ll a[nax] ,inc[nax] , dc[nax];
void dfs(int u , int par){
	for(auto v : g[u]){
		if(v!= par)
			dfs(v, u);
	}
	ll maxi = 0 , mini = 0;
	for(auto v : g[u]){
		maxi = max(maxi , inc[v]);
		mini = min(mini  , dc[v]);
	}
	inc[u] = maxi;
	dc[u] = mini;
	ll temp = a[u] + maxi - mini;
	if(temp > 0)
		dc[u] += temp;
	else
		inc[u] += abs(temp);

}
int main() {
	fast;
	cin >> n;
	for(int i = 0, x , y ; i < n - 1 ; i++){
		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 , -1);
	cout << inc[1] + dc[1] << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
int dx[] = {1 , 0 , -1 ,0};
int dy[] = {0 , 1 , 0 , -1};
const ll mod =1e9 + 7;
const ll inf =1e16 + 7;
const int nax = 2e5 + 7;
ll exp(ll a , ll b){
	if(b == 0)
		return 1LL;
	if(b % 2 == 1)
		return (a%mod*exp(a , b - 1)%mod)%mod;
	else
		return (exp(a , b / 2)%mod * exp(a , b / 2)%mod)%mod;
}
int n;
vector< ll > g[nax];
ll a[nax] ,inc[nax] , dc[nax];
void dfs(int u , int par){
	for(auto v : g[u]){
		if(v!= par)
			dfs(v, u);
	}
	ll maxi = 0 , mini = 0;
	for(auto v : g[u]){
		maxi = max(maxi , inc[v]);
		mini = min(mini  , dc[v]);
	}
	ll temp = a[u] + maxi - mini;
	if(temp > 0)
		dc[u] += temp;
	else
		inc[u] += abs(temp);

}
int main() {
	fast;
	cin >> n;
	for(int i = 0, x , y ; i < n - 1 ; i++){
		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 , -1);
	cout << inc[1] + dc[1] << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
int dx[] = {1 , 0 , -1 ,0};
int dy[] = {0 , 1 , 0 , -1};
const ll mod =1e9 + 7;
const ll inf =1e16 + 7;
const int nax = 2e5 + 7;
ll exp(ll a , ll b){
	if(b == 0)
		return 1LL;
	if(b % 2 == 1)
		return (a%mod*exp(a , b - 1)%mod)%mod;
	else
		return (exp(a , b / 2)%mod * exp(a , b / 2)%mod)%mod;
}
int n;
vector< ll > g[nax];
ll a[nax] ,inc[nax] , dc[nax];
void dfs(int u , int par){
	for(auto v : g[u]){
		if(v!= par)
			dfs(v, u);
	}
	ll maxinc = 0 , maxidc = 0;
	for(auto v : g[u]){
		maxinc = max(maxinc, inc[v]);
		maxidc = max(maxidc  , dc[v]);
	}
	inc[u] = maxinc;
	dc[u] = maxidc;
	ll temp = a[u] + maxinc - maxidc;
	if(temp > 0)
		dc[u] += abs(temp);
	else
		inc[u] += abs(temp);

}
int main() {
	fast;
	cin >> n;
	for(int i = 0, x , y ; i < n - 1 ; i++){
		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 , -1);
	cout << inc[1] + dc[1] << endl;
	return 0;
}


/*
░░░░██████████████████
░░▄███████▀▀▀▀▀▀███████▄
░▐████▀▒mohammad▒▀██████▄
░███▀▒▒▒▒alaa▒▒▒▒▒▒▀█████
░▐██▒▒▒alwrawrah▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄██████████████▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
*/

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

typedef long long ll ;
const ll M = 1e15 + 7 ;
const ll oo = 1e13 ;
const double PI = acos(-1) ;

ll de[100010] , n , mx , v[100010] , lz[100010] , p[100010] ;
vector<int> g[100010] , gde[100010];
ll ans ;

void dfs(int u){
	de[u] = de[p[u]] + 1 ;
	gde[de[u]].push_back(u);
	mx = max(mx , de[u]);
	for(auto x : g[u])
		if(x != p[u]){
			p[x] = u ;
			dfs(x);
		}
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input" , "r" , stdin );
		freopen("output" , "w" , stdout );
	#endif
	cin >> n;
	for(int a,b,i = 0 ; i < n - 1 ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1 ; i <= n ; ++i)
		cin >> v[i];
	p[1] = 0;
	dfs(1);
	for(int i = mx ; i ; --i){
		int pl = 0 , mi = 0 ;
		for(auto x : gde[i]){
			if(lz[x]){
				v[x] += lz[x];
				lz[p[x]] = lz[x];
				lz[x] = 0 ;
			}
			lz[p[x]] += -v[x];
			if(v[x] > 0)
				pl = max(pl , v[x]);
			else
				mi = min(mi , v[x]);
		}
		ans += pl - mi;
	}
	printf("%lld\n",ans );
}


/*
░░░░██████████████████
░░▄███████▀▀▀▀▀▀███████▄
░▐████▀▒mohammad▒▀██████▄
░███▀▒▒▒▒alaa▒▒▒▒▒▒▀█████
░▐██▒▒▒alwrawrah▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄██████████████▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
*/

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

typedef long long ll ;
const ll M = 1e15 + 7 ;
const ll oo = 1e13 ;
const double PI = acos(-1) ;

ll de[100010] , n , mx , v[100010] , p[100010] ;
pair<ll , ll> lz[100010];
vector<int> g[100010] , gde[100010];
ll ans ;

void dfs(int u){
	de[u] = de[p[u]] + 1 ;
	gde[de[u]].push_back(u);
	mx = max(mx , de[u]);
	for(auto x : g[u])
		if(x != p[u]){
			p[x] = u ;
			dfs(x);
		}
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input" , "r" , stdin );
		freopen("output" , "w" , stdout );
	#endif
	cin >> n;
	for(int a,b,i = 0 ; i < n - 1 ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1 ; i <= n ; ++i)
		cin >> v[i];
	p[1] = 0;
	dfs(1);
	for(int i = mx ; i ; --i){
		ll pl = 0 , mi = 0 ;
		for(auto x : gde[i]){
			if(lz[x].first || lz[x].second){
				v[x] += (-lz[x].second) - lz[x].first;
				lz[p[x]].first = max(lz[x].first , lz[p[x]].first);
				lz[p[x]].second = min(lz[x].second , lz[p[x]].second);
			}
			lz[p[x]].first = max(v[x] , lz[p[x]].first);
			lz[p[x]].second = min(v[x] , lz[p[x]].second);
			if(v[x] > 0)
				pl = max(pl , v[x]);
			else
				mi = min(mi , v[x]);
		}
		ans += pl - mi;
	}
	printf("%lld\n",ans );
}


/*
░░░░██████████████████
░░▄███████▀▀▀▀▀▀███████▄
░▐████▀▒mohammad▒▀██████▄
░███▀▒▒▒▒alaa▒▒▒▒▒▒▀█████
░▐██▒▒▒alwrawrah▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄██████████████▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
*/

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

typedef long long ll ;
const ll M = 1e15 + 7 ;
const ll oo = 1e13 ;
const double PI = acos(-1) ;

ll de[100010] , n , mx , v[100010] , lz[100010] , p[100010] ;
vector<int> g[100010] , gde[100010];
ll ans ;

void dfs(int u){
	de[u] = de[p[u]] + 1 ;
	gde[de[u]].push_back(u);
	mx = max(mx , de[u]);
	for(auto x : g[u])
		if(x != p[u]){
			p[x] = u ;
			dfs(x);
		}
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input" , "r" , stdin );
		freopen("output" , "w" , stdout );
	#endif
	cin >> n;
	for(int a,b,i = 0 ; i < n - 1 ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1 ; i <= n ; ++i)
		cin >> v[i];
	p[1] = 0;
	dfs(1);
	for(int i = mx ; i ; --i){
		ll pl = 0 , mi = 0 ;
		for(auto x : gde[i]){
			if(lz[x]){
				v[x] += lz[x];
				lz[p[x]] = lz[x];
				lz[x] = 0 ;
			}
			lz[p[x]] += -v[x];
			if(v[x] > 0)
				pl = max(pl , v[x]);
			else
				mi = min(mi , v[x]);
		}
		ans += pl - mi;
	}
	printf("%lld\n",ans );
}


/*
░░░░██████████████████
░░▄███████▀▀▀▀▀▀███████▄
░▐████▀▒mohammad▒▀██████▄
░███▀▒▒▒▒alaa▒▒▒▒▒▒▀█████
░▐██▒▒▒alwrawrah▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄██████████████▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
*/

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

typedef long long ll ;
const ll M = 1e15 + 7 ;
const ll oo = 1e13 ;
const double PI = acos(-1) ;

ll n , v[100010];
pair<ll , ll> lz[100010];
vector<int> g[100010];

void dfs(int u , int p){
	for(auto x : g[u])
		if(x != p){
			dfs(x , u);
			lz[u].first = max(lz[u].first , lz[x].first);
			lz[u].second = max(lz[u].second , lz[x].second);
		}
	ll ch = v[u] + lz[u].first - lz[u].second;
	if(ch < 0)
		lz[u].first += -ch ;
	else
		lz[u].second += ch ;
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input" , "r" , stdin );
		freopen("output" , "w" , stdout );
	#endif
	cin >> n;
	for(int a,b,i = 0 ; i < n - 1 ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1 ; i <= n ; ++i)
		cin >> v[i];
	dfs(1 , 0);
	printf("%lld\n",lz[1].second + lz[1].first );
}


/*
░░░░██████████████████
░░▄███████▀▀▀▀▀▀███████▄
░▐████▀▒mohammad▒▀██████▄
░███▀▒▒▒▒alaa▒▒▒▒▒▒▀█████
░▐██▒▒▒alwrawrah▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄██████████████▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
*/

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

typedef long long ll ;
const ll M = 1e15 + 7 ;
const ll oo = 1e13 ;
const double PI = acos(-1) ;

int de[100010] , n , mx , v[100010] , lz[100010] , p[100010] ;
vector<int> g[100010] , gde[100010];
ll ans ;

void dfs(int u){
	de[u] = de[p[u]] + 1 ;
	gde[de[u]].push_back(u);
	mx = max(mx , de[u]);
	for(auto x : g[u])
		if(x != p[u]){
			p[x] = u ;
			dfs(x);
		}
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input" , "r" , stdin );
		freopen("output" , "w" , stdout );
	#endif
	cin >> n;
	for(int a,b,i = 0 ; i < n - 1 ; ++i){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1 ; i <= n ; ++i)
		cin >> v[i];
	p[1] = 0;
	dfs(1);
	for(int i = mx ; i ; --i){
		int pl = 0 , mi = 0 ;
		for(auto x : gde[i]){
			if(lz[x]){
				v[x] += lz[x];
				lz[p[x]] = lz[x];
				lz[x] = 0 ;
			}
			lz[p[x]] += -v[x];
			if(v[x] > 0)
				pl = max(pl , v[x]);
			else
				mi = min(mi , v[x]);
		}
		ans += pl - mi;
	}
	printf("%lld\n",ans );
}


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

const int N = 100005;
struct node
{
    int i,d;
    node():i(0),d(0){}
    node(int i,int d):i(i),d(d){}
};
queue<int> q;
vector<int> g[N];
int par[N];
int deg[N];
int v[N];
node a[N];

void dfs(int u,int p)
{
    par[u] = p;
    if(deg[u]==1 && u!=1 || deg[u]==0 && u==1)
    {
        q.push(u);
    }
    for(int v: g[u])
    {
        if(v!=p)
            dfs(v,u);
    }
}
int main()
{
    //freopen("inputf.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);
    dfs(1,0);
    ll ans = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==1)
        {
            ans = a[1].i + a[1].d+abs(v[1]);
            break;
        }
        if(v[u]+a[u].i-a[u].d>0)
        {
            a[u].d = v[u]+a[u].i;
        }
        else
        {
            a[u].i = a[u].d-v[u];
        }
        a[par[u]].i = max(a[par[u]].i,a[u].i);
        a[par[u]].d = max(a[par[u]].d,a[u].d);
        deg[par[u]]--;
        if(deg[par[u]]==1 && par[u]!=1 || par[u]==1 && deg[par[u]]==0)
            q.push(par[u]);
    }
    cout<<ans;
    return 0;
}


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

const int N = 100005;
struct node
{
    int i,d;
    node():i(0),d(0){}
    node(int i,int d):i(i),d(d){}
};
queue<int> q;
vector<int> g[N];
int par[N];
int deg[N];
int v[N];
node a[N];

void dfs(int u,int p)
{
    par[u] = p;
    if(deg[u]==1 && u!=1 || deg[u]==0 && u==1)
    {
        q.push(u);
    }
    for(int v: g[u])
    {
        if(v!=p)
            dfs(v,u);
    }
}
signed main()
{
    //freopen("inputf.txt","r",stdin);
    int n;
    scanf("%lld",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%lld %lld",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    for(int i=1;i<=n;i++)
        scanf("%lld",&v[i]);
    dfs(1,0);
    ll ans = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==1)
        {
            ans = a[1].i+a[1].d+abs(v[1]+a[1].i-a[1].d);
            break;
        }
        if(v[u]+a[u].i-a[u].d>0)
        {
            a[u].d = v[u]+a[u].i;
        }
        else
        {
            a[u].i = a[u].d-v[u];
        }
        //cout<<u<<" "<<a[u].i<<" "<<a[u].d<<endl;
        a[par[u]].i = max(a[par[u]].i,a[u].i);
        a[par[u]].d = max(a[par[u]].d,a[u].d);
        deg[par[u]]--;
        if(deg[par[u]]==1 && par[u]!=1 || par[u]==1 && deg[par[u]]==0)
            q.push(par[u]);
    }
    cout<<ans;
    return 0;
}


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

const int N = 100005;
struct node
{
    int i,d;
    node():i(0),d(0){}
    node(int i,int d):i(i),d(d){}
};
queue<int> q;
vector<int> g[N];
int par[N];
int deg[N];
int v[N];
node a[N];

void dfs(int u,int p)
{
    par[u] = p;
    if(deg[u]==1 && u!=1 || deg[u]==0 && u==1)
    {
        q.push(u);
    }
    for(int v: g[u])
    {
        if(v!=p)
            dfs(v,u);
    }
}
signed main()
{
    //freopen("inputf.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);
    dfs(1,0);
    ll ans = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==1)
        {
            ans = a[1].i+a[1].d+abs(v[1]+a[1].i-a[1].d);
            break;
        }
        if(v[u]+a[u].i-a[u].d>0)
        {
            a[u].d = v[u]+a[u].i;
        }
        else
        {
            a[u].i = a[u].d-v[u];
        }
        //cout<<u<<" "<<a[u].i<<" "<<a[u].d<<endl;
        a[par[u]].i = max(a[par[u]].i,a[u].i);
        a[par[u]].d = max(a[par[u]].d,a[u].d);
        deg[par[u]]--;
        if(deg[par[u]]==1 && par[u]!=1 || par[u]==1 && deg[par[u]]==0)
            q.push(par[u]);
    }
    cout<<ans;
    return 0;
}


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

const int N = 100005;
struct node
{
    int i,d;
    node():i(0),d(0){}
    node(int i,int d):i(i),d(d){}
};
queue<int> q;
vector<int> g[N];
int par[N];
int deg[N];
int v[N];
node a[N];

void dfs(int u,int p)
{
    par[u] = p;
    if(deg[u]==1 && u!=1 || deg[u]==0 && u==1)
    {
        q.push(u);
    }
    for(int v: g[u])
    {
        if(v!=p)
            dfs(v,u);
    }
}
int main()
{
    //freopen("inputf.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
        deg[u]++;
        deg[v]++;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);
    dfs(1,0);
    ll ans = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==1)
        {
            ans = a[1].i+a[1].d+abs(v[1]+a[1].i-a[1].d);
            break;
        }
        if(v[u]+a[u].i-a[u].d>0)
        {
            a[u].d = v[u]+a[u].i;
        }
        else
        {
            a[u].i = a[u].d-v[u];
        }
        //cout<<u<<" "<<a[u].i<<" "<<a[u].d<<endl;
        a[par[u]].i = max(a[par[u]].i,a[u].i);
        a[par[u]].d = max(a[par[u]].d,a[u].d);
        deg[par[u]]--;
        if(deg[par[u]]==1 && par[u]!=1 || par[u]==1 && deg[par[u]]==0)
            q.push(par[u]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define noo     \
    {           \
        no();   \
        return; \
    }

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false) {
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++) {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false) {
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++) {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false) {
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++) {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

template <typename T, typename A>
void papair(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i].first << " " << a[i].second << endl;
    }
    cout << "\n";
}

void yes() { cout << "YES\n"; }

void no() {
    cout << "NO\n";
    // cout << -1 <<endl;
}

void yesno(int f) {
    if (f)
        yes();
    else
        no();
}

#define MAX (ll)3e5
v parents(MAX);
vb isleaf(MAX, false);
void dfs(vv &adjList, ll curr, ll prev) {
    parents[curr] = prev;
    bool l = true;
    for (auto neigh : adjList[curr]) {
        if (neigh != prev) {
            dfs(adjList, neigh, curr);
            l = false;
        }
    }
    isleaf[curr] = l;
}

void solve() {
    ll n;
    cin >> n;
    ll m = n - 1;

    // create a digraph with one as root
    vv adjList(n + 1, v(0));
    v outdeg(n + 1, 0);
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;

        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    v vals = ga(n, 1);
    dfs(adjList, 1, -1);
    for (ll i = 2; i <= n; i++) {
        outdeg[parents[i]]++;
    }

    queue<ll> degrees;
    for (ll i = 1; i <= n; i++)
        if (!outdeg[i])
            degrees.push(i);
    v toposort = {};
    while (!degrees.empty()) {
        auto node = degrees.front();
        degrees.pop();
        toposort.push_back(node);
        if (node != 1) {
            ll neigh = parents[node];
            outdeg[neigh]--;
            if (!outdeg[neigh])
                degrees.push(neigh);
        }
    }

    // come from leaf-side to root-side; handle leafs first
    v aggChange(n + 1, 0);
    ll ops = 0;
    for (auto node : toposort) {
        vals[node] += aggChange[node];
        ll diff = vals[node];
        ops += abs(diff);

        if (node != 1) {
            aggChange[parents[node]] += aggChange[node] - diff;
        }
    }
    cout << ops;
}

int main(void) {
#ifndef ONLINE_JUDGE
    // freopen("inp.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false) {
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++) {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vs gas(lu n, bool oneIndexed = false) {
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++) {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << endl;
}

template <typename T, typename A>
void papair(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i].first << " " << a[i].second << endl;
    }
}

#define MAX (ll)3e5
v values;
v incr(3e5, 0), decr(3e5, 0);

void dfs(vv &adjList, ll curr, ll prev) {
    ll maxci = 0, maxcd = 0;

    for (auto neigh : adjList[curr]) {
        if (neigh != prev) {
            dfs(adjList, neigh, curr);
            amax(maxci, incr[neigh]);
            amax(maxcd, decr[neigh]);
        }
    }
    ll nval = maxci - maxcd + values[curr];
    incr[curr] = maxci + max(-nval, 0ll);
    decr[curr] = maxcd + max(nval, 0ll);
}

void solve() {
    ll n, m;
    cin >> n;
    m = n - 1;
    vv adjList(n + 1, v(0));
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    values = ga(n, 1);
    dfs(adjList, 1, -1);
    cout << incr[1] + decr[1];
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define noo     \
    {           \
        no();   \
        return; \
    }

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false) {
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++) {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false) {
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++) {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false) {
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++) {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

template <typename T, typename A>
void papair(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i].first << " " << a[i].second << endl;
    }
    cout << "\n";
}

void yes() { cout << "YES\n"; }

void no() {
    cout << "NO\n";
    // cout << -1 <<endl;
}

void yesno(int f) {
    if (f)
        yes();
    else
        no();
}

#define MAX (ll)3e5
v parents(MAX);
vb isleaf(MAX, false);
void dfs(vv &adjList, ll curr, ll prev) {
    parents[curr] = prev;
    bool l = true;
    for (auto neigh : adjList[curr]) {
        dfs(adjList, neigh, curr);
        l = false;
    }
    isleaf[curr] = l;
}

void solve() {
    ll n;
    cin >> n;
    ll m = n - 1;

    // create a digraph with one as root
    vv adjList(n + 1, v(0));
    v indeg(n + 1, 0);
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        adjList[a].push_back(b);
        indeg[b]++;
        // adjList[b].push_back(a);
    }
    v vals = ga(n, 1);
    dfs(adjList, 1, -1);

    queue<ll> degrees;
    for (ll i = 1; i <= n; i++)
        if (!indeg[i])
            degrees.push(i);
    v toposort = {};
    while (!degrees.empty()) {
        auto node = degrees.front();
        degrees.pop();
        toposort.push_back(node);
        for (auto neigh : adjList[node]) {
            indeg[neigh]--;
            if (!indeg[neigh])
                degrees.push(neigh);
        }
    }

    // come from leaf-side to root-side; handle leafs first
    ll ops = 0;
    for (auto node : toposort) {
        ll diff = vals[node];
        ops += abs(diff);
        if (node != 1)
            vals[parents[node]] += -diff;
    }
    cout << ops;
}

int main(void) {
#ifndef ONLINE_JUDGE
    // freopen("inp.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define noo     \
    {           \
        no();   \
        return; \
    }

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false) {
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++) {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false) {
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++) {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false) {
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++) {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

template <typename T, typename A>
void papair(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i].first << " " << a[i].second << endl;
    }
    cout << "\n";
}

void yes() { cout << "YES\n"; }

void no() {
    cout << "NO\n";
    // cout << -1 <<endl;
}

void yesno(int f) {
    if (f)
        yes();
    else
        no();
}

#define MAX (ll)3e5
v parents(MAX);
vb isleaf(MAX, false);
void dfs(vv &adjList, ll curr, ll prev) {
    parents[curr] = prev;
    bool l = true;
    for (auto neigh : adjList[curr]) {
        if (neigh != prev) {
            dfs(adjList, neigh, curr);
            l = false;
        }
    }
    isleaf[curr] = l;
}

void solve() {
    ll n;
    cin >> n;
    ll m = n - 1;

    // create a digraph with one as root
    vv adjList(n + 1, v(0));
    v indeg(n + 1, 0);
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
        indeg[b]++;
    }
    v vals = ga(n, 1);
    dfs(adjList, 1, -1);

    queue<ll> degrees;
    for (ll i = 1; i <= n; i++)
        if (!indeg[i])
            degrees.push(i);
    v toposort = {};
    while (!degrees.empty()) {
        auto node = degrees.front();
        degrees.pop();
        toposort.push_back(node);
        for (auto neigh : adjList[node]) {
            indeg[neigh]--;
            if (!indeg[neigh])
                degrees.push(neigh);
        }
    }

    // come from leaf-side to root-side; handle leafs first
    v aggChange(n + 1, 0);
    ll ops = 0;
    for (auto node : toposort) {
        vals[node] += aggChange[node];
        ll diff = vals[node];
        ops += abs(diff);

        if (node != 1) {
            aggChange[parents[node]] += aggChange[node] - diff;
        }
    }
    cout << ops;
}

int main(void) {
#ifndef ONLINE_JUDGE
    // freopen("inp.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define noo     \
    {           \
        no();   \
        return; \
    }

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false) {
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++) {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false) {
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++) {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false) {
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++) {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

template <typename T, typename A>
void papair(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        // printf("%lu ", a[i]);
        cout << a[i].first << " " << a[i].second << endl;
    }
    cout << "\n";
}

void yes() { cout << "YES\n"; }

void no() {
    cout << "NO\n";
    // cout << -1 <<endl;
}

void yesno(int f) {
    if (f)
        yes();
    else
        no();
}

#define MAX (ll)3e5
v parents(MAX);
vb isleaf(MAX, false);
void dfs(vv &adjList, ll curr, ll prev) {
    parents[curr] = prev;
    bool l = true;
    for (auto neigh : adjList[curr]) {
        if (neigh != prev) {
            dfs(adjList, neigh, curr);
            l = false;
        }
    }
    isleaf[curr] = l;
}

void solve() {
    ll n;
    cin >> n;
    ll m = n - 1;

    // create a digraph with one as root
    vv adjList(n + 1, v(0));
    v indeg(n + 1, 0);
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
        indeg[b]++;
    }
    v vals = ga(n, 1);
    dfs(adjList, 1, -1);

    queue<ll> degrees;
    for (ll i = 1; i <= n; i++)
        if (!indeg[i])
            degrees.push(i);
    v toposort = {};
    while (!degrees.empty()) {
        auto node = degrees.front();
        degrees.pop();
        toposort.push_back(node);
        for (auto neigh : adjList[node]) {
            indeg[neigh]--;
            if (!indeg[neigh])
                degrees.push(neigh);
        }
    }

    // come from leaf-side to root-side; handle leafs first
    ll ops = 0;
    for (auto node : toposort) {
        ll diff = vals[node];
        ops += abs(diff);
        if (node != 1)
            vals[parents[node]] += -diff;
    }
    cout << ops;
}

int main(void) {
#ifndef ONLINE_JUDGE
    // freopen("inp.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author ocelopilli
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    LinkedList< Integer >[] g;
    long[] v = new long[ 100005 ];
    boolean[] seen = new boolean[ 100005 ];
    long dfs(int vertex)
    {
        seen[ vertex ] = true;
        long mx = 0, mn = 0;
        long ret = 0;
        for (int next : g[vertex]) if ( !seen[ next ] )
        {
            ret += dfs( next );
            mx = Math.max( mx, v[next] );
            mn = Math.min( mn, v[next] );
        }
        v[ vertex ] += Math.abs( mn );
        v[ vertex ] -= Math.abs( mx );
        ret += mx - mn;
        return ret;
    }
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        g = new LinkedList[ n ];
        for (int i=0; i<n; i++) g[i] = new LinkedList<Integer>();
        for (int i=1; i<n; i++)
        {
            int u = in.nextInt();
            int v = in.nextInt();
            g[ u-1 ].add( v-1 );
            g[ v-1 ].add( u-1 );
        }
        for (int i=0; i<n; i++) v[i] = in.nextInt();
        out.println( dfs( 0 ) + Math.abs(v[0]) );
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader(InputStream in)
    {
        br = new BufferedReader(new InputStreamReader(in));
        st = null;
    }

    public String next()
    {
        while (st==null || !st.hasMoreTokens())
        {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }

    public int nextInt()
    {
        return Integer.parseInt(next());
    }

    }


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author ocelopilli
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    LinkedList< Integer >[] g;
    long[] v = new long[ 100005 ];
    long[] mx = new long[ 100005 ];
    long[] mn = new long[ 100005 ];
    void dfs(int vertex, int parent)
    {
        for (int next : g[vertex]) if ( next != parent )
        {
            dfs( next, vertex );
            mx[vertex] = Math.max( mx[vertex], mx[next] );
            mn[vertex] = Math.min( mn[vertex], mn[next] );
        }
        v[vertex] += mx[vertex];
        v[vertex] += mn[vertex];
        if ( v[vertex] > 0 ) mn[vertex] -= v[vertex];
        else mx[vertex] -= v[vertex];
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        g = new LinkedList[ n ];
        for (int i=0; i<n; i++) g[i] = new LinkedList<Integer>();
        for (int i=1; i<n; i++)
        {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            g[ u ].add( v );
            g[ v ].add( u );
        }
        for (int i=0; i<n; i++) v[i] = in.nextInt();
        dfs( 0, -1 );
        out.println( Math.abs(mx[0]) + Math.abs(mn[0]) );
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader(InputStream in)
    {
        br = new BufferedReader(new InputStreamReader(in));
        st = null;
    }

    public String next()
    {
        while (st==null || !st.hasMoreTokens())
        {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }

    public int nextInt()
    {
        return Integer.parseInt(next());
    }

    }


import java.util.*;
import java.io.*;
public class Main
{

	static final int ub = 5 + (int) 1e5;
	// graph
	static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
	static long[] value = new long[ub];
	static long[] add = new long[ub];
	static long[] sub = new long[ub];
	static int N;

	public static void main(String[] args)
	{
		FastScanner in = new FastScanner(System.in);
		// get input N
		N = in.nextInt();
		// initialize graph
		for (int i = 1;i <= N + 5; i++)
			graph.add(new ArrayList<Integer>());
		// get a, b and make graph
		for (int i = 1; i <= N - 1; i++)
		{
			int a = in.nextInt();
			int b = in.nextInt();
			graph.get(a).add(b);
			graph.get(b).add(a);
		}

		// get value
		for (int i = 1; i <= N; i++)
			value[i] = in.nextInt();

		// dfs(-1, 1)
		dfs(-1, 1);

		// print add[1] + sub[1]
		System.out.println(add[1] + sub[1]);
	}

	// update the value of add[cur] and sub[cur]
	static void dfs(int prev, int cur)
	{
		// if cur has no child
		if (graph.get(cur).size() == 1 &&
			graph.get(cur).get(0) == prev)
		{
			if (value[cur] > 0)
				sub[cur] = value[cur];
			else
				add[cur] = -value[cur];
		}
		else
		{

			long maxAdd = 0, maxSub = 0;
			// for each child
			for (Integer child : graph.get(cur))
			{
				if (child != prev)
				{
					dfs(cur, child);
					maxAdd = Math.max(maxAdd, add[child]);
					maxSub = Math.max(maxSub, sub[child]);
				}
			}

			// deal with value[cur]
			long v = value[cur] + maxAdd - maxSub;
			if (v > 0)
				maxSub += v;
			else
				maxAdd -= v;

			// update add[cur], sub[cur]
			add[cur] = maxAdd;
			sub[cur] = maxSub;

		}

	}
}
class FastScanner
{
	BufferedReader reader;
	StringTokenizer tokenizer;
	FastScanner (InputStream inputStream)
	{
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}
	String nextToken()
	{
		while (tokenizer == null || ! tokenizer.hasMoreTokens())
		{
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (Exception e){}
		}
		return tokenizer.nextToken();
	}
	int nextInt()
	{
		return Integer.parseInt(nextToken());
	}
}


import java.util.*;
import java.io.*;
public class Main
{
	static ArrayList<ArrayList<Integer>> graph
		= new ArrayList<ArrayList<Integer>>();
	static int N;
	static int[] value = new int[100005];
	public static void main(String[] args)
	{
		FastScanner in = new FastScanner(System.in);
		int N = in.nextInt();
		for (int i = 0; i  <= N + 4; i++)
			graph.add(new ArrayList<Integer>());
		for (int i = 1; i <= N - 1; i++)
		{
			int a = in.nextInt();
			int b = in.nextInt();
			graph.get(a).add(b);
			graph.get(b).add(a);
		}
		for (int i = 1; i <= N; i++)
			value[i] = in.nextInt();
		Operation operationOnRoot = getOperation(-1, 1);
		System.out.println(operationOnRoot.add + operationOnRoot.sub);
	}
	// find the operation that is performed on node nodeNumber
	static Operation getOperation(int previous, int nodeNumber)
	{
		// if this is end, then...?
		if (graph.get(nodeNumber).size() == 1 &&
			graph.get(nodeNumber).get(0) == previous)
		{
			int add = 0;
			int sub = 0;
			if (value[nodeNumber] < 0)
				add = -value[nodeNumber];
			else
				sub = value[nodeNumber];
			return new Operation(add, sub);
		}
		int maxAdd = 0;
		int maxSub = 0;
		// for each children, get operation
		for (Integer node : graph.get(nodeNumber))
		{
			if (node != previous)
			{
				Operation ope = getOperation(nodeNumber, node);
				maxAdd = Math.max(maxAdd, ope.add);
				maxSub = Math.max(maxSub, ope.sub);
			}
		}
		int v = value[nodeNumber];
		v += maxAdd;
		v -= maxSub;
		if (v < 0)
			maxAdd += -v;
		else
			maxSub += v;
		// and find how many times we need to add/sub to make this node 0
		return new Operation(maxAdd, maxSub);
	}
}
class Operation
{
	int add;
	int sub;
	Operation(int add, int sub)
	{
		this.add = add;
		this.sub = sub;
	}
}
class FastScanner
{
	BufferedReader reader;
	StringTokenizer tokenizer;
	FastScanner (InputStream inputStream)
	{
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}
	String nextToken()
	{
		while (tokenizer == null || ! tokenizer.hasMoreTokens())
		{
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (Exception e){}
		}
		return tokenizer.nextToken();
	}
	int nextInt()
	{
		return Integer.parseInt(nextToken());
	}
}


import java.util.*;
import java.io.*;
public class Main
{
	static ArrayList<ArrayList<Integer>> graph
		= new ArrayList<ArrayList<Integer>>();
	static int N;
	static int[] value = new int[100005];
	public static void main(String[] args)
	{
		FastScanner in = new FastScanner(System.in);
		int N = in.nextInt();
		for (int i = 0; i  <= N + 4; i++)
			graph.add(new ArrayList<Integer>());
		for (int i = 1; i <= N - 1; i++)
		{
			int a = in.nextInt();
			int b = in.nextInt();
			graph.get(a).add(b);
			graph.get(b).add(a);
		}
		for (int i = 1; i <= N; i++)
			value[i] = in.nextInt();
		Operation operationOnRoot = getOperation(-1, 1);
		System.out.println(operationOnRoot.add + operationOnRoot.sub);
	}
	// find the operation that is performed on node nodeNumber
	static Operation getOperation(int previous, int nodeNumber)
	{
		// if this is end, then...?
		if (graph.get(nodeNumber).size() == 1 &&
			graph.get(nodeNumber).get(0) == previous)
		{
			long add = 0;
			long sub = 0;
			if (value[nodeNumber] < 0)
				add = -value[nodeNumber];
			else
				sub = value[nodeNumber];
			return new Operation(add, sub);
		}
		long maxAdd = 0;
		long maxSub = 0;
		// for each children, get operation
		for (Integer node : graph.get(nodeNumber))
		{
			if (node != previous)
			{
				Operation ope = getOperation(nodeNumber, node);
				maxAdd = Math.max(maxAdd, ope.add);
				maxSub = Math.max(maxSub, ope.sub);
			}
		}
		long v = value[nodeNumber];
		v += maxAdd;
		v -= maxSub;
		if (v < 0)
			maxAdd += -v;
		else
			maxSub += v;
		// and find how many times we need to add/sub to make this node 0
		return new Operation(maxAdd, maxSub);
	}
}
class Operation
{
	long add;
	long sub;
	Operation(long add, long sub)
	{
		this.add = add;
		this.sub = sub;
	}
}
class FastScanner
{
	BufferedReader reader;
	StringTokenizer tokenizer;
	FastScanner (InputStream inputStream)
	{
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}
	String nextToken()
	{
		while (tokenizer == null || ! tokenizer.hasMoreTokens())
		{
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (Exception e){}
		}
		return tokenizer.nextToken();
	}
	int nextInt()
	{
		return Integer.parseInt(nextToken());
	}
}


import java.io.DataInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.TreeMap;

public class ZeroTree {
	static TreeMap<Integer, ArrayList<Integer>> w;
	static boolean [] c;
	static int val [] ;
	public static void main(String [] args) throws Exception{
		FS in = new FS(System.in);
		int n = in.nextInt();
		w = new TreeMap<>();
		c = new boolean[n+1];
		val = new int[n+1];
		for(int i = 1 ; i <= n ; i++){
			w.put(i, new ArrayList<Integer>());
		}
		for(int i = 1 ; i < n ; i++){
			int t = in.nextInt();
			int h = in.nextInt();
			w.get(t).add(h);
			w.get(h).add(t);
		}
		for(int i= 1 ; i <= n ;i++){
			val[i]=in.nextInt();
		}
		System.out.println(DFS(1));
	}
	public static ZT DFS(int x){
		c[x]=true;
		ArrayList<Integer> aa = w.get(x);
		long a =0;
		long s =0;
		for(int i : aa){
			if(!c[i]){
				ZT z = DFS(i);
				a=Math.max(a, z.a);
				s=Math.max(s, z.s);
			}
		}
		if(val[x]+a-s>0){
			s+=val[x]+a-s;
		}
		else a-=val[x]+a-s;
		return new ZT(a,s);
	}
}
class ZT{
	long s;
	long a;
	public ZT(long aa , long ss){
		s=ss;
		a=aa;
	}
	@Override
	public String toString() {
		return a+s+"";
	}
}
class FS
{
   final private int BUFFER_SIZE = 1 << 17;

   private DataInputStream din;
   private byte[] buffer;
   private int bufferPointer, bytesRead;

   public FS(InputStream in)
   {
      din = new DataInputStream(in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
   }
   public String nextString() throws Exception
   {
       StringBuffer sb=new StringBuffer("");
       byte c = read();
       while (c <= ' ') c = read();
       do
       {
           sb.append((char)c);
           c=read();
       }while(c>' ');
       return sb.toString();
   }
   public char nextChar() throws Exception
   {
       byte c=read();
       while(c<=' ') c= read();
       return (char)c;
   }
   public int nextInt() throws Exception
   {
      int ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   public long nextLong() throws Exception
   {
      long ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   private void fillBuffer() throws Exception
   {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) buffer[0] = -1;
   }

   private byte read() throws Exception
   {
      if (bufferPointer == bytesRead) fillBuffer();
      return buffer[bufferPointer++];
   }
}