#include <bits/stdc++.h>
using namespace std;
long long int moves;
long long int vis[200005];
vector<long long int> v[200005];
long long int a[200005];
pair<long long int, long long int> dfs(long long int node, long long int inc,
                                       long long int dec) {
  vis[node] = 1;
  long long int inc1 = inc, dec1 = dec;
  for (auto i : v[node]) {
    if (vis[i] == 0) {
      pair<long long int, long long int> p1 = dfs(i, inc, dec);
      inc1 = max(inc1, p1.first);
      dec1 = max(dec1, p1.second);
    }
  }
  a[node] += inc1;
  a[node] -= dec1;
  if (a[node] < 0) {
    inc1 += abs(a[node]);
  } else {
    dec1 += a[node];
  }
  return {inc1, dec1};
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  for (long long int i = 1; i <= n - 1; i++) {
    long long int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (long long int i = 1; i <= n; i++) cin >> a[i];
  pair<long long int, long long int> p1 = dfs(1, 0, 0);
  cout << p1.first + p1.second << endl;
}
