#include <bits/stdc++.h>
using namespace std;
int n;
long long a[100005], b[100005];
vector<int> vec[100005];
long long v[100005];
void init() {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
}
void dfs(int now, int par) {
  int ok = 0;
  for (int i = 0; i < vec[now].size(); i++) {
    ok = vec[now][i];
    if (ok == par)
      continue;
    else
      dfs(ok, now);
    a[now] = max(a[now], a[ok]);
    b[now] = max(b[now], b[ok]);
  }
  v[now] = v[now] + a[now] - b[now];
  if (v[now] < 0)
    a[now] += abs(v[now]);
  else
    b[now] += v[now];
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> a[i] >> b[i];
    vec[a[i]].push_back(b[i]);
    vec[b[i]].push_back(a[i]);
  }
  for (int i = 1; i <= n; i++) cin >> v[i];
  init();
  dfs(1, 0);
  long long ans = a[1] + b[1];
  cout << ans << endl;
  return 0;
}
