#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
long long arr[100001];
int visit[100001];
void dfs(int s, long long *pos, long long *neg) {
  visit[s] = 1;
  int i;
  long long k, maxi = 0, mini = 0, ps = 0, ng = 0;
  for (i = 0; i < v[s].size(); i++) {
    if (!visit[v[s][i]]) {
      dfs(v[s][i], &ps, &ng);
      if (ps > maxi) maxi = ps;
      if (ng < mini) mini = ng;
    }
  }
  *pos = maxi;
  *neg = mini;
  k = arr[s] + maxi + mini;
  if (k < 0)
    *pos = *pos - k;
  else
    *neg = *neg - k;
}
int main() {
  int n, i, a, b;
  long long pos = 0, neg = 0;
  scanf("%d", &n);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (i = 1; i <= n; i++) cin >> arr[i];
  dfs(1, &pos, &neg);
  cout << pos - neg;
  return 0;
}
