#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
const ll LINF = 1e18 + 5;
int n, v[N];
ll dp[N][2];
vector<int> G[N];
void dfs(int u, int fa) {
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs(v, u);
    dp[u][0] = max(dp[v][0], dp[u][0]);
    dp[u][1] = max(dp[v][1], dp[u][1]);
  }
  v[u] += -dp[u][0] + dp[u][1];
  if (v[u] < 0)
    dp[u][1] -= v[u];
  else
    dp[u][0] += v[u];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) cin >> v[i];
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1];
  return 0;
}
