#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 42;
vector<vector<int> > g;
bool used[N];
long long v[N];
pair<long long, long long> DFS(int from) {
  used[from] = true;
  long long inc = 0, dec = 0;
  for (int i = 0; i < (int)(g[from]).size(); i++) {
    int to = g[from][i];
    if (used[to]) continue;
    pair<long long, long long> cur = DFS(to);
    inc = max(inc, cur.first);
    dec = max(dec, cur.second);
  }
  v[from] = v[from] + inc - dec;
  if (v[from] > 0)
    dec += v[from];
  else
    inc -= v[from];
  return pair<long long, long long>(inc, dec);
}
int main() {
  int n;
  scanf("%d", &n);
  g.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; i++) scanf("%I64d", &v[i]);
  pair<long long, long long> res = DFS(0);
  cout << res.first + res.second;
  return 0;
}
