#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const long long maxn = 1e5 + 10;
vector<long long> g[maxn];
long long ans;
long long V[maxn], mn[maxn], mx[maxn];
void dfs(long long aa, long long aaa) {
  for (long long a : g[aa]) {
    if (a == aaa) continue;
    dfs(a, aa);
    mn[aa] = min(mn[a], mn[aa]);
    mx[aa] = max(mx[a], mx[aa]);
  }
  V[aa] -= mn[aa];
  V[aa] -= mx[aa];
  if (V[aa] < 0)
    mn[aa] += V[aa];
  else
    mx[aa] += V[aa];
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i < n; ++i) {
    long long first, second;
    cin >> first >> second;
    g[first].push_back(second);
    g[second].push_back(first);
  }
  for (long long i = 1; i <= n; ++i) cin >> V[i];
  dfs(1, 0);
  cout << mx[1] - mn[1];
  return 0;
}
