#include <bits/stdc++.h>
using namespace std;
vector<long long int> a[200000];
long long int v[200000];
pair<long long int, long long int> DFS(long long int x, long long int par) {
  pair<long long int, long long int> p1, p2;
  long long int i, k = 0, l = 0;
  for (i = 0; i < a[x].size(); i++) {
    if (a[x][i] != par) {
      p1 = DFS(a[x][i], x);
      k = max(k, p1.first);
      l = max(l, p1.second);
    }
  }
  i = v[x] + k - l;
  if (i < 0)
    return p2 = make_pair(k - i, l);
  else
    return p2 = make_pair(k, l + i);
}
int main() {
  long long int n, m, i, j, k, l, x, y;
  scanf("%lld", &n);
  pair<long long int, long long int> p1;
  for (i = 1; i < n; i++) {
    scanf("%lld", &x);
    scanf("%lld", &y);
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (i = 1; i <= n; i++) scanf("%lld", &v[i]);
  p1 = DFS(1, 0);
  printf("%lld\n", p1.first + p1.second);
  return 0;
}
