#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long MAX = 2e6 + 7;
const long long MOD = 1000000007;
const long double PI = 3.141592653589793238463;
long long i, j, k, t, n, q, l, r, mid;
vector<long long> adj[MAX];
long long cst, x, y, mini;
long long val[MAX];
long long inc[MAX], dcr[MAX];
void dfs(long long x, long long p) {
  long long maxi = 0;
  for (auto xx : adj[x]) {
    if (xx != p) {
      dfs(xx, x);
      inc[x] = max(inc[x], inc[xx]);
      dcr[x] = max(dcr[x], dcr[xx]);
    }
  }
  val[x] += inc[x] - dcr[x];
  if (val[x] > 0)
    dcr[x] += val[x];
  else
    inc[x] += abs(val[x]);
}
void solve() {
  mini = INF;
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (long long i = 1; i < n + 1; i++) cin >> val[i];
  dfs(1, 0);
  cout << inc[1] + dcr[1] << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
