#include <bits/stdc++.h>
using namespace std;
long long int n, a, b;
vector<vector<long long int>> graph;
vector<long long int> value;
vector<bool> vis;
pair<long long int, long long int> dfs(long long int node) {
  vis[node] = true;
  pair<long long int, long long int> ret = make_pair(0, 0);
  for (auto i : graph[node]) {
    if (!vis[i]) {
      pair<long long int, long long int> y = dfs(i);
      ret.first = max(ret.first, y.first);
      ret.second = max(ret.second, y.second);
    }
  }
  long long int val = value[node];
  val = val - ret.first + ret.second;
  if (val > 0) ret.first += val;
  if (val < 0) ret.second += (-val);
  return ret;
}
int main() {
  cin >> n;
  graph.resize(n);
  value.resize(n);
  vis.resize(n), vis.assign(n, false);
  for (long long int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (long long int i = 0; i < n; i++) cin >> value[i];
  pair<long long int, long long int> ans = dfs(0);
  cout << ans.first + ans.second << endl;
  return 0;
}
