#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int cas = 1;
int dxx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dyy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
long long ai[100006], vs[100006], val[100006];
vector<long long> vc[100006];
pair<long long, long long> dfs(long long n) {
  vs[n] = 1;
  long long mi, mx, i, ck = 0;
  pair<long long, long long> a;
  mi = 1000000007;
  mx = -mi;
  for (i = 0; i < vc[n].size(); i++) {
    if (vs[vc[n][i]] == 0) {
      a = dfs(vc[n][i]);
      if (a.first < mi) mi = a.first;
      if (a.second > mx) mx = a.second;
      ck = 1;
    }
  }
  if (ck == 0) {
    if (ai[n] > 0) return make_pair(0, ai[n]);
    return make_pair(ai[n], 0);
  }
  ai[n] -= mx;
  ai[n] -= mi;
  if (ai[n] > 0)
    mx += ai[n];
  else
    mi += ai[n];
  return make_pair(mi, mx);
}
int main() {
  long long n, i, j, u, v, k, l;
  memset(val, 0, sizeof(val));
  scanf("%lld", &n);
  for (i = 1; i < n; i++) {
    scanf("%lld%lld", &u, &v);
    vc[u].push_back(v);
    vc[v].push_back(u);
  }
  for (i = 1; i <= n; i++) {
    scanf("%lld", &ai[i]);
  }
  pair<long long, long long> p = dfs(1);
  long long ans = p.second - p.first;
  printf("%lld\n", ans);
}
