#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
static bool constexpr dbg = 0;
ll constexpr NM = 1.1e5;
ll n, res;
ll v[NM];
vector<ll> adj[NM];
void init() {
  cin >> n;
  for (ll i = 1; i <= (ll)(n - 1); ++i) {
    ll u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (ll i = 1; i <= (ll)(n); ++i) cin >> v[i];
}
pll dfs(ll cur, ll p = 0) {
  pll ret = {0, 0};
  for (auto ch : adj[cur])
    if (ch != p) {
      auto r = dfs(ch, cur);
      ret.first = max(ret.first, r.first);
      ret.second = max(ret.second, r.second);
    }
  ll rem = v[cur] + ret.first - ret.second;
  return rem > 0 ? pll(ret.first, ret.second + rem)
                 : pll(ret.first - rem, ret.second);
}
void solve() {
  auto r = dfs(1);
  res += r.first + r.second;
  cout << res << '\n';
}
int main() {
  if (!dbg) {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
  init();
  solve();
  return 0;
}
