#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<long long> adj[N];
long long a[N];
pair<long long, long long> dfs(int u, int p) {
  long long mx = 0, mi = 0;
  for (int i : adj[u]) {
    if (i != p) {
      pair<long long, long long> tmp = dfs(i, u);
      mx = max(mx, tmp.first);
      mi = min(mi, tmp.second);
    }
  }
  if (a[u] - mx - mi >= 0) {
    mx += a[u] - mx - mi;
  } else {
    mi += a[u] - mx - mi;
  }
  return {mx, mi};
}
int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  pair<long long, long long> res = dfs(1, 0);
  cout << res.first + abs(res.second) << endl;
}
