#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 10;
long long L[Maxn], R[Maxn];
int v[Maxn];
int n;
struct Edge {
  int to, next;
  Edge() {}
  Edge(int to, int next) : to(to), next(next) {}
} edge[Maxn << 1];
int box[Maxn], size;
void add(int from, int to) {
  edge[size] = Edge(to, box[from]);
  box[from] = size++;
}
void dfs(int x, int last) {
  long long l = 0, r = 0;
  for (int i = box[x]; i != -1; i = edge[i].next) {
    int x1 = edge[i].to;
    if (x1 == last) continue;
    dfs(x1, x);
    l = max(l, L[x1]);
    r = max(r, R[x1]);
  }
  int tmp = v[x] - (l - r);
  if (tmp < 0)
    R[x] = r - tmp, L[x] = l;
  else
    R[x] = r, L[x] = l + tmp;
}
int main() {
  scanf("%d", &n);
  memset(box, -1, sizeof(box));
  size = 0;
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y), add(y, x);
  }
  for (int i = 1; i <= n; ++i) scanf("%d", v + i);
  dfs(1, -1);
  printf("%I64d\n", L[1] + R[1]);
  return 0;
}
