#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
long long int weight[100010], add[100010], sub[100010];
int n;
void dfs(int mom, int u) {
  for (vector<int>::iterator v = g[u].begin(); v != g[u].end(); v++)
    if (*v != mom) {
      dfs(u, *v);
      add[u] = max(add[*v], add[u]);
      sub[u] = max(sub[u], sub[*v]);
    }
  weight[u] += add[u] - sub[u];
  if (weight[u] < 0)
    add[u] -= weight[u];
  else
    sub[u] += weight[u];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) scanf("%I64d", weight + i);
  dfs(-1, 1);
  printf("%I64d\n", add[1] + sub[1]);
}
