#include <bits/stdc++.h>
using namespace std;
int n, val[100000];
vector<int> T[100000];
pair<long long, long long> dfs(int u, int pre) {
  long long plus = 0, minus = 0;
  for (int i = 0; i < (T[u].size()); i++) {
    int v = T[u][i];
    if (v != pre) {
      pair<long long, long long> tmp = dfs(v, u);
      plus = max(plus, tmp.first);
      minus = max(minus, tmp.second);
    }
  }
  long long val2 = val[u] + plus - minus;
  if (val2 > 0)
    minus += val2;
  else
    plus += -val2;
  return make_pair(plus, minus);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n - 1); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    T[u].push_back(v);
    T[v].push_back(u);
  }
  for (int u = 0; u < (n); u++) scanf("%d", val + u);
  pair<long long, long long> ans = dfs(0, -1);
  printf("%I64d\n", ans.first + ans.second);
  return 0;
}
