#include <bits/stdc++.h>
using namespace std;
void read(long long a = 0) {
  freopen("in", "r", stdin);
  if (a) freopen("out", "w", stdout);
}
long long mi[100009] = {}, mx[100009] = {};
long long a[100009] = {};
vector<vector<long long> > ad;
long long dfs(long long ind, long long par) {
  long long mivalue = 0, mxvalue = 0;
  for (int i = 0; i < ad[ind].size(); i++) {
    long long u = ad[ind][i];
    if (u != par) {
      dfs(u, ind);
      mivalue = max(mivalue, mi[u]);
      mxvalue = max(mxvalue, mx[u]);
    }
  }
  long long value = a[ind] + mivalue - mxvalue;
  if (value >= 0) {
    mx[ind] = mxvalue + abs(value);
    mi[ind] = mivalue;
  } else {
    mx[ind] = mxvalue;
    mi[ind] = mivalue + abs(value);
  }
  return mx[ind] + mi[ind];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  ad = vector<vector<long long> >(n + 1);
  for (int i = 0; i < n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    ad[x - 1].push_back(y - 1);
    ad[y - 1].push_back(x - 1);
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  cout << dfs(0, -1);
}
