#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
int n;
vector<int> g[N];
long long a[N], m[N], p[N];
void dfs(int v, int pr) {
  long long mi = 0ll, pl = 0ll;
  for (auto to : g[v]) {
    if (to == pr) continue;
    dfs(to, v);
    mi = max(mi, m[to]);
    pl = max(pl, p[to]);
  }
  m[v] += mi;
  p[v] += pl;
  a[v] += pl - mi;
  if (a[v] > 0ll)
    m[v] += a[v];
  else if (a[v] < 0ll)
    p[v] += -1ll * a[v];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
  }
  dfs(1, -1);
  printf("%I64d", p[1] + m[1]);
  return 0;
}
