#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:128000000")
const int MAXN = 100005;
int n;
int V[MAXN];
vector<vector<int> > g;
long long tres;
inline void init() {
  int x, y;
  scanf("%d", &n);
  tres = 0;
  g.resize(n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    --x, --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 0; i < n; ++i) scanf("%d", &V[i]);
}
inline long long abs64(long long x) {
  if (x < 0) return -x;
  return x;
}
pair<long long, long long> dfs(int x, int p) {
  int K = (int)g[x].size();
  char was = 0;
  pair<long long, long long> res(0, 0);
  for (int i = 0; i < K; ++i) {
    int y = g[x][i];
    if (y == p) continue;
    pair<long long, long long> cur = dfs(y, x);
    if (!was)
      res = cur;
    else {
      res.first = min(res.first, cur.first);
      res.second = max(res.second, cur.second);
    }
    was = 1;
  }
  long long bal = (long long)V[x] + res.first + res.second;
  if (bal > 0)
    res.first -= bal;
  else
    res.second -= bal;
  return res;
}
int main() {
  init();
  pair<long long, long long> ans = dfs(0, 0);
  cout << ans.second - ans.first << endl;
  return 0;
}
