#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int N;
long long int val[MAXN];
long long int increase[MAXN], decrease[MAXN];
vector<int> ways[MAXN];
inline void dfs(int nd, int dad) {
  for (int i = 0; i < (int)ways[nd].size(); i++)
    if (ways[nd][i] != dad) {
      dfs(ways[nd][i], nd);
      increase[nd] = max(increase[nd], increase[ways[nd][i]]);
      decrease[nd] = max(decrease[nd], decrease[ways[nd][i]]);
    }
  val[nd] += increase[nd] - decrease[nd];
  if (val[nd] < 0LL)
    increase[nd] -= val[nd];
  else
    decrease[nd] += val[nd];
}
int main() {
  cin >> N;
  for (int i = 1, a, b; i < N; i++) {
    scanf(" %d %d", &a, &b);
    ways[a].push_back(b);
    ways[b].push_back(a);
  }
  for (int i = 1; i <= N; i++) scanf(" %I64d", &val[i]);
  dfs(1, 0);
  cout << increase[1] + decrease[1] << endl;
  return 0;
}
