#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005];
long long val[100005];
long long add[100005], mi[100005];
void dfs(int u, int father) {
  long long mm = 0, mp = 0;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v == father) continue;
    dfs(v, u);
    mp = max(add[v], mp);
    mm = max(mi[v], mm);
  }
  val[u] += mp - mm;
  if (val[u] > 0)
    mm += val[u];
  else
    mp -= val[u];
  add[u] = mp;
  mi[u] = mm;
}
int main() {
  int n, u, v;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) cin >> val[i];
  dfs(1, 0);
  cout << add[1] + mi[1] << endl;
  return 0;
}
