#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, a, b, v[maxn];
vector<int> g[maxn];
long long cp[maxn], cm[maxn];
void dfs(int i, int p = -1) {
  long long maxm = 0, maxp = 0;
  for (int j = 0; j < (int)(g[i].size()); j++) {
    int y = g[i][j];
    if (y == p) continue;
    dfs(y, i);
    if (cp[y] > maxp) maxp = cp[y];
    if (cm[y] > maxm) maxm = cm[y];
  }
  cp[i] = maxp;
  cm[i] = maxm;
  long long val = v[i] + maxp - maxm;
  if (val < 0)
    cp[i] += -val;
  else
    cm[i] += val;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n - 1); i++) {
    scanf("%d %d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < (int)(n); i++) scanf("%d", &v[i]);
  dfs(0);
  cout << cp[0] + cm[0] << endl;
  return 0;
}
