#include <bits/stdc++.h>
using namespace std;
vector<int> vc[100001];
long long sum = 0;
int pnt[100001], on[100001], ar[100001], cnt[100001];
pair<long long, long long> dfs1(int id) {
  int a, b = 0, c, d, e, f, x, y, z;
  pair<long long, long long> rt, fin;
  fin.first = fin.second = 0;
  for (x = 0; x < vc[id].size(); x++) {
    if (pnt[id] != vc[id][x]) {
      pnt[vc[id][x]] = id;
      rt = dfs1(vc[id][x]);
      fin.first = max(fin.first, rt.first);
      fin.second = max(fin.second, rt.second);
    }
  }
  ar[id] += fin.first - fin.second;
  if (ar[id] <= 0)
    fin.first += (long long)(ar[id] * -1);
  else
    fin.second += (long long)ar[id];
  return fin;
}
int main() {
  int a, b, c, d, e, f, g, h, x, y, z;
  cin >> a;
  for (x = 1; x < a; x++) {
    cin >> b >> c;
    vc[b].push_back(c);
    vc[c].push_back(b);
  }
  for (x = 1; x <= a; x++) cin >> ar[x];
  pair<long long, long long> pp = dfs1(1);
  cout << pp.first + pp.second << endl;
}
