#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100000 + 100;
long long n, a[MAXN], ans, y, manfi[MAXN], mosbat[MAXN];
vector<long long> adj[MAXN];
void dfs(long long u, long long p) {
  long long minus = 0, plus = 0;
  for (auto x : adj[u]) {
    if (x == p) continue;
    dfs(x, u);
    minus = max(minus, manfi[x]);
    plus = max(plus, mosbat[x]);
  }
  manfi[u] = minus, mosbat[u] = plus;
  a[u] += mosbat[u] - manfi[u];
  if (a[u] > 0)
    manfi[u] += a[u];
  else
    mosbat[u] += -a[u];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  for (long long i = 0; i < n; i++) cin >> a[i];
  dfs(0, 0);
  cout << manfi[0] + mosbat[0] << endl;
}
