#include <bits/stdc++.h>
using namespace std;
int n, h;
int to[100010 * 2], nxt[100010 * 2], head[100010];
long long jia[100010], del[100010], v[100010];
void add(int u, int v) {
  to[++h] = v;
  nxt[h] = head[u];
  head[u] = h;
}
void dfs(int x, int dad) {
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != dad) {
      dfs(to[i], x);
      jia[x] = max(jia[x], jia[to[i]]);
      del[x] = max(del[x], del[to[i]]);
    }
  }
  int g = v[x] + jia[x] - del[x];
  if (g > 0)
    del[x] += g;
  else
    jia[x] -= g;
}
int main() {
  scanf("%d", &n);
  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("%lld", &v[i]);
  }
  dfs(1, 0);
  cout << del[1] + jia[1];
}
