#include <bits/stdc++.h>
int t1, t2, n;
bool g[100100];
long long dp[100100][2];
long long x[100100];
std::vector<int> v[100100];
void s(int k) {
  g[k] = true;
  dp[k][0] = 0;
  dp[k][1] = 0;
  for (int i = 0; i < v[k].size(); ++i) {
    int c = v[k][i];
    if (!g[c]) {
      s(c);
      dp[k][0] = std::max(dp[k][0], dp[c][0]);
      dp[k][1] = std::max(dp[k][1], dp[c][1]);
    }
  }
  x[k] += dp[k][0] - dp[k][1];
  if (x[k] > 0)
    dp[k][1] += x[k];
  else
    dp[k][0] -= x[k];
};
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d", &t1, &t2);
    v[t1].push_back(t2);
    v[t2].push_back(t1);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &x[i]);
    g[i] = false;
  }
  s(1);
  printf("%I64d\n", dp[1][0] + dp[1][1]);
  return 0;
}
