#include <bits/stdc++.h>
using namespace std;
vector<long long> v[100000 + 10];
long long d[100000 + 10], u[100000 + 10];
bool mark[100000 + 10];
long long a[100000 + 10];
void dfs(long long x) {
  mark[x] = true;
  for (long long i = 0; i < v[x].size(); i++)
    if (!mark[v[x][i]]) {
      dfs(v[x][i]);
      d[x] = max(d[x], d[v[x][i]]);
      u[x] = max(u[x], u[v[x][i]]);
    }
  a[x] -= d[x];
  a[x] += u[x];
  if (a[x] < 0)
    u[x] += -1 * a[x];
  else
    d[x] += a[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long in, on;
    cin >> in >> on;
    v[in].push_back(on);
    v[on].push_back(in);
  }
  for (long long i = 1; i <= n; i++) cin >> a[i];
  dfs(1);
  cout << d[1] + u[1];
}
