#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int n;
long long myinc[100001], mydec[100001];
int val[100001];
void solve(int cur, int p = -1) {
  for (int i = 0; i < (v[cur].size()); i++) {
    int to = v[cur][i];
    if (to != p) {
      solve(to, cur);
      myinc[cur] = max(myinc[to], myinc[cur]);
      mydec[cur] = max(mydec[to], mydec[cur]);
    }
  }
  long long curv = val[cur] + myinc[cur] - mydec[cur];
  if (curv < 0)
    myinc[cur] += -curv;
  else
    mydec[cur] += curv;
}
int main() {
  scanf("%d", &n);
  v.resize(n);
  for (int i = 0; i < (n - 1); i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 0; i < (n); i++) scanf("%d", &val[i]);
  solve(0);
  cout << myinc[0] + mydec[0] << endl;
}
