#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[100000 + 5];
long long cost[100000 + 5], vis[100000 + 5], pluss[100000 + 5],
    minuss[100000 + 5], ans[100000 + 5];
void dfs(long long u) {
  vis[u] = true;
  for (long long i = 0; i < graph[u].size(); i++) {
    long long v = graph[u][i];
    if (vis[v] == 0) {
      dfs(v);
      minuss[u] = max(minuss[u], minuss[v]);
      pluss[u] = max(pluss[u], pluss[v]);
    }
  }
  cost[u] += pluss[u];
  cost[u] -= minuss[u];
  if (cost[u] >= 0)
    minuss[u] += cost[u];
  else
    pluss[u] += abs(cost[u]);
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long frm, to;
    scanf("%lld %lld", &frm, &to);
    graph[frm].push_back(to);
    graph[to].push_back(frm);
  }
  for (long long i = 1; i <= n; i++) scanf("%lld", &cost[i]);
  dfs(1);
  cout << pluss[1] + minuss[1] << endl;
}
