#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N;
vector<int> adj[MAXN];
long long val[MAXN];
pair<long long, long long> go(int n, int p) {
  pair<long long, long long> ret(0, 0);
  for (int i = 0, _i = adj[n].size(); i < _i; ++i) {
    pair<long long, long long> tmp;
    if (adj[n][i] != p) {
      tmp = go(adj[n][i], n);
      ret.first = max(ret.first, tmp.first);
      ret.second = max(ret.second, tmp.second);
    }
  }
  long long delta = val[n] + ret.first - ret.second;
  if (delta > 0) {
    ret.second += delta;
  } else {
    ret.first += abs(delta);
  }
  return ret;
}
int main(int argc, char *argv[]) {
  scanf("%d", &N);
  for (int i = 0, _i = N - 1; i < _i; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0, _i = N; i < _i; ++i) {
    scanf("%I64d", val + i + 1);
  }
  pair<long long, long long> ret = go(1, -1);
  cout << ret.first + ret.second;
  return 0;
}
