#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector<int> vec[MAXN];
long long a[MAXN], add[MAXN], sub[MAXN];
int vis[MAXN];
void dfs(int x) {
  long long tmpadd = 0, tmpsub = 0;
  for (int i = 0; i < vec[x].size(); i++) {
    int nxt = vec[x][i];
    if (vis[nxt] == 0) {
      vis[nxt] = 1;
      dfs(nxt);
      tmpadd = max(tmpadd, add[nxt]);
      tmpsub = max(tmpsub, sub[nxt]);
    }
  }
  a[x] += tmpadd;
  a[x] -= tmpsub;
  add[x] = tmpadd;
  sub[x] = tmpsub;
  if (a[x] > 0) {
    sub[x] += a[x];
  } else if (a[x] < 0) {
    add[x] -= a[x];
  }
}
void init() {
  memset(add, 0, sizeof(add));
  memset(sub, 0, sizeof(sub));
  for (int i = 0; i < MAXN; i++) {
    vec[i].clear();
  }
  memset(vis, 0, sizeof(vis));
}
int main() {
  int n;
  while (cin >> n) {
    init();
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      vec[u].push_back(v);
      vec[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    vis[1] = 1;
    dfs(1);
    cout << add[1] + sub[1] << endl;
  }
  return 0;
}
