#include <bits/stdc++.h>
using namespace std;
vector<long long> min_inc, min_dec, val;
vector<vector<int> > e;
void visit(int parent, int v) {
  for (int i = 0; i < e[v].size(); ++i) {
    if (e[v][i] != parent) {
      visit(v, e[v][i]);
    }
  }
  if (e[v].size() == 1 && v != 0) {
    if (val[v] >= 0) {
      min_dec[v] = val[v];
      min_inc[v] = 0;
    } else {
      min_dec[v] = 0;
      min_inc[v] = -val[v];
    }
  } else {
    long long ch_max_inc = 0, ch_max_dec = 0;
    for (int i = 0; i < e[v].size(); ++i) {
      if (e[v][i] != parent) {
        ch_max_inc = max(ch_max_inc, min_inc[e[v][i]]);
        ch_max_dec = max(ch_max_dec, min_dec[e[v][i]]);
      }
    }
    int new_val = val[v] + ch_max_inc - ch_max_dec;
    if (new_val >= 0) {
      min_dec[v] = ch_max_dec + new_val;
      min_inc[v] = ch_max_inc;
    } else {
      min_dec[v] = ch_max_dec;
      min_inc[v] = ch_max_inc - new_val;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  e.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  val.resize(n);
  copy_n(istream_iterator<int>(cin), n, val.begin());
  min_inc.resize(n);
  min_dec.resize(n);
  visit(-1, 0);
  cout << min_inc[0] + min_dec[0];
  return 0;
}
