#include <bits/stdc++.h>
using namespace std;
vector<int> to[100005];
long long dp[100005][2], a[100005];
void dfs(int v, int p) {
  long long mx0 = 0, mx1 = 0;
  for (int x : to[v]) {
    if (x == p) continue;
    dfs(x, v);
    mx1 = max(mx1, dp[x][1]);
    mx0 = max(mx0, dp[x][0]);
  }
  dp[v][1] += mx1;
  dp[v][0] += mx0;
  a[v] = a[v] - mx1 + mx0;
  if (a[v] > 0)
    dp[v][1] += a[v];
  else
    dp[v][0] -= a[v];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> v >> u;
    to[v].push_back(u);
    to[u].push_back(v);
  }
  for (int i = 1; i <= n; i++) cin >> a[i];
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1];
}
