#include <bits/stdc++.h>
using namespace std;
double const eps = 1e-6;
const int inf = 0x3fffffff;
const int size = 100000 + 5;
vector<int> edge[size];
int v[size];
long long dp[size][2];
void tree_dp(int cur, int pre) {
  int sz = edge[cur].size();
  long long add = 0, sub = 0;
  for (int i = 0; i < sz; i++) {
    int v = edge[cur][i];
    if (v == pre) continue;
    tree_dp(v, cur);
    add = max(dp[v][0], add);
    sub = max(dp[v][1], sub);
  }
  long long t = v[cur] + add - sub;
  if (t < 0)
    add += -t;
  else
    sub += t;
  dp[cur][0] = add;
  dp[cur][1] = sub;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  tree_dp(1, 0);
  cout << dp[1][0] + dp[1][1] << endl;
}
