#include <bits/stdc++.h>
using namespace std;
const long long M = 1e5 + 5;
vector<long long> adj[M];
long long pos[M], neg[M], ans, a[M];
void dfs(long long cur, long long pre) {
  for (auto &val : adj[cur]) {
    if (val != pre) {
      dfs(val, cur);
      pos[cur] = max(pos[val], pos[cur]);
      neg[cur] = max(neg[val], neg[cur]);
    }
  }
  if (pos[cur] - neg[cur] + a[cur] > 0)
    neg[cur] += pos[cur] - neg[cur] + a[cur];
  else
    pos[cur] -= pos[cur] - neg[cur] + a[cur];
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) cin >> a[i];
  ans += abs(a[1]);
  dfs(1, 0);
  cout << pos[1] + neg[1];
}
