#include <bits/stdc++.h>
using namespace std;
void _fill_int(int* p, int val, int rep) {
  int i;
  for (i = 0; i < rep; i++) p[i] = val;
}
int GETi() {
  int i;
  scanf("%d", &i);
  return i;
}
template <class T>
T sqr(T val) {
  return val * val;
}
int N;
vector<int> E[100001];
vector<signed long long> V;
pair<signed long long, signed long long> dfs(int cur, int pre) {
  signed long long l = 0, h = 0, tv;
  int i;
  for (i = 0; i < E[cur].size(); i++) {
    if (E[cur][i] == pre) continue;
    pair<signed long long, signed long long> tt = dfs(E[cur][i], cur);
    l = max(l, tt.first);
    h = max(h, tt.second);
  }
  tv = V[cur] + h - l;
  if (tv >= 0) return make_pair(l + tv, h);
  return make_pair(l, h - tv);
}
void solve() {
  int f, r, i, j, k, l, x, y;
  N = GETi();
  for (i = 0; i < N - 1; i++) {
    j = GETi() - 1;
    l = GETi() - 1;
    E[j].push_back(l);
    E[l].push_back(j);
  }
  for (i = 0; i < N; i++) V.push_back((signed long long)GETi());
  pair<signed long long, signed long long> tval = dfs(0, -1);
  cout << tval.first + tval.second << endl;
  return;
}
int main(int argc, char** argv) {
  if (argc > 1) freopen(argv[1], "r", stdin);
  solve();
  return 0;
}
