#include <bits/stdc++.h>
using namespace std;
int ToInt(string& s, int len) {
  int r = 0;
  for (int i = 0, max_ = (len); i < max_; ++i) {
    r *= 10;
    r += s[i] - '0';
  }
  return r;
}
int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; }
int LCM(int a, int b) { return a * (b / GCD(a, b)); }
long long Pow(long long n, long long e) {
  if (e == 0) return 1;
  if (e == 1)
    return n;
  else if (e & 1) {
    long long t = Pow(n, e / 2);
    return n * t * t;
  } else {
    long long t = Pow(n, e / 2);
    return t * t;
  }
}
int val[100005];
vector<int> edge[100005];
bool vis[100005];
pair<long long, long long> DFS(int id, vector<int>& v) {
  if (vis[id]) return make_pair(-1, -1);
  vis[id] = true;
  v.push_back(val[id]);
  int nid;
  pair<long long, long long> res = make_pair(numeric_limits<long long>::min(),
                                             numeric_limits<long long>::min());
  for (int i = 0, max_ = ((int)(edge[id]).size()); i < max_; ++i) {
    nid = edge[id][i];
    pair<long long, long long> tmp = DFS(nid, v);
    res = make_pair(max(res.first, tmp.first), max(res.second, tmp.second));
  }
  if (res.first == -1 || res.second == -1) {
    res.first = 0;
    res.second = 0;
    long long curr = 0;
    for (int i = ((int)(v).size()) - 1; i >= 0; --i) {
      if (curr + v[i] > 0)
        res.first += abs(curr + v[i]);
      else
        res.second += abs(curr + v[i]);
      curr -= curr + v[i];
    }
  }
  v.pop_back();
  return res;
}
int main() {
  int N, a, b;
  scanf("%d", &N);
  for (int i = 0, max_ = (N - 1); i < max_; ++i) {
    scanf("%d %d", &a, &b);
    --a;
    --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  for (int i = 0, max_ = (N); i < max_; ++i) scanf("%d", &val[i]);
  vector<int> v;
  pair<long long, long long> res = DFS(0, v);
  printf("%I64d\n", res.first + res.second);
  return 0;
}
