#include <bits/stdc++.h>
using namespace std;
long long n, add[120000], ext[120000];
long long t[120000];
vector<int> a[120000];
int x, y;
void dfs(int v, int pred) {
  for (int i = 0; i < a[v].size(); i++)
    if (a[v][i] != pred) {
      dfs(a[v][i], v);
      ext[v] = max(ext[v], ext[a[v][i]]);
      add[v] = max(add[v], add[a[v][i]]);
    }
  t[v] -= ext[v];
  t[v] += add[v];
  if (t[v] < 0)
    add[v] -= t[v];
  else
    ext[v] += t[v];
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) cin >> t[i];
  dfs(1, -1);
  cout << add[1] + ext[1];
  return 0;
}
