#include <bits/stdc++.h>
using namespace std;
long long n;
vector<int> g[100005];
long long w[100005];
pair<long long, long long> sol[100005];
long long p[100005];
void scan(void) {
  cin >> n;
  for (long long i = (0); i < (n - 1); i++) {
    long long a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (long long i = (1); i < (n + 1); i++) cin >> w[i];
}
void dfs(long long v) {
  for (long long i = (0); i < (g[v].size()); i++)
    if (g[v][i] != p[v]) p[g[v][i]] = v, dfs(g[v][i]);
}
void el(long long v) {
  sol[v].first = sol[v].second = 0;
  for (long long i = (0); i < (g[v].size()); i++) {
    long long q = g[v][i];
    if (q == p[v]) continue;
    el(q);
    sol[v].first = max(sol[v].first, sol[q].first);
    sol[v].second = max(sol[v].second, sol[q].second);
  }
  long long k = w[v] + sol[v].second - sol[v].first;
  if (k > 0) sol[v].first += k;
  if (k < 0) sol[v].second -= k;
}
long long solve(void) {
  dfs(1);
  el(1);
  return sol[1].first + sol[1].second;
}
int main(void) {
  scan();
  cout << solve() << "\n";
  return 0;
}
