#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int INF = 1e9 + 7;
vector<int> adjList[MAXN];
long long a[MAXN], dp[MAXN][2];
void dfs(int start, int par) {
  long long maxdp = 0, maxrdp = 0;
  for (auto u : adjList[start]) {
    if (u != par) {
      dfs(u, start);
      maxdp = max(maxdp, dp[u][0]);
      maxrdp = max(maxrdp, dp[u][1]);
    }
  }
  long long val = a[start] + maxdp - maxrdp;
  dp[start][0] = maxdp, dp[start][1] = maxrdp;
  if (val < 0)
    dp[start][0] += abs(val);
  else
    dp[start][1] += val;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int c, d;
    cin >> c >> d;
    adjList[c].push_back(d);
    adjList[d].push_back(c);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dfs(1, -1);
  cout << dp[1][0] + dp[1][1];
}
