#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
vector<int> adj[MAXN];
long long c[MAXN];
long long d[MAXN][2];
bool mark[MAXN];
void dfs(int v) {
  long long a = 0;
  long long b = 0;
  mark[v] = true;
  for (int i = 0; i < adj[v].size(); i++) {
    if (mark[adj[v][i]] == false) {
      dfs(adj[v][i]);
      a = max(a, d[adj[v][i]][0]);
      b = max(b, d[adj[v][i]][1]);
    }
  }
  d[v][0] = a;
  d[v][1] = b;
  if (c[v] - a + b < 0) {
    d[v][1] -= c[v] - a + b;
  } else {
    d[v][0] += c[v] - a + b;
  }
}
int main() {
  int n;
  cin >> n;
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < n; i++) cin >> c[i];
  dfs(0);
  cout << d[0][0] + d[0][1] << endl;
  return 0;
}
