#include <bits/stdc++.h>
const int inf = INT_MAX;
const int nmax = 1e5 + 5;
const int mod = 1e9 + 7;
using namespace std;
long long vl[nmax], dp[nmax], dp1[nmax];
int viz[nmax], x, y, n, i;
vector<int> a[nmax];
void dfs(int x) {
  viz[x] = 1;
  for (int i = 0; i < a[x].size(); i++)
    if (!viz[a[x][i]]) {
      dfs(a[x][i]);
      dp[x] = max(dp[x], dp[a[x][i]]);
      dp1[x] = max(dp1[x], dp1[a[x][i]]);
    }
  long long tmp = vl[x] + dp[x] - dp1[x];
  if (tmp > 0)
    dp1[x] += tmp;
  else
    dp[x] += abs(tmp);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (i = 1; i <= n; i++) cin >> vl[i];
  dfs(1);
  cout << dp[1] + dp1[1] << endl;
  return 0;
}
