#include <bits/stdc++.h>
using namespace std;
long long fr[100009];
long long num[100009];
bool visited[100009];
vector<int> adjlist[100009];
long long o0 = 0;
pair<long long, long long> go(int x) {
  visited[x] = 1;
  if (fr[x] == 1 && x != 1)
    return pair<long long, long long>(-min(o0, num[x]), max(o0, num[x]));
  pair<long long, long long> ans;
  ans = pair<long long, long long>(o0, o0);
  for (int i = 0; i < (int)adjlist[x].size(); i++)
    if (!visited[adjlist[x][i]]) {
      pair<long long, long long> o = go(adjlist[x][i]);
      ans.first = max(ans.first, o.first);
      ans.second = max(ans.second, o.second);
    }
  num[x] += ans.first - ans.second;
  pair<long long, long long> a;
  a = pair<long long, long long>(-min(o0, num[x]), max(o0, num[x]));
  ans.first += a.first;
  ans.second += a.second;
  return ans;
}
int main() {
  memset(fr, 0, sizeof(fr));
  memset(visited, 0, sizeof(visited));
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    fr[x]++;
    fr[y]++;
    adjlist[x].push_back(y);
    adjlist[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%lld", &num[i]);
  pair<long long, long long> ans = go(1);
  cout << ans.first + ans.second;
  return 0;
}
