#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
const int inf = mod;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
  if (a < 0) return -1;
  if (a > 0) return 1;
  return 0;
}
template <typename T>
inline void in(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
const int maxn = 200010;
vector<int> to[maxn];
long long val[maxn];
pair<long long, long long> dfs(int cur, int fa) {
  vector<pair<long long, long long> > son;
  for (auto i : to[cur]) {
    if (i != fa) son.push_back(dfs(i, cur));
  }
  pair<long long, long long> cnt = {0, 0};
  for (auto i : son) {
    cnt.first = max(cnt.first, i.first);
    cnt.second = max(cnt.second, i.second);
  }
  val[cur] += cnt.first - cnt.second;
  if (val[cur] < 0) {
    return make_pair(abs(val[cur]) + cnt.first, cnt.second);
  }
  return make_pair(0 + cnt.first, cnt.second + abs(val[cur]));
}
int main() {
  int n;
  in(n);
  for (long long i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  for (long long i = 1; i < n + 1; i++) in(val[i]);
  long long mx = 0, mn = 0;
  for (long long i = 2; i < n + 1; i++) {
    mx = max(mx, val[i]);
    mn = min(mn, val[i]);
  }
  pair<long long, long long> ans = dfs(1, 0);
  cout << ans.second + ans.first;
  return 0;
}
