#include <bits/stdc++.h>
using namespace std;
long long gcd1(long long a, long long b) {
  if (a == 0) return b;
  return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
  long long ans = 1LL, val = base;
  while (ex > 0LL) {
    if (ex & 1LL) ans = (ans * val) % 1000000007LL;
    val = (val * val) % 1000000007LL;
    ex = ex >> 1LL;
  }
  return ans;
}
const int maxn = 1e5 + 10;
long long n, a[maxn], dp[maxn][2], x, y;
vector<int> adj[maxn];
bool visit[maxn];
void dfs(int start) {
  visit[start] = true;
  for (int i = 0; i < adj[start].size(); i++) {
    int pt = adj[start][i];
    if (!visit[pt]) {
      dfs(pt);
      dp[start][0] = max(dp[start][0], dp[pt][0]);
      dp[start][1] = max(dp[start][1], dp[pt][1]);
    }
  }
  long long num = a[start] - dp[start][0] + dp[start][1];
  if (num >= 0)
    dp[start][0] += num;
  else
    dp[start][1] += -1LL * num;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) cin >> a[i];
  dfs(1);
  cout << dp[1][1] + dp[1][0] << endl;
  return 0;
}
