#include <bits/stdc++.h>
using namespace std;
long long c[100010], a[100010], b[100010];
vector<int> G[100100];
void dfs(int u, int par = 0) {
  for (int i = 0; i < G[u].size(); i++) {
    if (G[u][i] != par) {
      dfs(G[u][i], u);
      a[u] = max(a[u], a[G[u][i]]);
      b[u] = max(b[u], b[G[u][i]]);
    }
  }
  c[u] += a[u];
  c[u] -= b[u];
  if (c[u] < 0)
    a[u] += -c[u];
  else
    b[u] += c[u];
}
int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) cin >> c[i];
  dfs(1);
  cout << a[1] + b[1] << endl;
  return 0;
}
