#include <bits/stdc++.h>
using namespace std;
const int max_v = 1e5 + 15;
const int oo = 0x3f3f3f3f;
int V, x, y;
long long ret, niz[max_v], add[max_v], sub[max_v];
vector<int> adj[max_v];
void dfs(int curr, int prev) {
  for (vector<int>::iterator it = adj[curr].begin(); it != adj[curr].end();
       it++)
    if (*it != prev) {
      dfs(*it, curr);
      add[curr] = max(add[curr], add[*it]);
      sub[curr] = max(sub[curr], sub[*it]);
    }
  niz[curr] += add[curr] - sub[curr];
  if (niz[curr] > 0)
    sub[curr] += niz[curr];
  else
    add[curr] -= niz[curr];
}
int main() {
  cin >> V;
  for (int i = 1; i < V; i++) {
    cin >> x >> y;
    adj[--x].push_back(--y);
    adj[y].push_back(x);
  }
  for (int i = 0; i < V; i++) cin >> niz[i];
  dfs(0, -1);
  cout << *add + *sub;
  return 0;
}
