#include <bits/stdc++.h>
using namespace std;
list<int> l[100005];
list<int> son[100005];
int n, dad[100005], x, y, val[100005], res;
void f(int a) {
  for (list<int>::iterator it = l[a].begin(); it != l[a].end(); it++)
    if ((*it) != dad[a]) {
      dad[*it] = a;
      f(*it);
      son[a].push_back(*it);
    }
}
pair<long long int, long long int> calc(int a) {
  pair<long long int, long long int> ret =
      pair<long long int, long long int>(0, 0);
  pair<long long int, long long int> tmp;
  for (list<int>::iterator it = son[a].begin(); it != son[a].end(); it++)
    if (*it != dad[a]) {
      tmp = calc(*it);
      ret = pair<long long int, long long int>(max(tmp.first, ret.first),
                                               max(tmp.second, ret.second));
    }
  int change = ret.first - ret.second;
  if (val[a] + change > 0)
    ret.second += abs(val[a] + change);
  else
    ret.first += abs(val[a] + change);
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d", &x, &y);
    l[x].push_back(y);
    l[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  f(1);
  pair<long long int, long long int> res = calc(1);
  cout << res.first + res.second << endl;
}
