#include <bits/stdc++.h>
inline long long sbt(long long x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long n;
long long a[100013], dp1[100013], dp2[100013];
vector<long long> adj[100013];
void dfs(long long node, long long par) {
  dp1[node] = dp2[node] = 0;
  for (auto nei : adj[node]) {
    if (nei != par) {
      dfs(nei, node);
      dp1[node] = max(dp1[node], dp1[nei]);
      dp2[node] = max(dp2[node], dp2[nei]);
    }
  }
  a[node] += dp1[node] - dp2[node];
  if (a[node] > 0) {
    dp2[node] += a[node];
  } else {
    dp1[node] += abs(a[node]);
  }
}
int32_t main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  cin >> n;
  long long m = n - 1;
  while (m--) {
    long long u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (long long i = 1; i <= n; i++) cin >> a[i];
  dfs(1, -1);
  cout << (dp1[1] + dp2[1]);
  return 0;
}
