#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1000 * 100;
vector<int> adj[MAXn + 2];
int num[MAXn + 5];
pair<long long, long long> c[MAXn + 5];
bool mark[MAXn + 5];
void DFS(int v) {
  mark[v] = true;
  for (int i = 0; i < (int)adj[v].size(); i++)
    if (!mark[adj[v][i]]) {
      DFS(adj[v][i]);
      c[v].first = min(c[v].first, c[adj[v][i]].first);
      c[v].second = max(c[v].second, c[adj[v][i]].second);
    }
  long long val = num[v] - c[v].first - c[v].second;
  if (val < 0)
    c[v].first += val;
  else
    c[v].second += val;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < n; i++) scanf("%d", &num[i]);
  DFS(0);
  cout << c[0].first * -1 + c[0].second << endl;
  return 0;
}
