#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
long long int power(long long int a, long long int b, long long int m) {
  if (b == 0) return 1;
  if (b == 1) return a % m;
  long long int t = power(a, b / 2, m) % m;
  t = (t * t) % m;
  if (b & 1) t = ((t % m) * (a % m)) % m;
  return t;
}
long long int modInverse(long long int a, long long int m) {
  return power(a, m - 2, m);
}
long long int n, fl[3000005];
vector<long long int> vc[3000005];
long long int val[3000005], ince[3000005], dcr[3000005];
void dfs(long long int i, long long int p) {
  fl[i] = 1;
  for (auto it : vc[i]) {
    if (fl[it]) continue;
    dfs(it, i);
    ince[i] = max(ince[i], ince[it]);
    dcr[i] = max(dcr[i], dcr[it]);
  }
  val[i] += (ince[i] - dcr[i]);
  if (val[i] > 0)
    dcr[i] += val[i];
  else
    ince[i] -= val[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> k >> l;
    vc[k].push_back(l);
    vc[l].push_back(k);
  }
  for (i = 1; i <= n; i++) {
    cin >> val[i];
  }
  dfs(1, 0);
  cout << (dcr[1] + ince[1]);
  return 0;
}
