#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = ((res % p) * (x % p)) % p;
    y = y >> 1;
    x = ((x % p) * (x % p)) % p;
  }
  return res;
}
long long raichu(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = ((res) * (x));
    y = y >> 1;
    x = ((x) * (x));
  }
  return res;
}
bool isprime(long long n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  else {
    long long z = sqrt(n);
    for (long long i = 0; i < z - 1; i++)
      if (n % (i + 2) == 0) return false;
    return true;
  }
}
void dfs(long long c, long long p, vector<pair<long long, long long> > &dp,
         vector<long long> &v, vector<vector<long long> > &g) {
  long long in = 0, de = 0;
  for (auto i : g[c]) {
    if (i != p) {
      dfs(i, c, dp, v, g);
      in = max(in, dp[i].first);
      de = max(de, dp[i].second);
    }
  }
  v[c] += in;
  v[c] -= de;
  dp[c].first = max(0LL, -v[c]) + in;
  dp[c].second = max(0LL, v[c]) + de;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<vector<long long> > g(n);
    for (long long i = 0; i < n - 1; i++) {
      long long a, b;
      cin >> a >> b;
      g[--a].push_back(--b);
      g[b].push_back(a);
    }
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    vector<pair<long long, long long> > dp(n, make_pair(0, 0));
    dfs(0, -1, dp, v, g);
    cout << dp[0].first + dp[0].second;
  }
  return 0;
}
