#include <bits/stdc++.h>
using namespace std;
template <class stl>
void DBGSTL(stl a) {
  for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++) {
    cerr << *i << " ";
  }
  cerr << "\n";
  return;
}
vector<int> e[100004];
long long v[100005];
long long P[100005];
long long M[100005];
long long dfs(int p, int now) {
  long long plus = 0, minus = 0;
  for (int i = 0; i < e[now].size(); i++) {
    int nxt = e[now][i];
    if (nxt != p) {
      dfs(now, nxt);
    }
    plus = max(plus, P[nxt]);
    minus = max(minus, M[nxt]);
  }
  long long diff = v[now] - (plus - minus);
  if (diff > 0)
    plus += diff;
  else
    minus -= diff;
  P[now] = plus;
  M[now] = minus;
  return P[now] + M[now];
}
int main() {
  int n, u, x;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> x;
    e[u].push_back(x);
    e[x].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  cout << dfs(-1, 1) << "\n";
  return 0;
}
