#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long mod = 1e9 + 7;
long long fmod(long long b, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
long long A[N];
long long dp[N][2];
vector<int> adj[N];
void dfs(int i, int p) {
  for (auto ve : adj[i]) {
    if (p - ve) {
      dfs(ve, i);
      dp[i][0] = max(dp[i][0], dp[ve][0]);
      dp[i][1] = max(dp[i][1], dp[ve][1]);
    }
  }
  long long val = A[i] + dp[i][0] - dp[i][1];
  if (val > 0)
    dp[i][1] += val;
  else
    dp[i][0] -= val;
}
int main() {
  int t = 1, n, i, j, u, v;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (i = 1; i <= n; i++) cin >> A[i];
  dfs(1, -1);
  cout << (dp[1][0] + dp[1][1]) << "\n";
  return 0;
}
