#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 9;
long long n, a, b, val[MAX], incc[MAX], decc[MAX];
vector<int> g[MAX];
void dfs(int v, int p) {
  for (auto u : g[v])
    if (u != p)
      dfs(u, v), decc[v] = max(decc[v], decc[u]),
                 incc[v] = max(incc[v], incc[u]);
  long long meg = val[v] + incc[v] - decc[v];
  if (meg < 0)
    incc[v] += -meg;
  else
    decc[v] += meg;
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> a >> b, a--, b--, g[a].push_back(b), g[b].push_back(a);
  for (int i = 0; i < n; i++) cin >> val[i];
  dfs(0, -1);
  cout << incc[0] + decc[0];
}
