#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int maxN = 100010;
vector<int> ady[maxN];
long long w[maxN];
long long add[maxN];
long long sub[maxN];
bool visited[maxN];
void go(int v) {
  visited[v] = true;
  long long aAdd = 0, aSub = 0;
  for (int i = 0; i < (int)ady[v].size(); i++) {
    int actualNeigh = ady[v][i];
    if (!visited[actualNeigh]) {
      go(actualNeigh);
      aAdd = max(aAdd, add[actualNeigh]);
      aSub = max(aSub, sub[actualNeigh]);
    }
  }
  add[v] = aAdd;
  sub[v] = aSub;
  w[v] += add[v];
  w[v] -= sub[v];
  if (w[v] > 0) {
    sub[v] += w[v];
  } else {
    add[v] += abs(w[v]);
  }
}
int main() {
  for (int i = 0; i < (int)maxN; i++) ady[i].clear();
  for (int i = 0; i < (int)maxN; i++) w[i] = 0;
  for (int i = 0; i < (int)maxN; i++) {
    add[i] = 0;
    sub[i] = 0;
  }
  for (int i = 0; i < (int)maxN; i++) visited[i] = false;
  int n;
  cin >> n;
  for (int i = 0; i < (int)n - 1; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    ady[ai].push_back(bi);
    ady[bi].push_back(ai);
  }
  for (int i = 0; i < (int)n; i++) cin >> w[i];
  go(0);
  cout << add[0] + sub[0] << endl;
  return 0;
}
