#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjl;
vector<int> vis, arr;
vector<pair<long long, long long>> op;
void dfs(int u) {
  vis[u] = 1;
  long long pos = 0, neg = 0;
  for (auto v : adjl[u]) {
    if (vis[v] != 1) dfs(v);
    pos = max(pos, op[v].first), neg = max(neg, op[v].second);
  }
  if (arr[u] + pos - neg >= 0)
    op[u] = {pos, arr[u] + pos};
  else
    op[u] = {neg - arr[u], neg};
}
int main() {
  int n;
  cin >> n;
  adjl.resize(n), vis.assign(n, 0);
  arr.resize(n), op.resize(n);
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    u--, v--;
    adjl[u].push_back(v);
    adjl[v].push_back(u);
  }
  for (int &x : arr) cin >> x;
  dfs(0);
  cout << op[0].first + op[0].second << "\n";
  return 0;
}
