#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> g[100010];
int vis[100010];
struct node {
  int num;
  long long inc;
  long long dec;
  int val;
} nodes[100010];
void dfs(node &u) {
  vis[u.num] = 1;
  for (int v : g[u.num]) {
    if (!vis[v]) {
      dfs(nodes[v]);
      u.inc = max(u.inc, nodes[v].inc);
      u.dec = max(u.dec, nodes[v].dec);
    }
  }
  if (u.val + u.inc - u.dec < 0) {
    u.inc += abs(u.val + u.inc - u.dec);
  } else {
    u.dec += abs(u.val + u.inc - u.dec);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    nodes[i].num = i;
    nodes[i].inc = nodes[i].dec = 0;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    int v;
    cin >> v;
    nodes[i].val = v;
  }
  dfs(nodes[1]);
  cout << nodes[1].inc + nodes[1].dec;
  return 0;
}
