#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
vector<long long> v[N];
long long val[N], add[N], subtract[N];
void dfs(int node, int par) {
  for (auto it : v[node]) {
    if (it != par) {
      dfs(it, node);
      add[node] = max(add[node], add[it]);
      subtract[node] = max(subtract[node], subtract[it]);
    }
  }
  long long z = (add[node] - subtract[node]);
  val[node] += z;
  if (val[node] < 0) {
    add[node] -= val[node];
  } else {
    subtract[node] += val[node];
  }
}
int main() {
  int node;
  scanf("%d", &node);
  ;
  for (int i = 1; i < node; i++) {
    int a, b;
    scanf("%d", &a);
    ;
    scanf("%d", &b);
    ;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 1; i <= node; i++) scanf("%lld", &val[i]);
  ;
  dfs(1, 0);
  cout << add[1] + subtract[1] << "\n";
}
