#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, par[maxn];
long long VAL[maxn], pos[maxn], neg[maxn];
bool mark[maxn];
vector<int> G[maxn], top;
void dfs(int u) {
  mark[u] = 1;
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (!mark[v]) {
      par[v] = u;
      dfs(v);
    }
  }
  top.push_back(u);
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v), G[v].push_back(u);
  }
  for (int i = 0; i < n; i++) cin >> VAL[i];
  dfs(0);
  for (int i = 0; i < (int)top.size(); i++) {
    int u = top[i], P = par[u];
    VAL[u] += pos[u] + neg[u];
    if (VAL[u] < 0)
      pos[u] += -VAL[u];
    else
      neg[u] += -VAL[u];
    pos[P] = max(pos[P], pos[u]);
    neg[P] = min(neg[P], neg[u]);
  }
  cout << pos[0] - neg[0] << endl;
}
