#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct Total {
  long long in;
  long long de;
} cnt[N];
vector<long long> node[N];
long long v[N];
bool vis[N];
Total dfs(int i) {
  vis[i] = true;
  long long in = 0, de = 0;
  Total t;
  for (vector<long long>::iterator it = node[i].begin(); it != node[i].end();
       it++) {
    if (vis[*it]) continue;
    t = dfs(*it);
    if (t.in > in) in = t.in;
    if (t.de > de) de = t.de;
  }
  cnt[i].de = de;
  cnt[i].in = in;
  int d = v[i] - cnt[i].de + cnt[i].in;
  if (d > 0)
    cnt[i].de += d;
  else
    cnt[i].in -= d;
  return cnt[i];
}
int main() {
  int n, a, b;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    node[a].push_back(b);
    node[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) scanf("%I64d", &v[i]);
  Total ans = dfs(1);
  printf("%I64d\n", ans.de + ans.in);
  return 0;
}
