#include <bits/stdc++.h>
using namespace std;
vector<int> adia[100010];
long long cost[100010];
pair<long long, long long> dfs(int nod, int tata);
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  int n, a, b;
  cin >> n;
  for (int i(1); i < n; i++) {
    cin >> a >> b;
    adia[a].push_back(b);
    adia[b].push_back(a);
  }
  for (int i(1); i <= n; i++) cin >> cost[i];
  pair<long long, long long> ans(dfs(1, 0));
  cout << ans.first + ans.second;
  return 0;
}
pair<long long, long long> dfs(int nod, int tata) {
  pair<long long, long long> ans({0, 0});
  for (auto i : adia[nod]) {
    if (i == tata) continue;
    pair<long long, long long> r(dfs(i, nod));
    if (r.first > ans.first) ans.first = r.first;
    if (r.second > ans.second) ans.second = r.second;
  }
  cost[nod] += ans.first - ans.second;
  if (cost[nod] > 0)
    ans.second += cost[nod];
  else
    ans.first -= cost[nod];
  return ans;
}
