#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const long long mod = (1e9) + 7;
const long long INF = (1e9) + 10;
struct edge {
  int v, nxt;
} e[100020 * 2];
long long adds[100020], eras[100020], a[100020];
int head[100020], en = 0;
void adde(int u, int v) {
  e[++en].v = v;
  e[en].nxt = head[u];
  head[u] = en;
}
void init() { memset(head, -1, sizeof(head)); }
void dfs(int x, int pre) {
  for (int i = head[x]; i != -1; i = e[i].nxt) {
    int v = e[i].v;
    if (v == pre) continue;
    dfs(v, x);
    adds[x] = max(adds[x], adds[v]);
    eras[x] = max(eras[x], eras[v]);
  }
  if (a[x] + adds[x] - eras[x] > 0)
    eras[x] += (a[x] + adds[x] - eras[x]);
  else
    adds[x] -= (a[x] + adds[x] - eras[x]);
  return;
}
int main() {
  init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adde(u, v);
    adde(v, u);
  }
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  dfs(1, -1);
  printf("%I64d\n", adds[1] + eras[1]);
  return 0;
}
