#include <bits/stdc++.h>
using namespace std;
long long n, i, j, dp[123456][2], x, y, a[123456];
vector<long long> adj[123456];
void dfs(long long x, long long fa) {
  long long i;
  for (i = 0; i < adj[x].size(); i++) {
    if (adj[x][i] == fa) continue;
    dfs(adj[x][i], x);
    dp[x][0] = max(dp[x][0], dp[adj[x][i]][0]);
    dp[x][1] = max(dp[x][1], dp[adj[x][i]][1]);
  }
  a[x] += dp[x][1] - dp[x][0];
  if (a[x] > 0)
    dp[x][0] += a[x];
  else
    dp[x][1] -= a[x];
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (i = 1; i <= n; i++) cin >> a[i];
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1] << endl;
  return 0;
}
