#include <bits/stdc++.h>
int edges[100001], to[200000], next[200000];
long long up[100001], down[100001];
int value[100001];
int n;
void edge_add(int u, int v) {
  static int total = 1;
  to[total] = v;
  next[total] = edges[u];
  edges[u] = total++;
}
void dfs(int x, int y) {
  int e;
  long long v;
  for (e = edges[x]; e; e = next[e]) {
    if (to[e] == y) continue;
    dfs(to[e], x);
    if (up[to[e]] > up[x]) up[x] = up[to[e]];
    if (down[to[e]] > down[x]) down[x] = down[to[e]];
  }
  v = value[x] + up[x] - down[x];
  if (v > 0)
    down[x] += v;
  else
    up[x] -= v;
}
int main(void) {
  int i;
  int u, v;
  scanf("%d", &n);
  for (i = 1; i < n; ++i)
    scanf("%d %d", &u, &v), edge_add(u, v), edge_add(v, u);
  for (i = 1; i <= n; ++i) scanf("%d", &value[i]);
  dfs(1, 0);
  printf("%I64d\n", up[1] + down[1]);
  return 0;
}
