#include <bits/stdc++.h>
using namespace std;
void solveA() {
  int n;
  cin >> n;
  vector<long long> cnt(n);
  vector<vector<long long> > vptr(n);
  vector<pair<long long, long long> > acc(n);
  for (int i = 0; i < int(n - 1); i++) {
    long long a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    cnt[a]++;
    cnt[b]++;
    vptr[a].push_back(b);
    vptr[b].push_back(a);
  }
  vector<long long> val(n);
  for (int i = 0; i < int(n); i++) {
    long long v;
    cin >> v;
    val[i] = v;
  }
  set<long long> lv;
  for (int i = int(1); i <= int(n - 1); i++) {
    if (cnt[i] == 1) lv.insert(i);
  }
  vector<bool> used(n, false);
  while (lv.size() != 0) {
    long long cur = *(lv.begin());
    lv.erase(lv.begin());
    used[cur] = true;
    long long par = -1;
    for (int i = 0; i < int(vptr[cur].size()); i++) {
      if (!used[vptr[cur][i]]) {
        par = vptr[cur][i];
        break;
      }
    }
    cnt[par]--;
    long long r = acc[cur].first + acc[cur].second;
    long long curv = r + val[cur];
    if (curv > 0)
      acc[cur].second -= curv;
    else
      acc[cur].first -= curv;
    acc[par].first = max(acc[cur].first, acc[par].first);
    acc[par].second = min(acc[cur].second, acc[par].second);
    if (cnt[par] == 1 && par != 0) lv.insert(par);
  }
  long long cur = 0l;
  long long r = acc[cur].first + acc[cur].second;
  long long curv = r + val[cur];
  if (curv > 0)
    acc[cur].second -= curv;
  else
    acc[cur].first -= curv;
  cout << acc[0].first - acc[0].second << endl;
}
int main() { solveA(); }
