#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n;
long long dp[N][2], a[N];
vector<int> v[N];
void dfs(int x, int fa) {
  long long maxn = 0, maxp = 0;
  int sz = (int)v[x].size();
  for (int i = 0; i < sz; i++) {
    int y = v[x][i];
    if (y == fa) continue;
    dfs(y, x);
    maxn = max(maxn, dp[y][0]);
    maxp = max(maxp, dp[y][1]);
  }
  int v = maxp - maxn;
  int opt = a[x] + v;
  dp[x][0] = maxn;
  dp[x][1] = maxp;
  if (opt > 0)
    dp[x][0] += opt;
  else
    dp[x][1] -= opt;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  for (int i = 1, x, y; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) cin >> a[i];
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1];
  return 0;
}
