#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
const int lmt = 100005;
vector<int> adj[lmt];
long long a[lmt];
pair<long long, long long> dfs(int u, int p) {
  if ((adj[u].size() == 1) && (adj[u][0] == p)) {
    long long add = 0, sub = 0;
    if (a[u] > 0)
      sub += a[u];
    else
      add += abs(a[u]);
    a[u] = 0;
    return make_pair(add, sub);
  }
  long long ad = 0, sb = 0;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v != p) {
      pair<long long, long long> p = dfs(v, u);
      ad = max(ad, p.first);
      sb = max(p.second, sb);
    }
  }
  long long val = a[u] + ad - sb;
  if (val > 0)
    sb += val;
  else
    ad += abs(val);
  a[u] = 0;
  return make_pair(ad, sb);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  pair<long long, long long> ans = dfs(1, 0);
  printf("%lld\n", ans.first + ans.second);
  return 0;
}
