#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
long long a[N], b[N];
vector<int> e[N];
vector<long long> val;
long long ans;
void dfs(int v = 0, int p = -1) {
  for (int to : e[v]) {
    if (to == p) continue;
    dfs(to, v);
    a[v] = max(a[v], a[to]);
    b[v] = max(b[v], b[to]);
  }
  long long tmp = val[v] + a[v] - b[v];
  if (tmp > 0)
    b[v] += tmp;
  else
    a[v] -= tmp;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  val.resize(n);
  for (long long &i : val) scanf("%lld", &i);
  dfs();
  printf("%lld", a[0] + b[0]);
  return 0;
}
