#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
int n;
long long a[MAX_N];
int par[MAX_N];
vector<int> G[MAX_N];
int mark[MAX_N];
long long b[MAX_N];
long long c[MAX_N];
void DFS(int u) {
  mark[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (mark[v] == 1) {
      continue;
    }
    par[v] = u;
    DFS(v);
  }
  if (G[u].size() == 1 && u != 1) {
    if (a[u] < 0) {
      b[u] = abs(a[u]);
      c[u] = 0;
      return;
    } else {
      b[u] = 0;
      c[u] = abs(a[u]);
      return;
    }
  }
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (par[u] == v) {
      continue;
    }
    b[u] = max(b[u], b[v]);
    c[u] = max(c[u], c[v]);
  }
  a[u] += b[u];
  a[u] -= c[u];
  if (a[u] < 0) {
    b[u] += abs(a[u]);
  } else {
    c[u] += a[u];
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  DFS(1);
  cout << b[1] + c[1] << endl;
}
