#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
long long n;
vector<long long> adj[100010];
long long val[100010];
pii dfs(long long node, long long par = -1) {
  pii res = {0, 0};
  long long maxd = 0, maxi = 0;
  for (long long i : adj[node]) {
    if (i == par) continue;
    pii x = dfs(i, node);
    res.first = max(x.first, res.first);
    res.second = max(x.second, res.second);
    maxd = max(maxd, x.first);
    maxi = max(maxi, x.second);
  }
  if (val[node] - maxd + maxi > 0) {
    res.first += abs(val[node] - maxd + maxi);
  } else {
    res.second += abs(val[node] - maxd + maxi);
  }
  return res;
}
signed main() {
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (long long i = 0; i < n; i++) {
    cin >> val[i];
  }
  pii p = dfs(0);
  cout << p.first + p.second << endl;
}
