#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to;
} e[200010];
int edg = 0, fat[100010], n;
long long up[100010], down[100010], num[100010];
void dfs(int l, int r) {
  int x;
  for (int i = fat[r]; i > 0; i = e[i].to) {
    x = e[i].from;
    if (x == l) continue;
    dfs(r, x);
    up[r] = max(up[r], up[x]);
    down[r] = max(down[r], down[x]);
  }
  num[r] += (up[r] - down[r]);
  if (num[r] > 0)
    down[r] += num[r];
  else
    up[r] -= num[r];
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int l, r;
    cin >> l >> r;
    e[++edg].from = r;
    e[edg].to = fat[l];
    fat[l] = edg;
    e[++edg].from = l;
    e[edg].to = fat[r];
    fat[r] = edg;
  }
  for (int i = 1; i <= n; i++) {
    int d;
    cin >> d;
    num[i] = d;
    if (d > 0)
      down[i] = d;
    else
      up[i] = -d;
  }
  dfs(0, 1);
  cout << up[1] + down[1] << endl;
  return 0;
}
