#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n;
long long w[N], ic[N], dc[N];
vector<int> e[N];
void dfs(int u, int fa) {
  for (int i = (0); i < (((int)(e[u]).size())); ++i) {
    int v = e[u][i];
    if (v == fa) continue;
    dfs(v, u);
    ic[u] = max(ic[u], ic[v]);
    dc[u] = max(dc[u], dc[v]);
  }
  w[u] += ic[u] - dc[u];
  w[u] >= 0 ? dc[u] += w[u] : ic[u] -= w[u];
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i < (n); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v), e[v].push_back(u);
  }
  for (int i = (1); i < (n + 1); ++i) scanf("%lld", &w[i]);
  dfs(1, -1);
  printf("%lld", ic[1] + dc[1]);
  return 0;
}
