#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
vector<int> cn[MAXN];
int n;
long long V[MAXN], dp[MAXN][2];
void dfs(int u, int fa) {
  long long sum = 0, tmp1 = 0, tmp2 = 0;
  for (int i = 0; i < cn[u].size(); i++) {
    int v = cn[u][i];
    if (v == fa) {
      continue;
    }
    dfs(v, u);
    tmp1 = max(tmp1, dp[v][0]);
    tmp2 = max(tmp2, dp[v][1]);
  }
  sum = tmp1 - tmp2;
  dp[u][0] += tmp1;
  dp[u][1] += tmp2;
  if (sum + V[u] > 0) {
    dp[u][1] += (sum + V[u]);
  } else {
    dp[u][0] += abs(sum + V[u]);
  }
}
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      cn[i].clear();
    }
    for (int i = 1; i <= n - 1; i++) {
      int u, v;
      cin >> u >> v;
      cn[u].push_back(v);
      cn[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
      cin >> V[i];
    }
    memset(dp, 0, sizeof(dp));
    dfs(1, -1);
    cout << dp[1][0] + dp[1][1] << endl;
  }
  return 0;
}
