#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long N = 3 * 1e5 + 1;
long long dp1[N], dp2[N];
vector<long long> gr[N];
long long color[N];
void dfs(long long v, long long pr = -1) {
  for (auto it : gr[v]) {
    if (it != pr) {
      dfs(it, v);
      dp1[v] = max(dp1[v], dp1[it]);
      dp2[v] = max(dp2[v], dp2[it]);
    }
  }
  color[v] += dp1[v] - dp2[v];
  if (color[v] > 0) {
    dp2[v] += color[v];
  } else {
    dp1[v] -= color[v];
  }
}
signed main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  for (long long i = 0; i < n; i++) cin >> color[i];
  dfs(0);
  cout << dp1[0] + dp2[0] << '\n';
}
