#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, d[N], c[N];
long long add[N], sub[N];
vector<int> a[N];
void dfs(int u) {
  d[u] = 1;
  for (int i = (0), _b = (a[u].size() - 1); i <= _b; ++i) {
    int v = a[u][i];
    if (d[v]) continue;
    dfs(v);
    add[u] = max(add[u], add[v]);
    sub[u] = max(sub[u], sub[v]);
  }
  long long value = c[u] + add[u] - sub[u];
  if (value > 0)
    sub[u] += value;
  else
    add[u] -= value;
}
int main() {
  scanf("%d", &n);
  for (int i = (1), _b = (n - 1); i <= _b; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    a[u].push_back(v);
    a[v].push_back(u);
  }
  for (int i = (1), _b = (n); i <= _b; ++i) scanf("%d", &c[i]);
  dfs(1);
  printf("%I64d\n", add[1] + sub[1]);
  return 0;
}
