#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100005;
template <class T>
inline void read(T &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
long long a[N], jia[N], jian[N];
int n, head[N], cnt, x, y;
struct node {
  int v, nxt;
} e[N << 1];
inline void add(int u, int v) { e[++cnt] = (node){v, head[u]}, head[u] = cnt; }
void dfs(int u, int fa) {
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == fa) continue;
    dfs(v, u);
    jian[u] = max(jian[u], jian[v]);
    jia[u] = max(jia[u], jia[v]);
  }
  a[u] = a[u] + jia[u] - jian[u];
  if (a[u] > 0)
    jian[u] += a[u];
  else
    jia[u] -= a[u];
}
int main() {
  read(n);
  for (int i = 1; i < n; ++i) read(x), read(y), add(x, y), add(y, x);
  for (int i = 1; i <= n; ++i) read(a[i]);
  dfs(1, 0);
  cout << jia[1] + jian[1] << endl;
  return 0;
}
