#include <bits/stdc++.h>
using namespace std;
const char *fi = "inp.txt";
const char *fo = "out.txt";
const int MAXN = 1e5;
pair<long long, long long> dfs(int, int);
vector<vector<int> > g;
vector<long long> v;
int main() {
  int n;
  scanf("%d", &n);
  g.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    v.push_back(x);
  }
  pair<long long, long long> res = dfs(0, -1);
  printf("%I64d", res.first + res.second);
  return 0;
}
pair<long long, long long> dfs(int u, int par) {
  pair<long long, long long> ret(0, 0);
  for (int i = 0; i < g[u].size(); i++) {
    if (g[u][i] != par) {
      pair<long long, long long> tmp = dfs(g[u][i], u);
      ret.first = max(ret.first, tmp.first);
      ret.second = max(ret.second, tmp.second);
    }
  }
  long long tmp = v[u] + ret.first - ret.second;
  if (tmp < 0)
    ret.first -= tmp;
  else
    ret.second += tmp;
  return ret;
}
