#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
long long cost[100005];
long long pos[100005];
long long neg[100005];
void dfs(int u, int p) {
  long long mx_plus = 0, mx_minus = 0;
  for (auto v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    mx_plus = max(mx_plus, pos[v]);
    mx_minus = max(mx_minus, neg[v]);
  }
  long long cur = cost[u] + mx_plus - mx_minus;
  neg[u] = mx_minus;
  pos[u] = mx_plus;
  if (cur >= 0) {
    neg[u] += cur;
  } else {
    pos[u] -= cur;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; 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 >> cost[i];
  }
  dfs(1, 0);
  long long ans = pos[1] + neg[1];
  cout << ans << endl;
  return 0;
}
