#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> q;
long long v[100001];
pair<long long, long long> solve(long long i, long long p) {
  if (q[i].size() == 1 and p != -1) {
    long long l = min((long long)0, v[i]);
    long long r = max((long long)0, v[i]);
    return {r, -1 * l};
  }
  pair<long long, long long> c = {0, 0};
  for (long long j = 0; j < q[i].size(); j++) {
    if (p != q[i][j]) {
      pair<long long, long long> x = solve(q[i][j], i);
      c.first = max(c.first, x.first);
      c.second = max(c.second, x.second);
    }
  }
  long long xx = -1 * c.first + c.second;
  v[i] += xx;
  c.first += max((long long)0, v[i]);
  c.second += max((long long)0, -1 * v[i]);
  return c;
}
int main() {
  long long n;
  cin >> n;
  q.resize(n + 1);
  for (long long i = 0; i < n - 1; i++) {
    long long l, r;
    cin >> l >> r;
    q[l].push_back(r);
    q[r].push_back(l);
  }
  for (long long i = 1; i <= n; i++) cin >> v[i];
  pair<long long, long long> ans = solve(1, -1);
  cout << ans.first + ans.second;
}
