#include <bits/stdc++.h>
using namespace std;
vector<int> edge[100005];
vector<long long int> ma;
vector<long long int> mi;
vector<int> value;
vector<bool> check;
void c(int i) {
  long long mas = 0, mis = 0;
  int j, k, s = edge[i].size();
  check[i] = true;
  for (j = 0; j < s; ++j) {
    k = edge[i][j];
    if (!check[k]) {
      c(k);
      if (ma[k] > mas) mas = ma[k];
      if (mi[k] < mis) mis = mi[k];
    }
  }
  k = value[i] - mas - mis;
  if (k > 0)
    mas += k;
  else
    mis += k;
  ma[i] = mas;
  mi[i] = mis;
}
int main() {
  int i, j, k, n;
  long long result;
  cin >> n;
  for (i = 0; i < n - 1; ++i) {
    cin >> j >> k;
    edge[j].push_back(k);
    edge[k].push_back(j);
  }
  for (i = 0; i <= n; ++i) ma.push_back(0);
  for (i = 0; i <= n; ++i) mi.push_back(0);
  value.push_back(0);
  for (i = 0; i < n; ++i) {
    cin >> j;
    value.push_back(j);
  }
  for (i = 0; i <= n; ++i) check.push_back(false);
  c(1);
  result = value[1] - ma[1] - mi[1];
  if (result > 0)
    cout << ma[1] - mi[1] + result << endl;
  else
    cout << ma[1] - mi[1] - result << endl;
  return 0;
}
