#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100025];
long long int incr[100025], vis[100025], decr[100025], val[100025];
void dfs(int v, int par) {
  for (int i = 0; i < adj[v].size(); i++) {
    if (adj[v][i] == par) continue;
    dfs(adj[v][i], v);
    incr[v] = max(incr[v], incr[adj[v][i]]);
    decr[v] = max(decr[v], decr[adj[v][i]]);
  }
  val[v] += (incr[v] - decr[v]);
  if (val[v] > 0)
    decr[v] += val[v];
  else
    incr[v] += abs(val[v]);
  return;
}
int main() {
  int n, x, y;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) cin >> val[i];
  dfs(1, 0);
  cout << incr[1] + decr[1];
}
