#include <bits/stdc++.h>
using namespace std;
long long add[100001], sub[100001], num[100001];
void dfs(int pa, vector<int>* A, int s);
int main() {
  int n, a, b;
  cin >> n;
  vector<int> A[n + 1];
  for (int i = 0; i < n - 1; ++i) {
    cin >> a >> b;
    A[b].push_back(a);
    A[a].push_back(b);
  }
  for (int i = 1; i <= n; ++i) cin >> num[i];
  memset(add, 0, sizeof(add));
  memset(sub, 0, sizeof(sub));
  dfs(-1, A, 1);
  cout << add[1] + sub[1] << endl;
  return 0;
}
void dfs(int pa, vector<int>* A, int s) {
  vector<int>::iterator pos;
  for (pos = A[s].begin(); pos != A[s].end(); ++pos) {
    if (*pos != pa) {
      dfs(s, A, *pos);
      add[s] = max(add[s], add[*pos]);
      sub[s] = max(sub[s], sub[*pos]);
    }
  }
  num[s] += add[s] - sub[s];
  if (num[s] > 0)
    sub[s] += num[s];
  else
    add[s] -= num[s];
}
