#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 10;
long long n, a, b, v[maxN];
vector<int> adjList[maxN];
pair<long long, long long> solve(int cur, int fr) {
  pair<long long, long long> ret(0, 0);
  for (int i = 0; i < adjList[cur].size(); ++i)
    if (adjList[cur][i] != fr) {
      pair<long long, long long> res = solve(adjList[cur][i], cur);
      ret.first = min(ret.first, res.first);
      ret.second = max(ret.second, res.second);
    }
  v[cur] += ret.first + ret.second;
  if (v[cur] > 0)
    ret.first -= v[cur];
  else
    ret.second -= v[cur];
  return ret;
}
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n - 1; ++i) {
    scanf("%lld", &a), scanf("%lld", &b);
    adjList[a].push_back(b);
    adjList[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) scanf("%lld", &v[i + 1]);
  pair<long long, long long> res = solve(1, -1);
  printf("%lld\n", -res.first + res.second);
}
