#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
int val[100005];
pair<long long, long long> dfs(int u, int p) {
  int i;
  pair<long long, long long> mx(0, 0);
  for (i = 0; i < (int)adj[u].size(); i++) {
    int v = adj[u][i];
    if (v != p) {
      pair<long long, long long> tmp = dfs(v, u);
      mx.first = max(mx.first, tmp.first);
      mx.second = max(mx.second, tmp.second);
    }
  }
  pair<long long, long long> ret(0, 0);
  val[u] += (mx.first - mx.second);
  ret.first = mx.first;
  ret.second = mx.second;
  if (val[u] > 0)
    ret.second += val[u];
  else
    ret.first -= val[u];
  return ret;
}
int main() {
  int n, i;
  scanf("%d", &n);
  for (i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
  }
  pair<long long, long long> ret = dfs(1, 0);
  long long ans = (ret.first + ret.second);
  printf("%I64d\n", ans);
  return 0;
}
