#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 1, mod = 1e9 + 7;
long long ans = 0;
vector<long long> g[maxn];
vector<long long> val(maxn), add(maxn), sub(maxn);
void dfs(long long cur, long long par) {
  for (auto it : g[cur]) {
    if (it == par) continue;
    dfs(it, cur);
    add[cur] = max(add[cur], add[it]);
    sub[cur] = max(sub[cur], sub[it]);
  }
  ans += add[cur];
  ans += sub[cur];
  val[cur] -= add[cur];
  val[cur] += sub[cur];
  add[cur] += max(0LL, val[cur]);
  sub[cur] += max(0LL, -1 * val[cur]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (long long i = 1; i <= n; i++) cin >> val[i];
  dfs(1, 1);
  cout << add[1] + sub[1];
  return 0;
}
