#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int INF = 1e9 + 7;
vector<int> e[N];
int a[N];
long long f[N][2];
void Dfs(int x, int par) {
  int flag = 0;
  f[x][0] = 0, f[x][1] = 0;
  for (int i = 0; i < e[x].size(); i++) {
    int y = e[x][i];
    if (y == par) continue;
    flag = 1;
    Dfs(y, x);
    f[x][0] = max(f[y][0], f[x][0]);
    f[x][1] = max(f[y][1], f[x][1]);
  }
  if (a[x] + f[x][0] - f[x][1] > 0) {
    f[x][1] += a[x] + f[x][0] - f[x][1];
  } else
    f[x][0] -= a[x] + f[x][0] - f[x][1];
}
int main() {
  int n, x, y;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  Dfs(1, 0);
  cout << f[1][1] + f[1][0] << endl;
}
