#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 7;
long long const size_1d = 1e6 + 7;
long long const size_2d = 1e3 + 7;
long long power(long long x, long long y, long long mod = 2e18) {
  long long ans = 1;
  x %= mod;
  while (y) {
    if (y & 1) ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
long long modInverse(long long a, long long m) {
  long long m0 = m;
  long long y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long q = a / m;
    long long t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long gcdext(long long a, long long b, long long *x = 0, long long *y = 0) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd1 = gcdext(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd1;
}
int randomize() { return (rand() % 10000); }
std::vector<std::vector<int>> v(size_1d);
long long increase[size_1d], decrease[size_1d];
void dfs(int a, int b, std::vector<long long> &cost) {
  long long change = 0;
  long long inc = 0;
  long long dec = 0;
  for (auto &m : v[a]) {
    if (m != b) {
      dfs(m, a, cost);
      inc = max(increase[m], inc);
      dec = max(decrease[m], dec);
    }
  }
  cost[a] += inc - dec;
  increase[a] += inc;
  decrease[a] += dec;
  if (cost[a] >= 0)
    decrease[a] += cost[a];
  else
    increase[a] -= cost[a];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  std::vector<long long> cost(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> cost[i + 1];
  }
  dfs(1, 0, cost);
  cout << increase[1] + decrease[1] << endl;
  return 0;
}
