#include <bits/stdc++.h>
using namespace std;
map<long long, vector<long long> > edgel;
long long visited[100001];
long long score[100001];
pair<long long, long long> rec(long long root) {
  visited[root] = 1;
  long long childs = 0;
  for (long long i = 0; i < edgel[root].size(); ++i) {
    if (visited[edgel[root][i]] == 0) childs++;
  }
  if (childs == 0)
    return make_pair((((0) > (score[root])) ? (0) : (score[root])),
                     (((0) > (-score[root])) ? (0) : (-score[root])));
  std::vector<pair<long long, long long> > v;
  for (long long i = 0; i < edgel[root].size(); ++i) {
    if (visited[edgel[root][i]] == 0) v.push_back(rec(edgel[root][i]));
  }
  long long neg = v[0].first, pos = v[0].second;
  for (long long i = 0; i < v.size(); ++i) {
    if (v[i].first > neg) neg = v[i].first;
    if (v[i].second > pos) pos = v[i].second;
  }
  pair<long long, long long> result;
  result = make_pair(neg, pos);
  long long root_score = score[root] - result.first + result.second;
  if (root_score > 0) result.first += root_score;
  if (root_score < 0) result.second += (-root_score);
  return result;
}
int main() {
  long long n;
  cin >> n;
  long long a, b;
  for (long long i = 0; i < n - 1; i++) {
    cin >> a >> b;
    if (edgel.find(a) == edgel.end()) {
      vector<long long> tmp;
      edgel[a] = tmp;
    }
    edgel[a].push_back(b);
    if (edgel.find(b) == edgel.end()) {
      vector<long long> tmp;
      edgel[b] = tmp;
    }
    edgel[b].push_back(a);
  }
  for (long long i = 1; i <= n; ++i) {
    cin >> score[i];
  }
  for (long long i = 0; i < 100001; ++i) {
    visited[i] = 0;
  }
  pair<long long, long long> result = rec(1);
  cout << result.first + result.second;
  return 0;
}
