#include <bits/stdc++.h>
struct {
  int v[(100005 * 2)], pre[(100005 * 2)], ind[100005], n;
  void push_edge(int ui, int vi) {
    int id = ++n;
    v[id] = vi;
    pre[id] = ind[ui];
    ind[ui] = id;
  }
} G;
using std::max;
using std::min;
struct Node {
  long long int val, add, sub;
  void print() { printf("(%I64d,%I64d,%I64d)\n", val, add, sub); }
} node[100005];
int N;
void dfs(int n, int ori = 0) {
  int m, mid;
  Node &U = node[n];
  for (mid = G.ind[n]; mid; mid = G.pre[mid]) {
    m = G.v[mid];
    if (m == ori) continue;
    dfs(m, n);
    Node &V = node[m];
    U.add = max(U.add, V.add);
    U.sub = min(U.sub, V.sub);
  }
  long long int r = U.val + U.add + U.sub;
  if (r > 0)
    U.sub -= r;
  else
    U.add -= r;
}
int main() {
  int i, j, k;
  scanf("%d", &N);
  for (k = 1; k < N; k++) {
    scanf("%d%d", &i, &j);
    G.push_edge(i, j);
    G.push_edge(j, i);
  }
  for (i = 1; i <= N; i++) {
    scanf("%d", &k);
    node[i].val = k;
  }
  dfs(1);
  printf("%I64d\n", node[1].add - node[1].sub);
}
