#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, u, v, cnt;
int a[100005], head[100005];
long long x[100005], y[100005];
bool vis[100005];
struct ff {
  int to, nxt;
} e[2 * 100005];
void add(int u, int v) {
  e[++cnt] = (ff){v, head[u]};
  head[u] = cnt;
}
void dfs(int now) {
  vis[now] = 1;
  for (int i = head[now]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (vis[v]) continue;
    dfs(v);
    x[now] = max(x[now], x[v]);
    y[now] = max(y[now], y[v]);
  }
  if (x[now] - y[now] < a[now])
    x[now] = a[now] + y[now];
  else
    y[now] = x[now] - a[now];
}
int main() {
  scanf("%d", &n);
  for (i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    add(u, v), add(v, u);
  }
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  dfs(1);
  printf("%I64d\n", x[1] + y[1]);
  return 0;
}
