#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100001;
struct node {
  int y, next;
} a[MaxN * 2];
int first[MaxN];
long long c[MaxN];
long long f[MaxN];
long long zh[MaxN], fu[MaxN];
bool v[MaxN];
int n, m, tot;
long long ans;
void insert(int x, int y) {
  a[++tot].next = first[x];
  first[x] = tot;
  a[tot].y = y;
}
void dfs(int x) {
  v[x] = 1;
  f[x] = 0;
  zh[x] = 0;
  f[x] = 0;
  int j = first[x], y;
  long long zd = 0, zx = 0;
  while (j) {
    y = a[j].y;
    if (!v[y]) {
      dfs(y);
      zh[x] = max(zh[x], zh[y]);
      fu[x] = max(fu[x], fu[y]);
    }
    j = a[j].next;
  }
  f[x] += -zh[x] + fu[x];
  c[x] += f[x];
  if (c[x] > 0)
    zh[x] += c[x];
  else
    fu[x] -= c[x];
}
int main() {
  int i, j, sum, x, y;
  memset(first, 0, sizeof(first));
  memset(v, 0, sizeof(v));
  cin >> n;
  tot = 0;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    insert(x, y);
    insert(y, x);
  }
  for (i = 1; i <= n; i++) cin >> c[i];
  ans = 0;
  dfs(1);
  cout << zh[1] + fu[1] << endl;
  cin >> n;
  return 0;
}
