#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 69;
vector<int> adj[mxn];
long long v[mxn];
long long dp1[mxn] = {};
long long dp2[mxn] = {};
void dfs(int cur, int prev) {
  if (v[cur] > 0) {
    dp1[cur] = 0;
    dp2[cur] = v[cur];
  } else {
    dp1[cur] = -v[cur];
    dp2[cur] = 0;
  }
  for (int i : adj[cur]) {
    if (i == prev) continue;
    dfs(i, cur);
    dp1[cur] = max(dp1[cur], dp1[i]);
    dp2[cur] = max(dp2[cur], dp2[i]);
  }
  if (dp1[cur] >= dp2[cur]) {
    if (v[cur] >= 0) {
      dp2[cur] = dp1[cur] + v[cur];
    } else {
      if (dp1[cur] - dp2[cur] >= abs(v[cur])) {
        dp2[cur] = dp1[cur] - abs(v[cur]);
      } else
        dp1[cur] = dp2[cur] + abs(v[cur]);
    }
  } else {
    if (v[cur] >= 0) {
      if (dp2[cur] - dp1[cur] >= v[cur]) {
        dp1[cur] = dp2[cur] - v[cur];
      } else {
        dp2[cur] = dp1[cur] + v[cur];
      }
    } else {
      dp1[cur] = dp2[cur] + abs(v[cur]);
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) cin >> v[i];
  dfs(1, 0);
  cout << dp1[1] + dp2[1] << endl;
  return 0;
}
