#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> adj[N];
long long f[N], c[N];
void dfs(int v, int p = -1) {
  long long mxv = 0;
  for (auto u : adj[v]) {
    if (u == p) continue;
    dfs(u, v);
    f[v] = max(f[v], f[u]);
    mxv = max(mxv, f[u] + c[u]);
  }
  if (c[v] + f[v] < 0) f[v] = -c[v];
  if (c[v] + f[v] < mxv) f[v] = mxv - c[v];
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; 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 >> c[i];
  dfs(1);
  cout << f[1] + (c[1] + f[1]) << '\n';
  ;
}
