#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
long long a[N], add[N], subtract[N];
vector<int> g[N];
void dfs(int u, int p) {
  for (int v : g[u])
    if (v != p) {
      dfs(v, u);
      add[u] = max(add[u], add[v]);
      subtract[u] = max(subtract[u], subtract[v]);
    }
  a[u] -= add[u];
  a[u] += subtract[u];
  add[u] += max(0LL, a[u]);
  subtract[u] += max(0LL, -1 * a[u]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++) scanf("%lld", a + i);
  dfs(0, -1);
  printf("%lld\n", add[0] + subtract[0]);
  return 0;
}
