#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
long long vis[100010];
long long val[100010];
long long NEGACUM[100010];
long long POSACUM[100010];
long long DEG[100010];
void dfs(int u) {
  vis[u] = 1;
  DEG[u] = 0;
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    if (vis[v]) continue;
    DEG[u]++;
    dfs(v);
  }
}
void dfs2(int u) {
  vis[u] = 1;
  NEGACUM[u] = 0;
  POSACUM[u] = 0;
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    if (vis[v]) continue;
    dfs2(v);
    NEGACUM[u] = min(NEGACUM[u], NEGACUM[v]);
    POSACUM[u] = max(POSACUM[u], POSACUM[v]);
  }
  val[u] = val[u] - POSACUM[u] - NEGACUM[u];
  NEGACUM[u] += (val[u] <= 0 ? val[u] : 0);
  POSACUM[u] += (val[u] >= 0 ? val[u] : 0);
}
int main(int argc, char* argv[]) {
  int N;
  cin >> N;
  adj = vector<vector<int> >(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < N; ++i) {
    cin >> val[i];
  }
  dfs2(0);
  cout << POSACUM[0] - NEGACUM[0] << endl;
}
