#include <bits/stdc++.h>
double eps = 1e-9;
using namespace std;
vector<vector<int> > v;
long long weight[100001];
pair<long long, long long> dfs(int source, int p) {
  long long maxp = 0, maxn = 0;
  for (int i = 0; i < v[source].size(); i++) {
    int front = v[source][i];
    if (front == p) {
      continue;
    }
    pair<long long, long long> curr = dfs(front, source);
    maxp = max(maxp, curr.first);
    maxn = max(maxn, curr.second);
  }
  if (weight[source] + maxp - maxn < 0) {
    return make_pair(maxp - (weight[source] + maxp - maxn), maxn);
  } else {
    return make_pair(maxp, weight[source] + maxp);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  v.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> weight[i + 1];
  }
  pair<long long, long long> ans = dfs(1, -1);
  cout << ans.first + ans.second;
  return 0;
}
