#include <bits/stdc++.h>
using namespace std;
const int maxn = 110000;
vector<int> vec[maxn];
long long val[maxn], up[maxn], down[maxn];
int n;
void init() {
  for (int i = 0; i < maxn; i++) vec[i].clear();
}
void dfs(int u, int pre) {
  for (int i = 0; i < vec[u].size(); i++) {
    int v = vec[u][i];
    if (v == pre) continue;
    dfs(v, u);
    long long a = 0, b = 0;
    if (val[v] > 0) {
      a = up[v];
      b = down[v] + val[v];
    } else {
      a = up[v] - val[v];
      b = down[v];
    }
    up[u] = max(up[u], a);
    down[u] = max(down[u], b);
  }
  val[u] += up[u] - down[u];
}
int main() {
  int a, b;
  while (cin >> n) {
    init();
    for (int i = 1; i < n; i++) {
      cin >> a >> b;
      vec[a].push_back(b);
      vec[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) {
      up[i] = down[i] = 0LL;
      cin >> val[i];
    }
    dfs(1, -1);
    cout << abs(val[1]) + up[1] + down[1] << endl;
  }
  return 0;
}
