#include <bits/stdc++.h>
using namespace std;
int n;
long long val[100000], dp[100000][2];
vector<int> g[100000];
void dfs(int v, int p) {
  long long c1 = 0, c2 = 0;
  for (int i = 0; i < (g[v].size()); i++) {
    int next = g[v][i];
    if (next != p) {
      dfs(next, v);
      c1 = max(c1, dp[next][0]);
      c2 = max(c2, dp[next][1]);
    }
  }
  long long d = c1 - c2;
  if (val[v] + d > 0)
    c2 += val[v] + d;
  else
    c1 += -(val[v] + d);
  dp[v][0] = c1, dp[v][1] = c2;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < n; i++) cin >> val[i];
  dfs(0, -1);
  cout << dp[0][0] + dp[0][1];
}
