#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
long long N, M;
vector<vector<int> > G(MX);
long long arr[MX], dp1[MX], dp2[MX];
void DFS(int node, int par) {
  long long add = 0, sub = 0;
  for (int i = 0; i < G[node].size(); i++) {
    int guy = G[node][i];
    if (guy != par) {
      DFS(guy, node);
      add = max(add, dp1[guy]);
      sub = max(sub, dp2[guy]);
    }
  }
  long long val = arr[node];
  val += add;
  val -= sub;
  if (val >= 0)
    sub += val;
  else
    add += abs(val);
  dp1[node] = add;
  dp2[node] = sub;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= N; i++) cin >> arr[i];
  DFS(1, -1);
  cout << dp1[1] + dp2[1] << "\n";
  return 0;
}
