#include <bits/stdc++.h>
using namespace std;
vector<int> G[100002];
int val[100002];
long long inc[100002], decr[100002];
void dfs(int root, int parent) {
  for (int node : G[root]) {
    if (node == parent) continue;
    dfs(node, root);
    inc[root] = max(inc[root], inc[node]);
    decr[root] = max(decr[root], decr[node]);
  }
  val[root] += (inc[root] - decr[root]);
  if (val[root] >= 0)
    decr[root] += (long long)val[root];
  else
    inc[root] -= (long long)val[root];
}
int main() {
  ios_base::sync_with_stdio(false);
  long long n, i, a, b;
  memset(inc, 0, sizeof(inc));
  memset(decr, 0, sizeof(decr));
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (i = 1; i < n + 1; i++) cin >> val[i];
  dfs(1, 0);
  cout << inc[1] + decr[1];
  return 0;
}
