#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
bool vis[100005];
long long int add[100005];
long long int sub[100005];
int val[100005];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (!vis[v]) {
      dfs(v);
      add[u] = max(add[u], add[v]);
      sub[u] = max(sub[u], sub[v]);
    }
  }
  long long int now = val[u] + add[u] - sub[u];
  if (now > 0)
    sub[u] += abs(now);
  else
    add[u] += abs(now);
}
int main() {
  int n, a, b;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  dfs(1);
  cout << add[1] + sub[1];
}
