#include <bits/stdc++.h>
using namespace std;
long long int n, ar[100005], incre[100005], decre[100005], vis[100005];
vector<long long int> v[100005];
void dfs(long long int cur) {
  {
    incre[cur] = 0;
    decre[cur] = 0;
    for (long long int i : v[cur]) {
      if (vis[i] == 0) {
        vis[i] = 1;
        dfs(i);
        incre[cur] = max(incre[cur], incre[i]);
        decre[cur] = max(decre[cur], decre[i]);
      }
    }
    long long int temp = incre[cur] - decre[cur];
    if (ar[cur] + temp > 0)
      decre[cur] += ar[cur] + temp;
    else
      incre[cur] += (-(ar[cur] + temp));
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (long long int i = 0; i < n - 1; i++) {
    long long int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (long long int i = 1; i < n + 1; i++) cin >> ar[i];
  memset(vis, 0, sizeof(vis));
  vis[1] = 1;
  dfs(1);
  cout << incre[1] + decre[1] << '\n';
}
