#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
vector<long long> v;
vector<vector<int>> g;
vector<long long> Inc, Dec;
int n;
void dfs(int u, int p) {
  for (auto v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    Inc[u] = max(Inc[u], Inc[v]);
    Dec[u] = max(Dec[u], Dec[v]);
  }
  v[u] += Inc[u] - Dec[u];
  if (v[u] > 0)
    Dec[u] += v[u];
  else
    Inc[u] -= v[u];
  return;
}
int main() {
  cin >> n;
  v = vector<long long>(n + 1);
  Inc = vector<long long>(n + 1);
  Dec = vector<long long>(n + 1);
  g = vector<vector<int>>(n + 1);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] > 0)
      Dec[i] = v[i];
    else
      Inc[i] = -v[i];
  }
  dfs(1, -1);
  cout << Inc[1] + Dec[1];
  return 0;
}
