#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
long long int ans = 0, v[100005];
int visited[100005];
pair<long long int, long long int> dfs(int i) {
  visited[i] = 1;
  long long int decr = 0, incr = 0;
  for (int j = 0; j < adj[i].size(); j++) {
    if (!visited[adj[i][j]]) {
      pair<long long int, long long int> temp = dfs(adj[i][j]);
      decr = max(decr, temp.first);
      incr = max(incr, temp.second);
    }
  }
  v[i] -= decr;
  v[i] += incr;
  if (v[i] > 0)
    decr += v[i];
  else
    incr -= v[i];
  return make_pair(decr, incr);
}
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);
  }
  adj[0].push_back(1);
  adj[1].push_back(0);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  pair<long long int, long long int> ans = dfs(1);
  cout << ans.first + ans.second;
  return 0;
}
