#include <bits/stdc++.h>
using namespace std;
long long ans;
pair<long long, long long> dfs(vector<long long> adj[], bool visited[],
                               long long x, long long &n, long long val[]) {
  visited[x] = true;
  pair<long long, long long> ans = {0, 0};
  for (auto i : adj[x]) {
    if (!visited[i]) {
      pair<long long, long long> temp = dfs(adj, visited, i, n, val);
      ans.first = max(ans.first, temp.first);
      ans.second = max(ans.second, temp.second);
    }
  }
  long long lav = val[x] + ans.first - ans.second;
  if (lav < 0)
    ans.first = ans.first + abs(lav);
  else
    ans.second = ans.second + lav;
  return ans;
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, j;
  long long n;
  cin >> n;
  vector<long long> adj[n];
  for (i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  long long val[n];
  for (i = 0; i < n; i++) cin >> val[i];
  long long root;
  for (i = 0; i < n; i++) {
    if (val[i] == 1) root = i;
  }
  bool visited[n];
  fill(visited, visited + n, false);
  pair<long long, long long> res = dfs(adj, visited, 0, n, val);
  cout << res.first + res.second << "\n";
  return 0;
}
