#include <bits/stdc++.h>
using namespace std;
int n, v[1000005];
vector<int> nei[1000005];
long long dp[2][1000005];
void dfs(int nv, int pa) {
  if (nei[nv].size() == 1 && pa != -1) {
    if (v[nv] > 0)
      dp[1][nv] = v[nv], dp[0][nv] = 0;
    else
      dp[0][nv] = -v[nv], dp[1][nv] = 0;
    return;
  }
  for (int i = 0; i < nei[nv].size(); i++) {
    if (nei[nv][i] == pa) continue;
    dfs(nei[nv][i], nv);
    dp[0][nv] = max(dp[0][nv], dp[0][nei[nv][i]]);
    dp[1][nv] = max(dp[1][nv], dp[1][nei[nv][i]]);
  }
  if (dp[0][nv] - dp[1][nv] + v[nv] < 0)
    dp[0][nv] += dp[1][nv] - v[nv] - dp[0][nv];
  else
    dp[1][nv] += v[nv] - dp[1][nv] + dp[0][nv];
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    nei[x].push_back(y);
    nei[y].push_back(x);
  }
  for (int i = 0; i < n; i++) cin >> v[i], dp[0][i] = -1e17, dp[1][i] = -1e17;
  dfs(0, -1);
  cout << dp[0][0] + dp[1][0];
  return 0;
}
