#include <bits/stdc++.h>
using namespace std;
long long int n, t1, t2, c[100005], vis[100005], M[100005], P[100005];
vector<long long int> adj[100005];
void dfs(long long int x) {
  vis[x] = 1;
  for (auto u : adj[x]) {
    if (!vis[u]) {
      dfs(u);
      M[x] = max(M[x], M[u]);
      P[x] = max(P[x], P[u]);
    }
  }
  long long int ne = P[x] - M[x] + c[x];
  if (ne > 0)
    M[x] += ne;
  else
    P[x] += -ne;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie();
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> t1 >> t2;
    adj[t1 - 1].push_back(t2 - 1);
    adj[t2 - 1].push_back(t1 - 1);
  }
  for (int i = 0; i < n; i++) cin >> c[i];
  dfs(0);
  cout << M[0] + P[0];
  return 0;
}
