#include <bits/stdc++.h>
using namespace std;
long long arr[(int)1e5 + 9];
bool vis[(int)1e5 + 9];
vector<vector<int>> adj;
long long n;
long long r;
pair<long long, long long> solve(int ind) {
  if (vis[ind]) return {0, 0};
  vis[ind] = 1;
  long long a = 0, b = 0;
  for (int i = 0; i < adj[ind].size(); i++) {
    pair<long long, long long> k = solve(adj[ind][i]);
    a = max(a, k.first);
    b = max(b, k.second);
  }
  arr[ind] += a - b;
  if (arr[ind] >= 0) return {a, b + arr[ind]};
  return {a - arr[ind], b};
}
int main() {
  scanf("%I64d", &n);
  adj = vector<vector<int>>(n + 9);
  for (int i = 0; i < n - 1; i++) {
    long long x, y;
    scanf("%I64d%I64d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%I64d", arr + i);
  pair<long long, long long> vv = solve(1);
  cout << vv.first + vv.second;
  return 0;
}
