#include <bits/stdc++.h>
using namespace std;
vector<long long> g[100100];
long long v[100100][4];
void dfs(long long a, long long f = 0) {
  long long* p = v[a];
  for (long long i = g[a].size(), b; i-- > 0;)
    if ((b = g[a][i]) != f) {
      dfs(b, a);
      p[1] = max(p[1], v[b][1]);
      p[2] = max(p[2], v[b][2]);
    }
  if ((p[0] += p[1] - p[2]) < 0)
    p[1] -= p[0];
  else
    p[2] += p[0];
}
int main(void) {
  long long n;
  cin >> n;
  for (long long i = 1, a, b; i < n && cin >> a >> b; ++i)
    g[a].push_back(b), g[b].push_back(a);
  for (long long i = 1; i <= n; ++i) cin >> v[i][0];
  dfs(1);
  cout << (v[1][1] + v[1][2]) << endl;
}
