#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> g[111111];
long long val[111111];
long long add[111111];
long long mns[111111];
int f[111111];
void mtree(int x, int p = 0) {
  f[x] = p;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to != p) {
      mtree(to, x);
    }
  }
}
void dfs(int x) {
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == f[x]) continue;
    dfs(to);
    add[x] = max(add[x], add[to]);
    mns[x] = max(mns[x], mns[to]);
  }
  val[x] += add[x];
  val[x] -= mns[x];
  if (val[x] > 0)
    mns[x] += abs(val[x]);
  else
    add[x] += abs(val[x]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(f, -1, sizeof(f));
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> val[i];
  }
  f[0] = 0;
  mtree(0);
  dfs(0);
  cout << add[0] + mns[0] << endl;
  return 0;
}
