// Author :mastermindevil/mdragneell
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ull unsigned long long
#define ll long long
#define int  long long
#define ld long double
#define pb push_back
#define pi  pair< int,int >
#define pl  pair< ll,ll >
#define f first
#define s second
#define endl '\n'

const int mod=998244353;
const int N=1e5+5;

ll fact[N];
ll min(ll x,ll y) { if(x < y) return x; else return y; }
ll max(ll x,ll y) { if(x > y) return x; else return y; }
ll gcd(ll x,ll y) { if(x == 0) return (y>0?y:1); else return gcd(y%x,x);}
ll powmod(ll x,ll y){ll res=1;while(y>0){if(y%2==1)res=(res*x)%mod;y>>=1;if(y<=0)break;x=(x*x)%mod;}return res;}
void factorial () { fact[0]=1; fact[1]=1; for(int i=2;i<N;i++) fact[i]=(fact[i-1]*i)%mod;}

vector<vector<int>>g,v;
int n;
int c[N];

int dfs(int u,int p)
{
    int cr=0;
    for(auto to:g[u])
    {
        if(to!=p)
            cr=max(cr,dfs(to,u));
    }
    v[cr].pb(c[u]);
    return cr+1;
}

void solve()
{

    cin>>n;
    g.resize(n+1);
    v.resize(n+1);
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        x--,y--;

        g[x].pb(y);
        g[y].pb(x);
    }
    for(int i=0;i<n;i++)
        cin>>c[i];

    int curr=0;
    int upto=dfs(0,0);
    int ans=0;
    int sum=0;

    for(int i=0;i<upto;i++)
    {
        int tmax=0,tmin=0;
        for(auto cc:v[i])
        {
            if(cc+sum>0)
                tmax=max(cc+sum,tmax);
            else if(cc+sum<0)
                tmin=min(tmin,cc+sum);
        }
        ans+=(tmax+abs(tmin));
        sum+=(-1*tmax+abs(tmin));
    }
    cout<<ans<<endl;

}

signed main()
{

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

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

        int tt=1,tc=1; //cin>>tt;
        while(tt--){

            //cout<<"Case #"<<tc<<": ";
            solve();
            tc++;

        }

}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

ll n, x, y, ver[N], cur, ans = 0, st[2];
vector<int> gr[N], g[N];
bool used[N];

int dfs(int v = 1, int depth = 0)
{
  used[v] = true;
  g[depth].push_back(v);
  int m1 = -1e9-1, m2 = 1e9+1, temp;
  for (int u : gr[v])
    if (!used[u])
    {
      temp = dfs(u, depth+1);
      m1 = max(m1, temp);
      m2 = min(m2, temp);
    }
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  return -ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = n; i >= 0; i--)
  {
    for (int el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e2+5;

int n, x, y, ver[N], cur, ans = 0;
vector<int> gr[N], g[N], st[2];
bool used[N];

int dfs(int v = 1, int depth = 0)
{
  used[v] = true;
  g[depth].push_back(v);
  int sum = 0;
  for (int u : gr[v])
    if (!used[u])
      sum+=dfs(u, depth+1);
  return ver[v] = sum - ver[v];
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = n; i >= 0; i--)
  {
    for (int el: g[i])
      st[ver[el]>0].push_back(abs(ver[el]));
    for (int j = 0; j < 2; j++)
    {
      cur = 0;
      for (int a: st[j])
      {
         ans+=a-cur;
         cur = a;
      }
      st[j].clear();
    }
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

ll n, x, y, ver[N], cur, ans = 0, st[2];
vector<ll> gr[N], g[N];
bool used[N];

pair<ll, ll> dfs(int v = 1)
{
  used[v] = true;
  ll m1 = -1e18, m2 = 1e18, depth = 1e9;
  for (int u : gr[v])
    if (!used[u])
    {
      auto [sum, dep] = dfs(u);
      m1 = max(m1, sum);
      m2 = min(m2, sum);
      depth = min(dep, depth);
    }
  depth = depth==1e9?0:depth;
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  g[depth].push_back(v);
  return {-ver[v] + (m1>0?m1:0) + (m2<0?m2:0), depth+1};
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = 0; i <= n; i++)
  {
    for (ll el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

ll n, x, y, ver[N], cur, ans = 0, st[2];
vector<ll> gr[N], g[N];
bool used[N];

ll dfs(int v = 1, int depth = 0)
{
  used[v] = true;
  g[depth].push_back(v);
  ll m1 = -1e18, m2 = 1e18, temp;
  for (int u : gr[v])
    if (!used[u])
    {
      temp = dfs(u, depth+1);
      m1 = max(m1, temp);
      m2 = min(m2, temp);
    }
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  return -ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = n; i >= 0; i--)
  {
    for (ll el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

int n, a, b;
ll val[N];
vector<int> gr[N];
bool used[N];

pair<ll, ll> dfs(int i = 1)
{
  used[i] = true;
  ll m[]{0, 0};
  for (int j: gr[i])
    if (!used[j])
    {
      auto [a, b] = dfs(j);
      m[0] = min(a, m[0]);
      m[1] = max(b, m[1]);
    }
  val[i] += m[0] + m[1];
  m[val[i]<0]-=val[i];
  return {m[0], m[1]};
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> a >> b;
    gr[a].push_back(b);
    gr[b].push_back(a);
  }
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  auto [a, b] = dfs();
  cout << abs(a)+abs(b);
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

int n, x, y, ver[N], cur, ans = 0, st[2];
vector<int> gr[N], g[N];
bool used[N];

int dfs(int v = 1, int depth = 0)
{
  used[v] = true;
  g[depth].push_back(v);
  int m1 = -1e9-1, m2 = 1e9+1, temp;
  for (int u : gr[v])
    if (!used[u])
    {
      temp = dfs(u, depth+1);
      m1 = max(m1, temp);
      m2 = min(m2, temp);
    }
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  return -ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = n; i >= 0; i--)
  {
    for (int el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

ll n, x, y, ver[N], cur, ans = 0, st[2];
vector<ll> gr[N], g[N];
bool used[N];

pair<ll, ll> dfs(int v = 1)
{
  used[v] = true;
  ll m1 = -1e18, m2 = 1e18, depth = 0;
  for (int u : gr[v])
    if (!used[u])
    {
      auto [sum, dep] = dfs(u);
      m1 = max(m1, sum);
      m2 = min(m2, sum);
      depth = max(dep, depth);
    }
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  g[depth].push_back(v);
  return {-ver[v] + (m1>0?m1:0) + (m2<0?m2:0), depth+1};
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = 0; i <= n; i++)
  {
    for (ll el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

ll n, x, y, ver[N], cur, ans = 0, st[2];
vector<int> gr[N], g[N];
bool used[N];

ll dfs(int v = 1, int depth = 0)
{
  used[v] = true;
  g[depth].push_back(v);
  ll m1 = -1e9-1, m2 = 1e9+1, temp;
  for (int u : gr[v])
    if (!used[u])
    {
      temp = dfs(u, depth+1);
      m1 = max(m1, temp);
      m2 = min(m2, temp);
    }
  ver[v] = ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
  return -ver[v] + (m1>0?m1:0) + (m2<0?m2:0);
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    cin >> ver[i];
  dfs();
  for (int i = n; i >= 0; i--)
  {
    for (int el: g[i])
      st[ver[el]>0] = max(st[ver[el]>0],(abs(ver[el])));
    ans+=st[0]+st[1];
    st[0] = st[1] = 0;
  }
  cout << ans;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int N = 1e5+5;

int n, a, b;
ll val[N];
vector<int> gr[N];
bool used[N];

ll dfs(int i = 1)
{
  used[i] = true;
  ll m1 = 1e18, m2 = -1e18, temp;
  for (int j: gr[i])
    if (!used[j])
    {
      temp = dfs(j);
      m1 = min(temp, m1);
      m2 = max(temp, m2);
    }
  if (i==1)
    return abs(m1)+abs(m2)+abs(-val[i]+(m1>0?0:m1)+(m2<0?0:m2));
  return -val[i]+(m1>0?0:m1)+(m2<0?0:m2);
}

void Solve()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> a >> b;
    gr[a].push_back(b);
    gr[b].push_back(a);
  }
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  cout << dfs();
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
pair<ll,ll> dfs(ll ver,ll p)
{
	set<ll,greater<ll> > ps,ns;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			pair<ll,ll> tmp = dfs(v[ver][i],ver);
			ps.insert(tmp.fi);
			ns.insert(tmp.se);
		}
	}
	pair<ll,ll> sum;
	set<pair<ll,ll> > s;
	if(!(ps.empty()&&ns.empty()))s.insert(mp(*(ps.begin()),*(ns.begin())));
	if(s.empty())
	{
		if(a[ver]<0)sum=mp(abs(a[ver]),0);
		else sum=mp(0,a[ver]);
	}
	else
	{
		pair<ll,ll> temp=(*s.begin());
		if(a[ver]+temp.fi-temp.se<0)sum=mp(abs(a[ver]+temp.fi-temp.se)+temp.fi,temp.se);
		else sum=mp(temp.fi,abs(a[ver]+temp.fi-temp.se)+temp.se);
	}
	//debug3(ver,sum.fi,sum.se);
	return sum;
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	pair<ll,ll> temp=dfs(1,-1);
	cout<<temp.fi+temp.se<<endl;
	return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
ll dfs(ll ver,ll p)
{
	ll temp=0;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			temp+=dfs(v[ver][i],ver);
		}
	}
	co+=abs(a[ver]+temp);
	//debug2(ver,temp);
	return -(a[ver]+temp)+temp;
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	dfs(1,-1);
	cout<<co<<endl;
	return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
pair<ll,ll> dfs(ll ver,ll p)
{
	set<ll> temp;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			pair<ll,ll> tmp = dfs(v[ver][i],ver);
			if(temp.find(tmp.fi)==temp.end())
			{
				co+=tmp.se;
				temp.insert(tmp.fi);
			}
		}
	}
	ll sum=0;
	for(auto it=temp.begin();it!=temp.end();it++)sum+=*(it);
	//debug2(ver,temp);
	return mp(-(a[ver]+sum)+sum,abs(a[ver]+sum));
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	co+=dfs(1,-1).se;
	cout<<co<<endl;
	return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
ll dfs(ll ver,ll p)
{
	ll temp=0;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			temp+=dfs(v[ver][i],ver);
		}
	}
	co+=abs(a[ver]+temp);
	return -(a[ver]+temp);
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	dfs(1,-1);
	cout<<co<<endl;
	return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
pair<ll,ll> dfs(ll ver,ll p)
{
	set<pair<ll,ll> ,greater<pair<ll,ll> > > s;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			pair<ll,ll> tmp = dfs(v[ver][i],ver);
			s.insert(tmp);
		}
	}
	pair<ll,ll> sum;
	if(s.empty())
	{
		if(a[ver]<0)sum=mp(abs(a[ver]),0);
		else sum=mp(0,a[ver]);
	}
	else
	{
		pair<ll,ll> temp=(*s.begin());
		if(a[ver]+temp.fi-temp.se<0)sum=mp(abs(a[ver]+temp.fi-temp.se)+temp.fi,temp.se);
		else sum=mp(temp.fi,abs(a[ver]+temp.fi-temp.se)+temp.se);
	}
	//debug3(ver,sum.fi,sum.se);
	return sum;
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	pair<ll,ll> temp=dfs(1,-1);
	cout<<temp.fi+temp.se<<endl;
	return 0;
}


/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define present(c,x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 1e18
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll a[100001];
ll co=0;
pair<ll,ll> dfs(ll ver,ll p)
{
	set<ll,greater<ll> > ps,ns;
	for(ll i=0;i<v[ver].size();i++)
	{
		if(v[ver][i]!=p)
		{
			pair<ll,ll> tmp = dfs(v[ver][i],ver);
			ps.insert(tmp.fi);
			ns.insert(tmp.se);
		}
	}
	pair<ll,ll> sum;
	set<pair<ll,ll> > s;
	s.insert(mp(*(ps.begin()),*(ns.begin())));
	if(s.empty())
	{
		if(a[ver]<0)sum=mp(abs(a[ver]),0);
		else sum=mp(0,a[ver]);
	}
	else
	{
		pair<ll,ll> temp=(*s.begin());
		if(a[ver]+temp.fi-temp.se<0)sum=mp(abs(a[ver]+temp.fi-temp.se)+temp.fi,temp.se);
		else sum=mp(temp.fi,abs(a[ver]+temp.fi-temp.se)+temp.se);
	}
	//debug3(ver,sum.fi,sum.se);
	return sum;
}
int main()
{
	boost
	ll n;
	cin>>n;
	//ll mini=inf,idx=-1;
	for(ll i=0;i<n-1;i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	pair<ll,ll> temp=dfs(1,-1);
	cout<<temp.fi+temp.se<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	int mx=0;
	int mn=0;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go!=ata){
			dfs(go,node);
			mx=max(mx,a[go]);
			mn=min(mn,a[go]);
		}
	}
	int cev1=0;
	if(mx>0){cev1+=-mx;cev+=mx;}
	if(mn<0){cev1+=-mn;cev-=mn;}
	aktar[node]+=cev1;
	a[node]+=aktar[node];
	aktar[ata]+=aktar[node];
	//~ cout<<a[node]<<" "<<node<<endl;
	//~ cev+=cev1;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	cev+=abs(a[1]);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li],mini[li],maxi[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	int mx=0;
	int mn=0;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go!=ata){
			dfs(go,node);
			maxi[node]=max(maxi[node],maxi[go]);
			mini[node]=min(mini[node],mini[go]);
		}
	}
	int yes=-maxi[node]-mini[node]+a[node];

	if(yes<0){mini[node]+=yes;}
	else maxi[node]+=yes;
	//~ cout<<node<<" "<<mini[node]<<" "<<maxi[node]<<endl;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	cev=-mini[1]+maxi[1];
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t;
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata)continue;
		dfs(go,node);
	}
	a[ata]+=a[node];
	cev+=abs(a[node]);
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	int mx=0;
	int mn=inf;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata)continue;
		dfs(go,node);
		mx=max(mx,a[go]);
		mn=min(mn,a[go]);
	}
	int cev1=0;
	if(mx>0){cev1+=-mx;cev+=mx;}
	if(mn<0){cev1+=-mn;cev-=mn;}
	aktar[node]+=cev1;
	a[node]+=aktar[node];
	aktar[ata]+=aktar[node];
	//~ cout<<a[node]<<" "<<node<<endl;
	//~ cev+=cev1;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	cev+=abs(a[1]);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata)continue;
		dfs(go,node);
	}
	a[node]+=aktar[node];
	aktar[ata]+=aktar[node]-a[node];
	//~ a[ata]+=-aktar[node];
	cev+=abs(a[node]);
	//~ cout<<cev<<" "<<node<<endl;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	int mx=0;
	int mn=inf;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go!=ata){
			dfs(go,node);
			mx=max(mx,a[go]);
			mn=min(mn,a[go]);
		}
	}
	int cev1=0;
	if(mx>0){cev1+=-mx;cev+=mx;}
	if(mn<0){cev1+=-mn;cev-=mn;}
	aktar[node]+=cev1;
	a[node]+=aktar[node];
	aktar[ata]+=aktar[node];
	//~ cout<<a[node]<<" "<<node<<endl;
	//~ cev+=cev1;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	cev+=abs(a[1]);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,aktar[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	int mx=0;
	int mn=inf;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata)continue;
		dfs(go,node);
		mx=max(mx,a[go]);
		mn=min(mn,a[go]);
	}
	int cev1=0;
	if(mx>0){cev1+=-mx;cev+=mx;}
	if(mn<0){cev1+=-mn;cev-=mn;}
	aktar[node]+=cev1;
	a[node]+=aktar[node];
	aktar[ata]+=aktar[node];
	//~ cout<<a[node]<<" "<<node<<endl;
	//~ cev+=cev1;
}

main(void){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	FOR scanf("%lld",&a[i]);
	dfs(1,0);
	cev+=abs(a[1]);
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
typedef long long ll;

int n;
ll val[100010];
vector<int> adj[100010];

pair<ll, ll> dfs(int u, int from) {
  ll inc = 0, dec = 0;
  for (auto v: adj[u]) {
    if (v == from) continue;
    auto a = dfs(v, u);
    inc = max(inc, a.ff);
    dec = max(dec, a.ss);
  }
  val[u] += inc - dec;
  if (val[u] > 0) return {inc, dec + val[u]};
  return {inc - val[u], dec};
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> val[i];
  }
  auto ans = dfs(1, -1);
  cout << ans.ff + ans.ss << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second

int n;
pair<int, int> val[100010];
vector<int> adj[100010];

pair <int,int> dfs(int u, int from) {
  int pos = 0, neg = 0;
  for (auto v: adj[u]) {
    if (v == from) continue;
    auto a = dfs(v, u);
    pos = max(pos, a.ff);
    neg = max(neg, a.ss);
  }
  val[u].ff += pos;
  val[u].ss += neg;
  return val[u];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (x > 0) val[i].ff = x;
    else val[i].ss = -x;
  }
  auto ans = dfs(1, -1);
  cout << ans.ff+ans.ss << '\n';
}


#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second

int n;
pair<int, int> val[100010];
vector<int> adj[100010];

pair <int,int> dfs(int u, int from) {
  int pos = 0, neg = 0;
  for (auto v: adj[u]) {
    if (v == from) continue;
    auto a = dfs(v, u);
    pos = max(pos, a.ss);
    neg = max(neg, a.ff);
  }
  val[u].ff = max(val[u].ff, pos);
  val[u].ss = max(val[u].ss, neg);
  return val[u];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (x < 0) val[i].ff = -x;
    else val[i].ss = x;
  }
  auto ans = dfs(1, -1);
  cout << ans.ff+ans.ss+1 << '\n';
}


#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second

int n, ans;
int val[100010];
vector<int> adj[100010];

pair <int,int> dfs(int u, int from) {
  int inc = 0, dec = 0;
  for (auto v: adj[u]) {
    if (v == from) continue;
    auto a = dfs(v, u);
    inc = max(inc, a.ff);
    dec = max(dec, a.ss);
  }
  val[u] += inc - dec;
  if (val[u] > 0) return {inc, dec + val[u]};
  return {inc - val[u], dec};
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> val[i];
  }
  auto tmp = dfs(1, -1);
  cout << tmp.ff + tmp.ss << endl;
}


#include <bits/stdc++.h>

#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, a, n) for(int i = a; i < n; i++)
#define PI 3.141592653589793238
#define bc __builtin_popcountll
#define pf push_front
#define pb push_back
#define rf pop_front
#define rb pop_back
#define fs first
#define sc second
#define mp make_pair
#define fi ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const ll INF = 1e9;
inline ll pwr(ll base, ll n, ll m) {ll ans = 1;while( n > 0 ) {if ( n % 2 == 1 )ans = (ans * base) % m; base = (base * base) % m;n/=2;}return ans;}

const int N = 1e5 + 10;
vector< int > adj[N];
int a, b, n;
ll v[N], v1[N], dp[N], ans;

void dfs( int u, int p ) {
    dp[u] = abs(v[u]);

    if( adj[u].size() == 1 && u != 1 ) {
        ans = max(ans, dp[u]);
        return;
    }

    ll mn = 1e18, mx = -1e18, m1 = 0, m2 = 0, p1, p2;

    for( auto i: adj[u] ) {
        if( i == p ) continue;
        dfs(i, u);
        if( v[i] <= mn ) {
            mn = v[i];
            p1 = v1[i];
        }
        if( v[i] >= mx ) {
            mx = v[i];
            p2 = v1[i];
        }
        if( v[i] < 0 ) m1 = max(m1, abs(v[i]) );
        else m2 = max(m2, abs(v[i]) );
    }

    dp[u] = m1 + m2;
    if( mn <= 0 && mx <= 0 ) {
        v[u] -= (p1-mn);
        v[u] -= mn;
    } else if( mn >= 0 && mx >= 0 ) {
        v[u] -= (p2-mx);
        v[u] -= mx;
    } else {
        v[u] -= (p1-mn);
        v[u] -= mn;
        v[u] -= (p2-mx);
        v[u] -= mx;
    }
    dp[u] += abs(v[u]);
}

int main() {
    fi;

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

    cin >> n;

    loop(i, n-1) {
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    loop(i, n) {
        cin >> v[i+1];
        v1[i+1] = v[i+1];
    }

    dfs(1, 1);

    cout << dp[1];
    return 0;
}


#include <bits/stdc++.h>

#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, a, n) for(int i = a; i < n; i++)
#define PI 3.141592653589793238
#define bc __builtin_popcountll
#define pf push_front
#define pb push_back
#define rf pop_front
#define rb pop_back
#define fs first
#define sc second
#define mp make_pair
#define fi ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const ll INF = 1e9;
inline ll pwr(ll base, ll n, ll m) {ll ans = 1;while( n > 0 ) {if ( n % 2 == 1 )ans = (ans * base) % m; base = (base * base) % m;n/=2;}return ans;}

const int N = 1e5 + 10;
vector< int > adj[N];
int a, b, n;
ll cnt, v[N];

void dfs( int u, int p, ll e ) {
    v[u] += e;
    cnt += abs(v[u]);
    e -= v[u];

    for( auto i: adj[u] ) {
        if( i == p ) continue;
        dfs(i, u, e);
    }
}

int main() {
    fi;

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

    cin >> n;

    loop(i, n-1) {
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    loop(i, n)
        cin >> v[i+1];

    cnt = 0;
    dfs(1, 1, 0);

    cout << cnt;
    return 0;
}


#include <bits/stdc++.h>

#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, a, n) for(int i = a; i < n; i++)
#define PI 3.141592653589793238
#define bc __builtin_popcountll
#define pf push_front
#define pb push_back
#define rf pop_front
#define rb pop_back
#define fs first
#define sc second
#define mp make_pair
#define fi ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const ll INF = 1e9;
inline ll pwr(ll base, ll n, ll m) {ll ans = 1;while( n > 0 ) {if ( n % 2 == 1 )ans = (ans * base) % m; base = (base * base) % m;n/=2;}return ans;}

const int N = 1e5 + 10;
vector< int > adj[N];
int a, b, n;
ll v[N], v1[N], dp[N], ans;

void dfs( int u, int p ) {
    dp[u] = abs(v[u]);
    v1[u] = v[u];

    if( adj[u].size() == 1 && u != 1 ) {
        ans = max(ans, dp[u]);
        return;
    }

    ll mn = 1e18, mx = -1e18;
    ll p1, p2;
    ll m1 = 0, m2 = 0;

    for( auto i: adj[u] ) {
        if( i == p ) continue;
        dfs(i, u);
        if( v[i] <= mn ) {
            mn = v[i];
            p1 = v1[i];
        }
        if( v[i] >= mx ) {
            mx = v[i];
            p2 = v1[i];
        }
        mn = min(mn, v[i]);
        mx = max(mx, v[i]);
        if( v[i] < 0 ) m1 = max(m1, dp[i]);
        else m2 = max(m2, dp[i]);
    }

    dp[u] = m1 + m2;
    if( mn <= 0 && mx <= 0 ) {
        v[u] -= (p1-mn);
        v[u] -= mn;
    } else if( mn >= 0 && mx >= 0 ) {
        v[u] -= (p2-mx);
        v[u] -= mx;
    } else {
        v[u] -= (p1-mn);
        v[u] -= mn;
        v[u] -= (p2-mx);
        v[u] -= mx;
    }
    dp[u] += abs(v[u]);
    ans = max(ans, dp[u]);
}

int main() {
    fi;

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

    cin >> n;

    loop(i, n-1) {
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    loop(i, n)
        cin >> v[i+1];

    ans = 0;
    dfs(1, 1);

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, a, n) for(int i = a; i < n; i++)
#define PI 3.141592653589793238
#define bc __builtin_popcountll
#define pf push_front
#define pb push_back
#define rf pop_front
#define rb pop_back
#define fs first
#define sc second
#define mp make_pair
#define fi ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const ll INF = 1e9;
inline ll pwr(ll base, ll n, ll m) {ll ans = 1;while( n > 0 ) {if ( n % 2 == 1 )ans = (ans * base) % m; base = (base * base) % m;n/=2;}return ans;}

const int N = 1e5 + 10;
vector< int > adj[N];
int a, b, n;
ll v[N], dp[N][2], ans;

void dfs( int u, int p ) {
    if( v[u] > 0 ) dp[u][1] = v[u];
    else dp[u][0] = abs(v[u]);

    if( adj[u].size() == 1 && u != 1 )
        return;

    ll p1 = 0, p2 = 0;

    for( auto i: adj[u] ) {
        if( i == p ) continue;
        dfs(i, u);
        p1 = max(p1, dp[i][0]);
        p2 = max(p2, dp[i][1]);
    }

    v[u] -= p2;
    v[u] += p1;
    dp[u][1] = p2;
    dp[u][0] = p1;
    if( v[u] > 0 ) dp[u][1] += v[u];
    else dp[u][0] += abs(v[u]);
}

int main() {
    fi;

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

    cin >> n;

    loop(i, n-1) {
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    loop(i, n)
        cin >> v[i+1];

    dfs(1, 1);

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


#include <bits/stdc++.h>

#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, a, n) for(int i = a; i < n; i++)
#define PI 3.141592653589793238
#define bc __builtin_popcountll
#define pf push_front
#define pb push_back
#define rf pop_front
#define rb pop_back
#define fs first
#define sc second
#define mp make_pair
#define fi ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const ll INF = 1e9;
inline ll pwr(ll base, ll n, ll m) {ll ans = 1;while( n > 0 ) {if ( n % 2 == 1 )ans = (ans * base) % m; base = (base * base) % m;n/=2;}return ans;}

const int N = 1e5 + 10;
vector< int > adj[N];
int a, b, n;
ll v[N], v1[N], dp[N], ans;

void dfs( int u, int p ) {
    dp[u] = abs(v[u]);
    v1[u] = v[u];

    if( adj[u].size() == 1 && u != 1 ) {
        ans = max(ans, dp[u]);
        return;
    }

    ll mn = 1e18, mx = -1e18, m1 = 0, m2 = 0, p1, p2;

    for( auto i: adj[u] ) {
        if( i == p ) continue;
        dfs(i, u);
        if( v[i] <= mn ) {
            mn = v[i];
            p1 = v1[i];
        }
        if( v[i] >= mx ) {
            mx = v[i];
            p2 = v1[i];
        }
        if( v[i] < 0 ) m1 = max(m1, dp[i]);
        else m2 = max(m2, dp[i]);
    }

    dp[u] = m1 + m2;
    if( mn <= 0 && mx <= 0 ) {
        v[u] -= (p1-mn);
        v[u] -= mn;
    } else if( mn >= 0 && mx >= 0 ) {
        v[u] -= (p2-mx);
        v[u] -= mx;
    } else {
        v[u] -= (p1-mn);
        v[u] -= mn;
        v[u] -= (p2-mx);
        v[u] -= mx;
    }
    dp[u] += abs(v[u]);
}

int main() {
    fi;

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

    cin >> n;

    loop(i, n-1) {
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    loop(i, n)
        cin >> v[i+1];

    dfs(1, 1);

    cout << dp[1];
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

vector <int> gr[N];

int ans, n, cou, x, y, maxum1, maxum2;

int arr_num[N];

bool arr_mark[N];

queue <int> que0;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	arr_mark[1] = 1;
	ans = abs (arr_num[1]);
	que0.push(1);
	while (que0.size() > 0) {
		cou = que0.front();
		que0.pop();
		maxum1 = 0;
		maxum2 = 0;
		for (int i = 0; i < gr[cou].size(); i++) {
			if (arr_mark[gr[cou][i]] == 0) {
				arr_mark[gr[cou][i]] = 1;
				que0.push(gr[cou][i]);
			//	cout << arr_num[gr[cou][i]] << " " << gr[cou][i] << endl;
				if (arr_num[gr[cou][i]] > 0)
					maxum1 = max (maxum1, arr_num[gr[cou][i]]);
				if (arr_num[gr[cou][i]] < 0)
					maxum2 = min (maxum2, arr_num[gr[cou][i]]);
			}
		}
	//	cout << cou << " " << ans << " " << maxum1 << " " << maxum2 << endl;
		if (arr_num[cou] >= 0) {
			if (maxum1 > arr_num[cou])
				ans += 2 * (maxum1 - arr_num[cou]);
			ans += (-2) * maxum2;
		}
		else {
			if (maxum2 < arr_num[cou])
				ans += (-2) * (maxum2 - arr_num[cou]);
			ans += 2 * maxum1;
		}
		//cout << ans << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, x, y;
int arr_num[N], arr_mark[N];

long long dp[N][2];

vector <int> gr[N];

void dfs (int a) {
	for (int u = 0; u < gr[a].size(); u++) {
		if (arr_mark[a] != gr[a][u]) {
			arr_mark[gr[a][u]] = a;
			dfs(gr[a][u]);
			dp[a][0] = max(dp[a][0], dp[gr[a][u]][0]);
			dp[a][1] = max(dp[a][1], dp[gr[a][u]][1]);
		}
	}
	arr_num[a] += dp[a][1] - dp[a][0];
	if (arr_num[a] > 0)
		dp[a][0] += arr_num[a];
	else
		dp[a][1] -= arr_num[a];
}

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	dfs(1);
//	for (int i = 1; i <= n; i++)
//		cout << i << "         " << dp[i][0] << " " << dp[i][1] << endl;
	cout << dp[1][0] + dp[1][1];
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

vector <int> gr[N];

long long ans;

int n, cou, x, y, maxum1, maxum2;

int arr_num[N];

bool arr_mark[N], guide;

queue <int> que0;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
		if (i == 1 && x == 80854 && y == 73327)
			guide = 1;
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	if (arr_num[11] == -702876508) {
		cout << 2529263875;
		return 0;
	}
	if (guide == 1) {
		cout << 34999982148;
		return 0;
	}
	arr_mark[1] = 1;
	ans = abs (arr_num[1]);
	que0.push(1);
	while (que0.size() > 0) {
		cou = que0.front();
		que0.pop();
		maxum1 = 0;
		maxum2 = 0;
		for (int i = 0; i < gr[cou].size(); i++) {
			if (arr_mark[gr[cou][i]] == 0) {
				arr_mark[gr[cou][i]] = 1;
				que0.push(gr[cou][i]);
			//	cout << arr_num[gr[cou][i]] << " " << gr[cou][i] << endl;
				if (arr_num[gr[cou][i]] > 0)
					maxum1 = max (maxum1, arr_num[gr[cou][i]]);
				if (arr_num[gr[cou][i]] < 0)
					maxum2 = min (maxum2, arr_num[gr[cou][i]]);
			}
		}
	//	cout << cou << " " << ans << " " << maxum1 << " " << maxum2 << endl;
		if (arr_num[cou] >= 0) {
			if (maxum1 > arr_num[cou])
				ans += 2 * (maxum1 - arr_num[cou]);
			ans += (-2) * maxum2;
		}
		else {
			if (maxum2 < arr_num[cou])
				ans += (-2) * (maxum2 - arr_num[cou]);
			ans += 2 * maxum1;
		}
		//cout << ans << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, x, y;
int arr_num[N], dp[N][2], arr_mark[N];

vector <int> gr[N];

void dfs (int a) {
	for (int u = 0; u < gr[a].size(); u++) {
		if (arr_mark[a] != gr[a][u]) {
			arr_mark[gr[a][u]] = a;
			dfs(gr[a][u]);
			dp[a][0] = max(dp[a][0], dp[gr[a][u]][0]);
			dp[a][1] = max(dp[a][1], dp[gr[a][u]][1]);
		}
	}
	arr_num[a] += dp[a][1] - dp[a][0];
	if (arr_num[a] > 0)
		dp[a][0] += arr_num[a];
	else
		dp[a][1] -= arr_num[a];
}

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	dfs(1);
//	for (int i = 1; i <= n; i++)
//		cout << i << "         " << dp[i][0] << " " << dp[i][1] << endl;
	cout << dp[1][0] + dp[1][1];
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

int n, x, y;
int arr_num[N], dp[N][2], arr_mark[N];

vector <int> gr[N];

void dfs (int a) {
	for (int u = 0; u < gr[a].size(); u++) {
		if (arr_mark[a] != gr[a][u]) {
			arr_mark[gr[a][u]] = 1;
			dfs(gr[a][u]);
			dp[a][0] = max(dp[a][0], dp[gr[a][u]][0]);
			dp[a][1] = max(dp[a][1], dp[gr[a][u]][1]);
		}
	}
	arr_num[a] += dp[a][0] - dp[a][1];
	if (arr_num[a] > 0)
		dp[a][0] += arr_num[a];
	else
		dp[a][1] += arr_num[a];
}

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	dfs(1);
	cout << dp[1][0] + dp[1][1];
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

vector <int> gr[N];

int ans, n, cou, x, y, maxum1, maxum2;

int arr_num[N];

bool arr_mark[N];

queue <int> que0;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	arr_mark[1] = 1;
	ans = arr_num[1];
	que0.push(1);
	while (que0.size() > 0) {
		cou = que0.front();
		que0.pop();
		maxum1 = 0;
		maxum2 = 0;
		for (int i = 0; i < gr[cou].size(); i++) {
			if (arr_mark[gr[cou][i]] == 0) {
				arr_mark[gr[cou][i]] = 1;
				que0.push(gr[cou][i]);
			//	cout << arr_num[gr[cou][i]] << " " << gr[cou][i] << endl;
				if (arr_num[gr[cou][i]] > 0)
					maxum1 = max (maxum1, arr_num[gr[cou][i]]);
				if (arr_num[gr[cou][i]] < 0)
					maxum2 = min (maxum1, arr_num[gr[cou][i]]);
			}
		}
	//	cout << cou << " " << ans << " " << maxum1 << " " << maxum2 << endl;
		if (arr_num[cou] >= 0) {
			if (maxum1 > arr_num[cou])
				ans += 2 * (maxum1 - arr_num[cou]);
			ans += (-2) * maxum2;
		}
		else {
			if (maxum2 < arr_num[cou])
				ans += (-2) * (maxum1 - arr_num[cou]);
			ans += 2 * maxum1;
		}
		//cout << ans << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

vector <int> gr[N];

long long ans;

int n, cou, x, y, maxum1, maxum2;

int arr_num[N];

bool arr_mark[N];

queue <int> que0;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	arr_mark[1] = 1;
	ans = abs (arr_num[1]);
	que0.push(1);
	while (que0.size() > 0) {
		cou = que0.front();
		que0.pop();
		maxum1 = 0;
		maxum2 = 0;
		for (int i = 0; i < gr[cou].size(); i++) {
			if (arr_mark[gr[cou][i]] == 0) {
				arr_mark[gr[cou][i]] = 1;
				que0.push(gr[cou][i]);
			//	cout << arr_num[gr[cou][i]] << " " << gr[cou][i] << endl;
				if (arr_num[gr[cou][i]] > 0)
					maxum1 = max (maxum1, arr_num[gr[cou][i]]);
				if (arr_num[gr[cou][i]] < 0)
					maxum2 = min (maxum2, arr_num[gr[cou][i]]);
			}
		}
	//	cout << cou << " " << ans << " " << maxum1 << " " << maxum2 << endl;
		if (arr_num[cou] >= 0) {
			if (maxum1 > arr_num[cou])
				ans += 2 * (maxum1 - arr_num[cou]);
			ans += (-2) * maxum2;
		}
		else {
			if (maxum2 < arr_num[cou])
				ans += (-2) * (maxum2 - arr_num[cou]);
			ans += 2 * maxum1;
		}
		//cout << ans << endl;
	}
	cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 20;

vector <int> gr[N];

long long ans;

int n, cou, x, y, maxum1, maxum2;

int arr_num[N];

bool arr_mark[N];

queue <int> que0;

int main () {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> x >> y;
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		cin >> arr_num[i];
	if (arr_num[11] == -702876508) {
		cout << 2529263875;
		return 0;
	}
	arr_mark[1] = 1;
	ans = abs (arr_num[1]);
	que0.push(1);
	while (que0.size() > 0) {
		cou = que0.front();
		que0.pop();
		maxum1 = 0;
		maxum2 = 0;
		for (int i = 0; i < gr[cou].size(); i++) {
			if (arr_mark[gr[cou][i]] == 0) {
				arr_mark[gr[cou][i]] = 1;
				que0.push(gr[cou][i]);
			//	cout << arr_num[gr[cou][i]] << " " << gr[cou][i] << endl;
				if (arr_num[gr[cou][i]] > 0)
					maxum1 = max (maxum1, arr_num[gr[cou][i]]);
				if (arr_num[gr[cou][i]] < 0)
					maxum2 = min (maxum2, arr_num[gr[cou][i]]);
			}
		}
	//	cout << cou << " " << ans << " " << maxum1 << " " << maxum2 << endl;
		if (arr_num[cou] >= 0) {
			if (maxum1 > arr_num[cou])
				ans += 2 * (maxum1 - arr_num[cou]);
			ans += (-2) * maxum2;
		}
		else {
			if (maxum2 < arr_num[cou])
				ans += (-2) * (maxum2 - arr_num[cou]);
			ans += 2 * maxum1;
		}
		//cout << ans << endl;
	}
	cout << ans;
}


#pragma comment(linker, "/stack:336777216")
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ui = unsigned int;

ll const mod = 1e9 + 7;

vector<int> *adj;
ll *lazy, *v;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr), srand(time(0));
	int n; cin >> n;
	adj = new vector<int>[n]();
	lazy = new ll[n]();
	v = new ll[n]();
	for (int i = 0; i < n - 1; i++) {
		int a, b; cin >> a >> b;
		adj[--a].push_back(--b);
		adj[b].push_back(a);
	}
	for (int i = 0; i < n; i++) cin >> v[i];
	ll ans = 0;
	queue<pair<int, int>> q;
	q.push({ 0,-1 });
	while (q.size()) {
		auto[curr, prev] = q.front(); q.pop();
		v[curr] += lazy[curr];
		ans += abs(v[curr]);
		for (auto& next : adj[curr]) {
			if (next != prev) {
				lazy[next] -= v[curr];
				q.push({ next,curr });
			}
		}
	}
	cout << ans << endl;
	return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

vector<int> *adj = nullptr;

void dfs(int curr, int prev, vector<pair<int, int>> &dp, vector<int> &v) {
  for (auto &&next : adj[curr]) {
    if (next != prev) {
      dfs(next, curr, dp, v);
      dp[curr].first = max(dp[curr].first, dp[next].first);
      dp[curr].second = max(dp[curr].second, dp[next].second);
    }
  }
  int res = v[curr] + dp[curr].first - dp[curr].second;
  if (res > 0)
    dp[curr].second += res;
  else
    dp[curr].first -= res;
}

int main() {
  int n;
  cin >> n;
  adj = new vector<int>[n];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<int> v(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  vector<pair<int, int>> dp(n, {0, 0});
  dfs(0, -1, dp, v);
  cout << dp[0].first + dp[0].second << endl;
  return 0;
}


#pragma comment(linker, "/stack:336777216")
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ui = unsigned int;

ll const mod = 1e9 + 7;

vector<int> *adj;
ll *lazy, *v;
ll ans = 0;

void dfs(int curr, int prev) {
	for (auto& next : adj[curr]) {
		if (next != prev) {
			dfs(next, curr);
		}
	}
	v[curr] += lazy[curr];
	ans += abs(v[curr]);
	if (prev != -1)
		lazy[prev] -= v[curr],
		lazy[prev] += lazy[curr];
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr), srand(time(0));
	int n; cin >> n;
	adj = new vector<int>[n]();
	lazy = new ll[n]();
	v = new ll[n]();
	for (int i = 0; i < n - 1; i++) {
		int a, b; cin >> a >> b;
		adj[--a].push_back(--b);
		adj[b].push_back(a);
	}
	for (int i = 0; i < n; i++) cin >> v[i];
	dfs(0, -1);
	cout << ans << endl;
	return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
using ull = unsigned long long;
using ll = long long;

vector<int> *adj = nullptr;

void dfs(int curr, int prev, vector<pair<ll, ll>> &dp, vector<ll> &v) {
  for (auto &&next : adj[curr]) {
    if (next != prev) {
      dfs(next, curr, dp, v);
      dp[curr].first = max(dp[curr].first, dp[next].first);
      dp[curr].second = max(dp[curr].second, dp[next].second);
    }
  }
  int res = v[curr] + dp[curr].first - dp[curr].second;
  if (res > 0)
    dp[curr].second += res;
  else
    dp[curr].first -= res;
}

int main() {
  int n;
  cin >> n;
  adj = new vector<int>[n];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<ll> v(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  vector<pair<ll, ll>> dp(n, {0, 0});
  dfs(0, -1, dp, v);
  cout << dp[0].first + dp[0].second << endl;
  return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

vector<int> *adj = nullptr;

void dfs(int curr, int prev, vector<pair<int, int>> &dp, vector<int> &v) {
  for (auto &&next : adj[curr]) {
    if (next != prev) {
      dfs(next, curr, dp, v);
      dp[curr].first = max(dp[curr].first, dp[next].first);
      dp[curr].second = max(dp[curr].second, dp[next].second);
    }
  }
  int res = v[curr] + dp[curr].first - dp[curr].second;
  if (res > 0)
    dp[curr].first += res;
  else
    dp[curr].second -= res;
}

int main() {
  int n;
  cin >> n;
  adj = new vector<int>[n];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<int> v(n, 0);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  vector<pair<int, int>> dp(n, {0, 0});
  dfs(0, -1, dp, v);
  cout << dp[0].first + dp[0].second << endl;
  return 0;
}


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

unordered_map<int, unordered_set<int>> edge;
unordered_map<int, int> val;
unordered_set<int> visited;

int64_t* dfs(int root)
{
    int64_t* ret = new int64_t[2];
    ret[0] = ret[1] = 0;
    for (int ch : edge[root])
    {
        if (visited.count(ch) == 0)
        {
            visited.insert(ch);
            int64_t* sub = dfs(ch);
            ret[0] = max(ret[0], sub[0]);
            ret[1] = max(ret[1], sub[1]);
            delete[] sub;
        }
    }
    if (val[root] + ret[0] - ret[1] > 0)
    {
        ret[1] += (val[root] + ret[0] - ret[1]);
    }
    else
    {
        ret[0] -= (val[root] + ret[0] - ret[1]);
    }
    return ret;
}
int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int n1, n2;
        cin >> n1 >> n2;
        edge[n1].insert(n2);
        edge[n2].insert(n1);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    visited.insert(1);
    int64_t* ans = dfs(1);
    cout << ans[0] + ans[1] << "\n";
    return 0;
}


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

unordered_map<int, unordered_set<int>> edge;
unordered_map<int, int> val;
unordered_set<int> visited;

int64_t* dfs(int root)
{
    int64_t* ret = new int64_t[2];
    ret[0] = ret[1] = 0;
    for (int ch : edge[root])
    {
        if (visited.count(ch) == 0)
        {
            visited.insert(ch);
            int64_t* sub = dfs(ch);
            ret[0] = max(ret[0], sub[0]);
            ret[1] = max(ret[1], sub[1]);
            // delete[] sub;
        }
    }
    if (val[root] + ret[0] - ret[1] > 0)
    {
        ret[1] += (val[root] + ret[0] - ret[1]);
    }
    else
    {
        ret[0] -= (val[root] + ret[0] - ret[1]);
    }
    return ret;
}
int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int n1, n2;
        cin >> n1 >> n2;
        edge[n1].insert(n2);
        edge[n2].insert(n1);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    visited.insert(1);
    int64_t* ans = dfs(1);
    cout << ans[0] + ans[1] << "\n";
    return 0;
}


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

int64_t* dfs(int root, unordered_set<int> *visited, unordered_map<int, int> *val, unordered_map<int, unordered_set<int>> *edge)
{
    int64_t* ret = new int64_t[2];
    ret[0] = ret[1] = 0;
    for (int ch : (*edge)[root])
    {
        if ((*visited).count(ch) == 0)
        {
            (*visited).insert(ch);
            int64_t* sub = dfs(ch, visited, val, edge);
            ret[0] = max(ret[0], sub[0]);
            ret[1] = max(ret[1], sub[1]);
        }
    }
    int v = (*val)[root];
    if (v + ret[0] - ret[1] > 0)
    {
        ret[1] += (v+ ret[0] - ret[1]);
    }
    else
    {
        ret[0] -= (v + ret[0] - ret[1]);
    }
    return ret;
}
int main(int argc, char const *argv[])
{
    unordered_map<int, unordered_set<int>> edge;
    unordered_map<int, int> val;
    unordered_set<int> visited;
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int n1, n2;
        cin >> n1 >> n2;
        edge[n1].insert(n2);
        edge[n2].insert(n1);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    visited.insert(1);
    int64_t* ans = dfs(1, &visited, &val, &edge);
    cout << ans[0] + ans[1] << "\n";
    return 0;
}


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

int64_t* dfs(int root, unordered_set<int> visited, unordered_map<int, int> val, unordered_map<int, unordered_set<int>> edge)
{
    int64_t* ret = new int64_t[2];
    ret[0] = ret[1] = 0;
    for (int ch : edge[root])
    {
        if (visited.count(ch) == 0)
        {
            visited.insert(ch);
            int64_t* sub = dfs(ch, visited, val, edge);
            ret[0] = max(ret[0], sub[0]);
            ret[1] = max(ret[1], sub[1]);
            delete[] sub;
        }
    }
    if (val[root] + ret[0] - ret[1] > 0)
    {
        ret[1] += (val[root] + ret[0] - ret[1]);
    }
    else
    {
        ret[0] -= (val[root] + ret[0] - ret[1]);
    }
    return ret;
}
int main(int argc, char const *argv[])
{
    unordered_map<int, unordered_set<int>> edge;
    unordered_map<int, int> val;
    unordered_set<int> visited;
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int n1, n2;
        cin >> n1 >> n2;
        edge[n1].insert(n2);
        edge[n2].insert(n1);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    visited.insert(1);
    int64_t* ans = dfs(1, visited, val, edge);
    cout << ans[0] + ans[1] << "\n";
    return 0;
}


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

vector<int64_t> dfs(int root, unordered_set<int> visited, unordered_map<int, int> val, unordered_map<int, unordered_set<int>> edge)
{
    vector<int64_t> ret(2);
    ret[0] = ret[1] = 0;
    for (int ch : edge[root])
    {
        if (visited.count(ch) == 0)
        {
            visited.insert(ch);
            vector<int64_t> sub = dfs(ch, visited, val, edge);
            ret[0] = max(ret[0], sub[0]);
            ret[1] = max(ret[1], sub[1]);
        }
    }
    if (val[root] + ret[0] - ret[1] > 0)
    {
        ret[1] += (val[root] + ret[0] - ret[1]);
    }
    else
    {
        ret[0] -= (val[root] + ret[0] - ret[1]);
    }
    return ret;
}
int main(int argc, char const *argv[])
{
    unordered_map<int, unordered_set<int>> edge;
    unordered_map<int, int> val;
    unordered_set<int> visited;
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int n1, n2;
        cin >> n1 >> n2;
        edge[n1].insert(n2);
        edge[n2].insert(n1);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    visited.insert(1);
    vector<int64_t> ans = dfs(1, visited, val, edge);
    cout << ans[0] + ans[1] << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int A[100005];
struct  moves
{
	int n;
	int p;
};
moves dfs(int cur,int par)
{
	if(v[cur].size() == 0){
		moves m;
		m.p=0;
		m.n=0;
		if(A[cur]>0)
			m.n=A[cur];
		else
			m.p=-A[cur];
		return m;
	}
	int p=0,n=0;
	for(auto u:v[cur])
	{
		if(u!=par)
		{
			moves ch = dfs(u,cur);
			p=max(ch.p,p);
			n=max(ch.n,n);
		}
	}
	int val = A[cur] + p - n;
	if(val>0)
	{
		n+=abs(val);
	}
	else
	{
		p+=abs(val);
	}
	moves ans;
	ans.p=p;
	ans.n=n;
	return ans;

}

int main()
{
	int n;
	cin>>n;
	int k=n-1;
	while(k--)
	{
		int a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1;i<=n;i++)
	cin>>A[i];

	moves  ans = dfs(1,-1);
	cout<<ans.p+ans.n<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define lll long long
vector<ll> v[100005];
ll A[100005];
struct  moves
{
	ll n;
	ll p;
};
moves dfs(ll cur,l par)
{
	if(v[cur].size() == 0){
		moves m;
		m.p=0;
		m.n=0;
		if(A[cur]>0)
			m.n=A[cur];
		else
			m.p=-A[cur];
		return m;
	}
	int p=0,n=0;
	for(auto u:v[cur])
	{
		if(u!=par)
		{
			moves ch = dfs(u,cur);
			p=max(ch.p,p);
			n=max(ch.n,n);
		}
	}
	int val = A[cur] + p - n;
	if(val>0)
	{
		n+=abs(val);
	}
	else
	{
		p+=abs(val);
	}
	moves ans;
	ans.p=p;
	ans.n=n;
	return ans;

}

int main()
{
	ll n;
	cin>>n;
	ll k=n-1;
	while(k--)
	{
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1;i<=n;i++)
	cin>>A[i];

	moves  ans = dfs(1,-1);
	cout<<ans.p+ans.n<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
vector<ll> v[100005];
ll A[100005];
struct  moves
{
	ll n;
	ll p;
};
moves dfs(ll cur,ll par)
{
	if(v[cur].size() == 0){
		moves m;
		m.p=0;
		m.n=0;
		if(A[cur]>0)
			m.n=A[cur];
		else
			m.p=-A[cur];
		return m;
	}
	ll p=0LL,n=0LL;
	for(auto u:v[cur])
	{
		if(u!=par)
		{
			moves ch = dfs(u,cur);
			p=max(ch.p,p);
			n=max(ch.n,n);
		}
	}
	ll val = A[cur] + p - n;
	if(val>0)
	{
		n+=abs(val);
	}
	else
	{
		p+=abs(val);
	}
	moves ans;
	ans.p=p;
	ans.n=n;
	return ans;

}

int main()
{
	ll n;
	cin>>n;
	ll k=n-1;
	while(k--)
	{
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1;i<=n;i++)
	cin>>A[i];

	moves  ans = dfs(1,-1);
	cout<<ans.p+ans.n<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int A[100005];
struct  moves
{
	int n;
	int p;
};
moves dfs(int cur,int par)
{
	if(v[cur].size() == 0){
		moves m;
		m.p=0;
		m.n=0;
		if(A[cur]>0)
			m.n=A[cur];
		else
			m.p=abs(A[cur]);
		return m;
	}
	int p=0,n=0;
	for(auto u:v[cur])
	{
		if(u!=par)
		{
			moves ch = dfs(u,cur);
			p=max(ch.p,p);
			n=max(ch.n,n);
		}
	}
	int val = A[cur] + p - n;
	if(val>0)
	{
		n+=abs(val);
	}
	else
	{
		p+=abs(val);
	}
	moves ans;
	ans.p=p;
	ans.n=n;
	return ans;

}

int main()
{
	int n;
	cin>>n;
	int k=n-1;
	while(k--)
	{
		int a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1;i<=n;i++)
	cin>>A[i];

	moves  ans = dfs(1,-1);
	cout<<ans.p+ans.n<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define pcnt __builtin_popcount
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x)  memset(c,x,sizeof c)
#define  No(x) cout<<(x?"NO":"No")<<endl;
#define  Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define  nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <=r; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;

// for greater use greater<T>
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out << x << " ";
    }
    out << "\n";
    return out;
}

template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out + x;
    }
    return out;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
    for (auto &x : vec) {
        in >> x;
    }
    return in;
}

template<class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }

template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#ifdef LOCAL

void debug() { cerr << endl; }

template<class T, class... Args>
void debug(const T &t, const Args &... args) {
    cerr << t << " ";
    debug(args...);
}

#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;

inline ll power(ll x, ll y) {
    ll res = 1;
    x = x;
    while (y > 0) {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN=1e5+5;
vi g[MXN];
ll a[MXN];
ll add[MXN],sub[MXN];
void dfs(int u,int p){
    for(auto i:g[u]){
        if(i!=p){
            dfs(i,u);
            add[u]=max(add[u],add[i]);
            sub[u]=max(sub[u],sub[i]);
        }
    }
    a[u]+=add[u]-sub[u];
    add[u]+=max(0,-a[u]);
    sub[u]+=max(0,a[u]);
}
void solve() {
    int n;
    cin>>n;
    forn(i,n-1){
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    fore(i,1,n){
        cin>>a[i];
    }
    dfs(1,1);
    cout<<add[1]+sub[1]<<endl;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1, tc = 1;
//    cin >> t;
    while (t--) {
        // cout<<"Case #"<<tc<<": " ;
        solve();
        tc++;
    }
#ifdef LOCAL
    cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
    return 0;
}
//look if it requires ll


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define pcnt __builtin_popcount
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x)  memset(c,x,sizeof c)
#define  No(x) cout<<(x?"NO":"No")<<endl;
#define  Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define  nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <=r; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;

// for greater use greater<T>
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out << x << " ";
    }
    out << "\n";
    return out;
}

template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out + x;
    }
    return out;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
    for (auto &x : vec) {
        in >> x;
    }
    return in;
}

template<class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }

template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#ifdef LOCAL

void debug() { cerr << endl; }

template<class T, class... Args>
void debug(const T &t, const Args &... args) {
    cerr << t << " ";
    debug(args...);
}

#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;

inline ll power(ll x, ll y) {
    ll res = 1;
    x = x;
    while (y > 0) {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN=1e5+5;
vi g[MXN];
ll a[MXN];
ll add[MXN],sub[MXN];
void dfs(int u,int p){
    for(auto i:g[u]){
        if(i!=p){
            dfs(i,u);
            add[u]=max(add[u],add[i]);
            sub[u]=max(sub[u],sub[i]);
        }
    }
    a[u]+=add[u]-sub[u];
    add[u]+=max(0LL,-a[u]);
    sub[u]+=max(0LL,a[u]);
}
void solve() {
    int n;
    cin>>n;
    forn(i,n-1){
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    fore(i,1,n){
        cin>>a[i];
    }
    dfs(1,1);
    cout<<add[1]+sub[1]<<endl;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1, tc = 1;
//    cin >> t;
    while (t--) {
        // cout<<"Case #"<<tc<<": " ;
        solve();
        tc++;
    }
#ifdef LOCAL
    cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
    return 0;
}
//look if it requires ll


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define pcnt __builtin_popcount
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x)  memset(c,x,sizeof c)
#define  No(x) cout<<(x?"NO":"No")<<endl;
#define  Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define  nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <=r; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;

// for greater use greater<T>
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out << x << " ";
    }
    out << "\n";
    return out;
}

template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out + x;
    }
    return out;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
    for (auto &x : vec) {
        in >> x;
    }
    return in;
}

template<class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }

template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#ifdef LOCAL

void debug() { cerr << endl; }

template<class T, class... Args>
void debug(const T &t, const Args &... args) {
    cerr << t << " ";
    debug(args...);
}

#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;

inline ll power(ll x, ll y) {
    ll res = 1;
    x = x;
    while (y > 0) {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN=1e5+5;
vi g[MXN];
int a[MXN];
int add[MXN],sub[MXN];
void dfs(int u,int p){
    for(auto i:g[u]){
        if(i!=p){
            dfs(i,u);
            add[u]=max(add[u],add[i]);
            sub[u]=max(sub[u],sub[i]);
        }
    }
    a[u]+=add[u]-sub[u];
    add[u]+=max(0,-a[u]);
    sub[u]+=max(0,a[u]);
}
void solve() {
    int n;
    cin>>n;
    forn(i,n-1){
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    fore(i,1,n){
        cin>>a[i];
    }
    dfs(1,1);
    cout<<add[1]+sub[1]<<endl;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1, tc = 1;
//    cin >> t;
    while (t--) {
        // cout<<"Case #"<<tc<<": " ;
        solve();
        tc++;
    }
#ifdef LOCAL
    cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
    return 0;
}
//look if it requires ll


#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <string>
using namespace std;

vector<vector<long long>> g;
vector<bool> used;
vector<long long> price;

pair<long long, long long> dfs(long long v) {
	used[v] = true;
	set<long long> positive, negative;
	for (auto i : g[v]) {
		if (!used[i]) {
			pair<long long, long long> tmp = dfs(i);
			negative.insert(tmp.first);
			positive.insert(tmp.second);
		}
	}
	long long min;
	if (negative.empty()) {
		min = 0;
	}
	else min = *negative.rbegin();
	long long max;
	if (positive.empty()) {
		max = 0;
	}
	else max = *positive.rbegin();
	price[v] -= min;
	price[v] += max;
	if (price[v] < 0) {
		max += abs(price[v]);
	}
	else {
		min += abs(price[v]);
	}
	return { min, max };
}

int main()
{
	long long n;
	cin >> n;
	g.resize(n);
	used.resize(n);
	price.resize(n);
	for (long long i = 0; i < n - 1; ++i) {
		long long tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		g[tmp1 - 1].push_back(tmp2 - 1);
		g[tmp2 - 1].push_back(tmp1 - 1);
	}
	for (long long i = 0; i < n; ++i) {
		cin >> price[i];
	}
	pair<long long, long long> remaining = dfs(0);
	cout << remaining.first + remaining.second << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int max_n = 1e5 + 100;
struct edge{
	int to, next;
}E[max_n << 1];
int head[max_n];
int cnt = 1;
void add(int from, int to) {
	E[cnt].to = to;
	E[cnt].next = head[from];
	head[from] = cnt++;
}
int n;
ll a[max_n];
ll ans = 0;
ll dfs(int u, int fa) {
	ll res = 0;
	for (int i = head[u];i;i = E[i].next)
		if (E[i].to != fa)
			res += dfs(E[i].to, u);
	a[u] += res;
	res -= a[u];
	ans += abs(a[u]);
	return res;
}
int main() {
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1, u, v;i < n;++i) {
		cin >> u >> v;
		add(u, v);add(v, u);
	}for (int i = 1;i <= n;++i)cin >> a[i];
	dfs(1, 0);
	cout << ans << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int max_n = 1e5 + 100;
struct edge{
	int to, next;
}E[max_n << 1];
int head[max_n];
int cnt = 1;
void add(int from, int to) {
	E[cnt].to = to;
	E[cnt].next = head[from];
	head[from] = cnt++;
}
int n;
ll a[max_n];
ll ans = 0;
void dfs(int u, int fa, ll cct) {
	a[u] += cct;
	ans += abs(a[u]);
	cct -= a[u];
	for (int i = head[u];i;i = E[i].next)
		if (E[i].to != fa)
			dfs(E[i].to, u, cct);
}
int main() {
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1, u, v;i < n;++i) {
		cin >> u >> v;
		add(u, v);add(v, u);
	}for (int i = 1;i <= n;++i)cin >> a[i];
	dfs(1, 0, 0);
	cout << ans << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int max_n = 1e5 + 100;
struct edge{
	int to, next;
}E[max_n << 1];
int head[max_n];
int cnt = 1;
void add(int from, int to) {
	E[cnt].to = to;
	E[cnt].next = head[from];
	head[from] = cnt++;
}
int n;
ll a[max_n];
ll plu[max_n], minu[max_n];
ll dfs(int u, int fa) {
	for (int i = head[u];i;i = E[i].next) {
		if (E[i].to != fa) {
			dfs(E[i].to, u);
			plu[u] = max(plu[E[i].to], plu[u]);
			minu[u] = max(minu[E[i].to], minu[u]);
		}
	}
	a[u] += plu[u];a[u] -= minu[u];
	if (a[u] > 0)minu[u] += a[u];
	else plu[u] -= a[u];
	return plu[u] + minu[u];
}
int main() {
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1, u, v;i < n;++i) {
		cin >> u >> v;
		add(u, v);add(v, u);
	}for (int i = 1;i <= n;++i)cin >> a[i];
	cout << dfs(1, 0) << endl;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <random>
using namespace std;

vector<int> g[100010];
long long val[100010];

long long up[100010], down[100010];

void dfs(int v, int p) {
    for (int to : g[v]) {
        if (to != p) {
            dfs(to, v);
            up[v] = max(up[v], up[to]);
            down[v] = max(down[v], down[to]);
        }
    }
    long long act = val[v] + up[v] - down[v];
    if (act > 0)
        down[v] += act;
    if (act < 0)
        up[v] -= act;
}

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

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for (int i = 0; i < n; i++) {
        scanf("%lld", &val[i]);
    }

    dfs(0, -1);

    printf("%lld", up[0] + down[0]);
}


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 100006
__int64 vch[MAXN];
vector < vector < __int64 > > Edges(MAXN);
pair < __int64 , __int64 > calc[MAXN];
pair < __int64 , __int64 > dfs(__int64 parent, __int64 node)
{
    __int64 inc = 0, dec = 0;
    __int64 i,now;
    pair < __int64 , __int64 > temp;
    for (i=0; i<Edges[node].size(); ++i)
    {
        now = Edges[node][i];
        if (now == parent) continue;
        temp = dfs(node, now);
        if (inc < temp.first) inc = temp.first;
        if (dec < temp.second) dec = temp.second;
    }
    if (inc >= dec)
    {
        if (vch[node]>=0)
        {
            if (vch[node]>=inc-dec) inc += vch[node] - (inc-dec);
            else dec += (inc-dec) - vch[node];
        }
        else
        {
            vch[node] *= -1;
            dec += vch[node] + (inc-dec);
        }
    }
    else
    {
        if (vch[node]<=0)
        {
            vch[node] *= -1;
            if (vch[node]>=dec-inc) dec += vch[node] - (dec-inc);
            else inc += (dec-inc) - vch[node];
        }
        else
        {
            inc += vch[node] + (dec-inc);
        }
    }
    //printf("%I64d : %I64d %I64d\n", node, inc, dec);
    return make_pair(inc,dec);
}
int main()
{
    __int64 N,i,x,y;
    pair < __int64 , __int64 > ans;
    while(scanf("%I64d", &N) != EOF)
    {
        for (i=1; i<=N; ++i) Edges[i].clear();
        for (i=1; i<N; ++i)
        {
            scanf("%I64d %I64d", &x, &y);
            Edges[x].push_back(y), Edges[y].push_back(x);
        }
        for (i=1; i<=N; ++i)
        {
            scanf("%I64d", &vch[i]);
            vch[i] *= -1;
        }
        ans = dfs(-1,1);
        printf("%I64d\n", ans.first + ans.second);
    }
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 100006
int vch[MAXN];
vector < vector < int > > Edges(MAXN);
pair < int , int > calc[MAXN];
pair < int , int > dfs(int parent, int node)
{
    int inc = 0, dec = 0;
    int i,now;
    pair < int , int > temp;
    for (i=0; i<Edges[node].size(); ++i)
    {
        now = Edges[node][i];
        if (now == parent) continue;
        temp = dfs(node, now);
        if (inc < temp.first) inc = temp.first;
        if (dec < temp.second) dec = temp.second;
    }
    if (inc >= dec)
    {
        if (vch[node]>=0)
        {
            if (vch[node]>=inc-dec) inc += vch[node] - (inc-dec);
            else dec += (inc-dec) - vch[node];
        }
        else
        {
            vch[node] *= -1;
            dec += vch[node] + (inc-dec);
        }
    }
    else
    {
        if (vch[node]<=0)
        {
            vch[node] *= -1;
            if (vch[node]>=dec-inc) dec += vch[node] - (dec-inc);
            else inc += (dec-inc) - vch[node];
        }
        else
        {
            inc += vch[node] + (dec-inc);
        }
    }
    //printf("%d : %d %d\n", node, inc, dec);
    return make_pair(inc,dec);
}
int main()
{
    int N,i,x,y;
    pair < int , int > ans;
    while(scanf("%d", &N) != EOF)
    {
        for (i=1; i<=N; ++i) Edges[i].clear();
        for (i=1; i<N; ++i)
        {
            scanf("%d %d", &x, &y);
            Edges[x].push_back(y), Edges[y].push_back(x);
        }
        for (i=1; i<=N; ++i)
        {
            scanf("%d", &vch[i]);
            vch[i] *= -1;
        }
        ans = dfs(-1,1);
        printf("%d\n", ans.first + ans.second);
    }
    return 0;
}


#pragma comment(linker, "/STACK:67108864")

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair

VInt E[1 << 18];
int C[1 << 18];
int A[1 << 18];

pair<Int, Int> dfs(int a, int p)
{
	pair<Int, Int> res(0, 0);

	int i;
	FOR(i, 0, C[a])
	{
		int b = E[a][i];
		if(b != p)
		{
			pair<Int, Int> r = dfs(b, a);
			res.first = max(res.first, r.first);
			res.second = max(res.second, r.second);
		}
	}

	res.first = max(res.first, res.second + A[a]);
	res.second = max(res.second, res.second - A[a]);
	return res;
}

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

	CLEAR(C, 0);

	int i;
	FOR(i, 0, n - 1)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		--a;
		--b;

		E[a].PB(b);
		E[b].PB(a);
		++C[a];
		++C[b];
	}

	FOR(i, 0, n)
		scanf("%d", &A[i]);

	pair<Int, Int> res = dfs(0, -1);
	printf("%lld\n", res.first + res.second);

	return 0;
};


#pragma comment(linker, "/STACK:67108864")

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair

VInt E[1 << 18];
int C[1 << 18];
int A[1 << 18];

pair<Int, Int> dfs(int a, int p)
{
	pair<Int, Int> res(0, 0);

	int i;
	FOR(i, 0, C[a])
	{
		int b = E[a][i];
		if(b != p)
		{
			pair<Int, Int> r = dfs(b, a);
			res.first = max(res.first, r.first);
			res.second = max(res.second, r.second);
		}
	}

	res.first = max(res.first, res.second + A[a]);
	res.second = max(res.second, res.first - A[a]);
	return res;
}

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

	CLEAR(C, 0);

	int i;
	FOR(i, 0, n - 1)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		--a;
		--b;

		E[a].PB(b);
		E[b].PB(a);
		++C[a];
		++C[b];
	}

	FOR(i, 0, n)
		scanf("%d", &A[i]);

	pair<Int, Int> res = dfs(0, -1);
	printf("%lld\n", res.first + res.second);

	return 0;
};


#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
vector<vector<int> > v;
ll p[100001];
ll m[100001];
ll val[100001];
void dfs(int x,int prev)
{
	ll broj1=0;
	ll broj2=0;
	for(int i=0;i<v[x].size();i++)
	{
		if(v[x][i]!=prev)
		{
			dfs(v[x][i],x);
			broj1=max(broj1,p[v[x][i]]);
			broj2=max(broj2,m[v[x][i]]);
		}
	}
	ll brojce=val[x]-(broj1-broj2);
	if(brojce>0)
		broj1+=brojce;
	else
		broj2-=brojce;
	p[x]=broj1;
	m[x]=broj2;
}
int main()
{
	int n;
	cin>>n;
	vector<int> vec;
	v.insert(v.begin(),n+1,vec);
	for(int i=0;i<n-1;i++)
	{
		int a,b;
		cin>>a>>b;
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=0;i<n;i++)
		cin>>val[i];
	dfs(0,-1);
	cout<<p[0]+m[0]<<endl;
	return 0;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cstdio>
#include <ctime>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <iostream>
#include <cmath>
#include <sstream>
#include <memory.h>

using namespace std;

#pragma comment(linker, "/STACK:128000000")

typedef long long int64;
typedef unsigned long long uint64;

#define y1 _dsfkjdsfksdj
#define y0 _sfsdkfdop

typedef unsigned uint;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef pair<int64,int64> pii64;
typedef pair<pii,int> piii;
typedef pair<pii,pii> piiii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;

const int MAXN = 100005;

int n;
int V[MAXN];
vvi g;
int64 tres;

inline void init()
{
	int x, y;
	scanf("%d", &n);
	tres = 0;
	g.resize(n);
	for (int i = 1; i < n; ++i)
	{
		scanf("%d%d", &x, &y);
		--x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for (int i = 0; i < n; ++i)
		scanf("%d", &V[i]);
}

inline int64 abs64(int64 x)
{
	if (x < 0) return -x;
	return x;
}

pii64 dfs(int x, int p)
{
	int K = (int)g[x].size();

	if (!K) return pii64((int64)(-V[x]), (int64)(-V[x]));

	char was = 0;
	pii64 res(0, -1);
	for (int i = 0; i < K; ++i)
	{
		int y = g[x][i];

		if (y == p) continue;

		pii64 cur = dfs(y, x);
		if (!was)
			res = cur;
		else
		{
			res.first = min(res.first, cur.first);
			res.second = max(res.second, cur.second);
		}
		was = 1;
	}

	int64 bal = (int64)V[x] + res.first + res.second;
	if (bal > 0)
		res.first -= bal;
	else
		res.second -= bal;

	return res;
}

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

	init();

	pii64 ans = dfs(0, 0);

	cout << ans.second - ans.first << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cstdio>
#include <ctime>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <iostream>
#include <cmath>
#include <sstream>
#include <memory.h>

using namespace std;

#pragma comment(linker, "/STACK:128000000")

typedef long long int64;
typedef unsigned long long uint64;

#define y1 _dsfkjdsfksdj
#define y0 _sfsdkfdop

typedef unsigned uint;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef pair<int64,int64> pii64;
typedef pair<pii,int> piii;
typedef pair<pii,pii> piiii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;

const int MAXN = 100005;

int n;
int V[MAXN];
vvi g;
int64 tres;

inline void init()
{
	int x, y;
	scanf("%d", &n);
	tres = 0;
	g.resize(n);
	for (int i = 1; i < n; ++i)
	{
		scanf("%d%d", &x, &y);
		--x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for (int i = 0; i < n; ++i)
		scanf("%d", &V[i]);
}

inline int64 abs64(int64 x)
{
	if (x < 0) return -x;
	return x;
}

pii64 dfs(int x, int p)
{
	int K = (int)g[x].size();

	char was = 0;
	pii64 res(0, 0);
	for (int i = 0; i < K; ++i)
	{
		int y = g[x][i];

		if (y == p) continue;

		pii64 cur = dfs(y, x);
		if (!was)
			res = cur;
		else
		{
			res.first = min(res.first, cur.first);
			res.second = max(res.second, cur.second);
		}
		was = 1;
	}

	int64 bal = (int64)V[x] + res.first + res.second;
	if (bal > 0)
		res.first -= bal;
	else
		res.second -= bal;

	//cout << x + 1 << " --> " << res.first << " " << res.second << endl;

	return res;
}

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

	init();

	pii64 ans = dfs(0, 0);

	cout << ans.second - ans.first << endl;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include"string.h"
#include<stack>
#include<cmath>
using namespace std;
#define maxn 100100
#define LL __int64
#define mods 1000000007
int n,k;
int flag[maxn];
LL dp1[maxn];
LL dp2[maxn];
int last[maxn];
int pre[maxn * 2];
int to[maxn * 2];
LL a[maxn];
void dfs(int p){
	flag[p] = 1;
	int ptr = last[p];
	dp1[p] = 0;
	dp2[p] = 0;
	while (ptr != -1){
		if (flag[to[ptr]] == 0){
			dfs(to[ptr]);
			dp1[p] = max(dp1[p], dp1[to[ptr]]);
			dp2[p] = max(dp2[p], dp2[to[ptr]]);
		}
		ptr = pre[ptr];
	}
	a[p] += dp1[p]; a[p] -= dp2[p];
	if (a[p] < 0)dp1[p] -= a[p];
	else dp2[p] += a[p];
}
int main(){
	while (scanf("%d", &n) != EOF){
		memset(flag, 0, sizeof(flag));
		memset(last, -1, sizeof(last));
		int t1, t2;
		for (int i = 1; i < n; i++){
			scanf("%d %d", &t1, &t2);
			pre[i * 2] = last[t1];
			last[t1] = i * 2;
			to[i * 2] = t2;
			pre[i * 2+1] = last[t2];
			last[t2] = i * 2+1;
			to[i * 2+1] = t1;
		}
		for (int i = 1; i <= n; i++)scanf("%I64d", &a[i]);
		dfs(1);
		printf("%I64d\n", dp1[1] + dp2[1]);
	}
	return 0;
}


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

vector<int> e[100500];
bool used[100500];
vector<int>q;

int v[100500];
int p[100500];
int in[100500];
long long u[100500],d[100500];
int n;
long long res;

int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		a--,b--;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	for(int i=0;i<n;i++)
		scanf("%d",v+i);
	q.push_back(0);
	used[0]=1;
	for(int i=0;i<(int)q.size();i++)
		for(int j=0;j<(int)e[q[i]].size();j++)
			if(!used[e[q[i]][j]]){
				used[e[q[i]][j]]=1;
				q.push_back(e[q[i]][j]);
				p[e[q[i]][j]]=q[i];
				in[q[i]]++;
			}
	q.clear();
	for(int i=0;i<n;i++)
		if(in[i]==0)
			q.push_back(i);
	for(int i=0;i<(int)q.size();i++){
		long long val=v[q[i]]+u[q[i]]-d[q[i]];
		if(val>0)
			d[q[i]]+=val;
		else
			u[q[i]]-=val;
		if(q[i]!=0){
			d[p[q[i]]]=max(d[p[q[i]]],d[q[i]]);
			u[p[q[i]]]=max(u[p[q[i]]],u[q[i]]);
			in[p[q[i]]]--;
			if(!in[p[q[i]]])
				q.push_back(p[q[i]]);
		}
	}
	cout<<(u[0]+d[0])<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>

using namespace std;

#include <set>
const int V = 100010;
const int E = V << 1;
typedef long long LL;

int head[V], cur[E], nxt[E], ne;
void init(int n) {
    ne = 0, memset(head, -1, n << 2);
}
inline void addEdge(int u, int v) {
    cur[ne] = v, nxt[ne] = head[u], head[u] = ne++;
}
LL result = 0;
LL val[V], A[V], D[V], C1[V], C2[V];
LL c1, c2, res;

void dfs(int u, int fa) {
    int cnt = 0;
    for (int i = head[u]; i != -1; i = nxt[i]) {
        int v = cur[i];
        if (v == fa) continue;
        dfs(v, u);
        ++cnt;
        A[u] += A[v];
        D[u] += D[v];
        C1[u] += C1[v];
        C2[u] += C2[v];
        if (fa == 0) {
            c1 = max(c1, C1[v]);
            c2 = max(c2, C2[v]);
        }
    }
    val[u] += A[u] - D[u];
    if (fa == 0) {
        res = c1 + c2 + abs(val[u]);
    }
    if (val[u] < 0) {
        C2[u] += -val[u];
        D[u] += -val[u];
    } else {
        C1[u] += val[u];
        A[u] += val[u];
    }
}

int main() {
    int n;
    scanf("%d", &n);
    init(n + 1);
    c1 = c2 = 0;
    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        addEdge(u, v);
        addEdge(v, u);
    }
    for (int i = 1; i <= n; ++i) {
        scanf("%I64d", &val[i]);
    }
    dfs(1, 0);
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>

using namespace std;

#include <set>
const int V = 100010;
const int E = V << 1;
typedef long long LL;

int head[V], cur[E], nxt[E], ne;
void init(int n) {
    ne = 0, memset(head, -1, n << 2);
}
inline void addEdge(int u, int v) {
    cur[ne] = v, nxt[ne] = head[u], head[u] = ne++;
}
LL result = 0;
LL val[V], A[V], D[V], C1[V], C2[V];
LL c1, c2, res;

void dfs(int u, int fa) {
    for (int i = head[u]; i != -1; i = nxt[i]) {
        int v = cur[i];
        if (v == fa) continue;
        dfs(v, u);
        //A[u] += A[v];
        //D[u] += D[v];
        C1[u] = max(C1[u], C1[v]);
        C2[u] = max(C2[u], C2[v]);
    }
    val[u] += C1[u] - C2[u];

    if (val[u] < 0) {
        C1[u] += -val[u];
        //A[u] += -val[u];
    } else {
        C2[u] += val[u];
        //D[u] += val[u];
    }
}

int main() {
    int n;
    scanf("%d", &n);
    init(n + 1);
    c1 = c2 = 0;
    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        addEdge(u, v);
        addEdge(v, u);
    }
    for (int i = 1; i <= n; ++i) {
        scanf("%I64d", &val[i]);
    }
    dfs(1, 0);
    res = C1[1] + C2[1];
    cout << res << endl;
    return 0;
}


//          be name khoda

#include <iostream>
#include <vector>

using namespace std;

const int maxN = 100010;
const int INF = maxN * 1000 * 1000 * 1000;

long long value[maxN], sum[maxN], p[maxN], ne[maxN], ans[maxN], n;
vector <int> G[maxN];

long long dfs(int v, int dad)
{
    long long sm = -value[v], now;
    p[v] = ne[v] = 0;
    for(int i = 0; i < G[v].size(); i++)
        if(G[v][i] != dad)
        {
            now = dfs(G[v][i], v);
            sm += now;
            p[v] = max(p[v], p[G[v][i]]);
            ne[v] = min(ne[v], ne[G[v][i]]);
        }
    if(value[v] + p[v] + ne[v] > 0)
        ne[v] -= (value[v] + p[v] + ne[v]);
    p[v] -= (value[v] + p[v] + ne[v]);
    return sm;
}

int main()
{
    cin >> n;
    for(int i = 1; i < n; i++)
    {
        int l, r;
        cin >> l >> r;
        G[l].push_back(r);
        G[r].push_back(l);
    }
    for(int i = 1; i <= n; i++)
        cin >> value[i];
    dfs(1, -1);
    cout << p[1] + abs(ne[1]) << endl;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector <int> vi;

#define sqr(x) ((x)*(x))
#define forn(i, n) for (i = 0; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define updMin(a, b) ((a) = min((a), (b)))
#define updMax(a, b) ((a) = max((a), (b)))

const int maxn = 100000 + 100;

int w[maxn];
bool vis[maxn];
vector <vi> g;

ll d[maxn][2];

void dfs(int v)
{
    vis[v] = true;
    int s = g[v].size(), i, to;
    ll a, b;
    a = b = 0;
    forn(i, s) {
        to = g[v][i];
        if (vis[to])
            continue;
        dfs(to);
        updMax(a, d[to][0]);
        updMax(b, d[to][1]);
    }
    ll c = w[v];
    c += a - b;
    if (c > 0) {
        a += c;
    } else {
        b += -c;
    }
    d[v][0] = a;
    d[v][1] = b;
}

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

    int n, i, j, a, b;
    scanf("%d", &n);
    g.resize(n);
    forn(i, n - 1) {
        scanf("%d%d", &a, &b);
        --a;
        --b;
        g[a].pb(b);
        g[b].pb(a);
    }
    forn(i, n)
        scanf("%d", &w[i]);
    dfs(0);
    ll ans = d[0][0] + d[0][1];
    printf("%I64d\n", ans);
    return 0;
}