#include <bits/stdc++.h>
using namespace std;
struct node {
  long long pos, neg;
} tree[100005];
long long tv[100005];
bool vis[100005];
vector<int> v[100005];
node dfs(int u) {
  int i;
  long long mp = 0, mn = 0, x;
  node temp;
  for (i = 0; i < v[u].size(); i++) {
    if (vis[v[u][i]]) continue;
    vis[v[u][i]] = true;
    temp = dfs(v[u][i]);
    mp = max(temp.pos, mp);
    mn = min(temp.neg, mn);
  }
  tree[u].pos = mp;
  tree[u].neg = mn;
  x = (mp + mn + tv[u]) * (-1);
  if (x >= 0)
    tree[u].pos += x;
  else
    tree[u].neg += x;
  return tree[u];
}
int main() {
  int n, i, x, y;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (i = 1; i <= n; i++) cin >> tv[i];
  memset(vis, false, sizeof(vis));
  vis[1] = true;
  node res = dfs(1);
  cout << res.pos - res.neg << "\n";
  return 0;
}
