#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 7;
long long n, a[N], dp[N][2], u, v;
std::vector<long long> mp[N];
inline void dfs(long long now, long long pre) {
  long long add = 0, dec = 0;
  for (long long i = 0; i < mp[now].size(); i++) {
    long long nex = mp[now][i];
    if (nex != pre) dfs(nex, now);
    add = max(add, dp[nex][0]);
    dec = max(dec, dp[nex][1]);
  }
  dp[now][0] = add;
  dp[now][1] = dec;
  if (a[now] + add - dec >= 0)
    dp[now][1] += a[now] + add - dec;
  else
    dp[now][0] -= a[now] + add - dec;
}
signed main(signed argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i < n; i++) {
    cin >> u >> v;
    mp[u].push_back(v);
    mp[v].push_back(u);
  }
  for (long long i = 1; i <= n; i++) cin >> a[i];
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1] << endl;
  return 0;
}
