#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<vector<int> > tree;
vector<ll> values;
vector<bool> vis;
pair<ll, ll> dfs(int source) {
  vis[source] = true;
  ll ch_dec = 0;
  ll ch_inc = 0;
  for (int child : tree[source]) {
    if (vis[child] == false) {
      pair<ll, ll> chs = dfs(child);
      ch_inc = max(ch_inc, chs.first);
      ch_dec = max(ch_dec, chs.second);
    }
  }
  ll value = values[source] + ch_inc - ch_dec;
  if (value > 0) {
    ch_dec += value;
  } else {
    value = abs(value);
    ch_inc += value;
  }
  return make_pair(ch_inc, ch_dec);
}
int main() {
  ios_base::sync_with_stdio(0);
  ;
  cin >> n;
  tree.resize(n);
  values.resize(n);
  vis.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u -= 1;
    v -= 1;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  for (int i = 0; i < n; i++) {
    cin >> values[i];
  }
  pair<ll, ll> ans = dfs(0);
  cout << ans.first + ans.second << "\n";
  return 0;
}
