#include <bits/stdc++.h>
using namespace std;
int n;
struct node {
  int v, next;
} edge[201010];
int head[201010], add;
void adj(int u, int v) {
  edge[add].v = v;
  edge[add].next = head[u];
  head[u] = add++;
  edge[add].v = u;
  edge[add].next = head[v];
  head[v] = add++;
}
long long val[201010];
long long m1[201010], m2[201010];
void init() {
  int i, j, u, v;
  memset(head, -1, sizeof(head));
  add = 0;
  for (i = 0; i < n - 1; i++) {
    scanf("%d %d", &u, &v);
    adj(u, v);
  }
  for (i = 1; i <= n; i++) scanf("%I64d", &val[i]);
}
void dfs(int u, int fa) {
  m1[u] = m2[u] = 0;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].v;
    if (fa == v) continue;
    dfs(v, u);
    m1[u] = max(m1[u], m1[v]);
    m2[u] = max(m2[u], m2[v]);
  }
  if (val[u] + m1[u] - m2[u] > 0)
    m2[u] += val[u] + m1[u] - m2[u];
  else
    m1[u] -= val[u] + m1[u] - m2[u];
}
int main() {
  while (scanf("%d", &n) != EOF) {
    init();
    dfs(1, -1);
    printf("%I64d\n", m1[1] + m2[1]);
  }
  return 0;
}
