#include <bits/stdc++.h>
using namespace std;
int n;
long long val[100000 + 10], in[100000 + 10], de[100000 + 10], ans;
vector<int> adj[100000 + 10];
bool mark[100000 + 10];
void dfs(long long v);
void input();
int main() {
  input();
  dfs(0);
  cout << in[0] + de[0] << '\n';
  return 0;
}
void input() {
  cin >> 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];
}
void dfs(long long v) {
  mark[v] = true;
  for (int i = 0; i < adj[v].size(); i++)
    if (!mark[adj[v][i]]) {
      dfs(adj[v][i]);
      in[v] = max(in[v], in[adj[v][i]]);
      de[v] = max(de[v], de[adj[v][i]]);
    }
  if (in[v] - de[v] + val[v] > 0)
    de[v] = val[v] + in[v];
  else
    in[v] = de[v] - val[v];
}
