#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > e;
vector<long long> val;
vector<long long> mp, mn;
vector<int> deg;
vector<bool> vis;
pair<long long, long long> dfs(int x) {
  vis[x] = true;
  long long mx = 0, mi = 0;
  for (int to : e[x]) {
    if (!vis[to]) {
      auto p = dfs(to);
      mx = max(mx, p.first);
      mi = max(mi, p.second);
    }
  }
  long long d = val[x] + mx - mi;
  mp[x] = mx;
  mn[x] = mi;
  if (d > 0)
    mi += d;
  else
    mx -= d;
  return make_pair(mx, mi);
}
int main(int argc, char **argv) {
  int n;
  scanf("%d", &n);
  deg = vector<int>(n);
  mp = mn = val = vector<long long>(n);
  vis = vector<bool>(n);
  e = vector<vector<int> >(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    e[a - 1].push_back(b - 1);
    e[b - 1].push_back(a - 1);
    deg[a - 1]++;
    deg[b - 1]++;
  }
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    val[i] = t;
  }
  auto p = dfs(0);
  cout << p.first + p.second << endl;
  return 0;
}
