#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
int par[100500];
bool used[100500];
long long ans1[100500], ans[100500];
long long w[100500];
int n;
vector<int> sm[100500];
long long lmax(const long long &a, const long long &b) {
  return (a < b) ? b : a;
}
int DFS(int x) {
  used[x] = true;
  ans1[x] = ans[x] = 0LL;
  for (int i = 0; i < (int)(sm[x].size()); ++i)
    if (!used[sm[x][i]]) {
      DFS(sm[x][i]);
      ans1[x] = lmax(ans1[x], ans1[sm[x][i]]);
      ans[x] = lmax(ans[sm[x][i]], ans[x]);
    }
  w[x] += ans[x] - ans1[x];
  if (w[x] > 0)
    ans1[x] += w[x];
  else
    ans[x] += (-w[x]);
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    sm[a].push_back(b);
    sm[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) scanf("%lld", w + i + 1);
  DFS(1);
  printf(
      "%lld"
      "\n",
      ans[1] + ans1[1]);
  return 0;
}
