#include <bits/stdc++.h>
using namespace std;
const int MAX = 100050;
int n;
long long wyn;
int war[MAX];
vector<int> tab[MAX];
bool vis[MAX];
long long mini[MAX];
long long maks[MAX];
void DFS(int v, int par) {
  vis[v] = true;
  for (int i = 0; i < (int)tab[v].size(); i++)
    if (!vis[tab[v][i]]) DFS(tab[v][i], v);
  for (int i = 0; i < (int)tab[v].size(); i++)
    if (tab[v][i] != par) {
      maks[v] = max(maks[v], maks[tab[v][i]]);
      mini[v] = min(mini[v], mini[tab[v][i]]);
    }
  long long pom = war[v] + mini[v] + maks[v];
  if (pom < 0)
    maks[v] += pom * -1;
  else
    mini[v] += pom * -1;
}
int main() {
  int a = 0, b = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &a, &b);
    tab[a].push_back(b);
    tab[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &war[i]);
  DFS(1, 1);
  printf("%lld\n", mini[1] * -1 + maks[1]);
  return 0;
}
