#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct edge {
  int v, next;
} e[maxn << 1];
int head[maxn], cnt = 0, n;
long long up[maxn], down[maxn], val[maxn];
void add(int u, int v) {
  e[++cnt] = (edge){v, head[u]};
  head[u] = cnt;
  e[++cnt] = (edge){u, head[v]};
  head[v] = cnt;
}
void dfs(int u, int fa) {
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].v;
    if (v == fa) continue;
    dfs(v, u);
    up[u] = max(up[u], up[v]);
    down[u] = max(down[u], down[v]);
  }
  if (up[u] + val[u] > down[u])
    down[u] += up[u] + val[u] - down[u];
  else
    up[u] += down[u] - val[u] - up[u];
  val[u] = 0;
}
int u, v;
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 1; i < n; i++) scanf("%d%d", &u, &v), add(u, v);
  for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
  dfs(1, 0);
  printf("%lld\n", down[1] + up[1]);
}
