#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> g[maxn];
long long mark[maxn], dph[maxn], dpp[maxn];
int n;
void input() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    mark[i] = x;
  }
}
void dfs(int v, int p = 0) {
  for (int i = 0; i < g[v].size(); i++)
    if (g[v][i] != p) {
      dfs(g[v][i], v);
      dpp[v] = max(dpp[g[v][i]], dpp[v]);
      dph[v] = max(dph[g[v][i]], dph[v]);
    }
  long long d = dpp[v] - dph[v] + mark[v];
  if (d < 0)
    dpp[v] += -d;
  else
    dph[v] += d;
}
int main() {
  input();
  dfs(1);
  cout << dpp[1] + dph[1] << endl;
}
