#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll infll = 1ll * INT_MAX * INT_MAX;
const long double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
ll max(ll a, ll b) { return (a > b ? a : b); }
ll min(ll a, ll b) { return (a > b ? b : a); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
ll gcd(ll a, ll b, ll& x, ll& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll d = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
ll modinv(ll a, ll b) {
  ll x, y;
  gcd(a, b, x, y);
  return (x % b + b) % b;
}
ll ceiling(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
ll powm(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % m;
    b >>= 1;
    a = a * a % m;
  }
  return res;
}
const int N = 1e6 + 5;
ll n, m, q, t, k, x, y, z;
vector<ll> vis(N), v(N), a(N), b(N);
vector<vector<ll> > adj(N);
void dfs(ll i) {
  vis[i] = 1;
  for (auto it : adj[i]) {
    if (!vis[it]) {
      dfs(it);
      a[i] = max(a[i], a[it]);
      b[i] = max(b[i], b[it]);
    }
  }
  v[i] += a[i] - b[i];
  if (v[i] >= 0)
    b[i] += v[i];
  else
    a[i] += -v[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
  }
  dfs(1);
  cout << a[1] + b[1] << '\n';
}
