#include <bits/stdc++.h>
using namespace std;
vector<long long> a[111111];
long long n, val[111111], mx[111111], mn[111111];
bool vis[111111];
void dfs(long long x) {
  vis[x] = true;
  for (int i = 0; i < a[x].size(); i++)
    if (!vis[a[x][i]]) {
      dfs(a[x][i]);
      mn[x] = (mn[x] < mn[a[x][i]] ? mn[a[x][i]] : mn[x]);
      mx[x] = (mx[x] < mx[a[x][i]] ? mx[a[x][i]] : mx[x]);
    }
  val[x] += -mn[x] + mx[x];
  if (val[x] > 0)
    mn[x] += abs(0 - val[x]);
  else
    mx[x] += abs(0 - val[x]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (int i = 0; i < n; i++) cin >> val[i];
  dfs(0);
  cout << mx[0] + mn[0];
}
