#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
long long plus1[MAXN], minus1[MAXN], v[MAXN];
vector<int> grafo[MAXN];
void dfs(int v1, int p) {
  long long mx = 0, mn = 0;
  for (int i = 0; i < grafo[v1].size(); i++) {
    int viz = grafo[v1][i];
    if (viz == p) continue;
    dfs(viz, v1);
    mx = max(mx, plus1[viz]);
    mn = max(mn, minus1[viz]);
  }
  plus1[v1] = mx;
  minus1[v1] = mn;
  v[v1] += mx - mn;
  if (v[v1] > 0)
    minus1[v1] += v[v1];
  else
    plus1[v1] -= v[v1];
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    grafo[a].push_back(b);
    grafo[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) scanf("%lld", &v[i]);
  dfs(1, 1);
  printf("%lld\n", plus1[1] + minus1[1]);
  return 0;
}
