#include <bits/stdc++.h>
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1, -2, 2, 0, 0},
    dy[] = {1, 0, 0, -1, -1, 1, 1, -1, 0, 0, -2, 2};
using namespace std;
vector<vector<long long> > g;
vector<long long> deg;
bool visited[123456];
long long ans = 0;
pair<long long, long long> dfs(int cur) {
  visited[cur] = 1;
  long long mx = 0, mi = 0, pr = 0;
  for (int i = 0; i < g[cur].size(); i++) {
    if (visited[g[cur][i]]) continue;
    pair<long long, long long> c = dfs(g[cur][i]);
    mx = max(mx, c.second);
    mi = min(mi, c.first);
  }
  if (cur == 0) {
    deg[cur] = deg[cur] + mx + mi;
    ans += abs(mi) + abs(mx);
    ans += abs(deg[cur]);
  }
  deg[cur] = deg[cur] + mx + mi;
  if (deg[cur] > 0) mi -= deg[cur];
  if (deg[cur] < 0) mx -= deg[cur];
  return make_pair(mi, mx);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    vector<long long> t;
    g.push_back(t);
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    deg.push_back(x);
  }
  dfs(0);
  cout << ans << endl;
}
