#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int n;
vector<int> g[100001];
long long a[100001];
pair<long long, long long> dfs(int v, int par) {
  pair<long long, long long> ret(0, 0);
  for (int i = 0, _n = ((int)((g[v]).size())); i < _n; ++i) {
    int v2 = g[v][i];
    if (v2 == par) continue;
    pair<long long, long long> nxt = dfs(v2, v);
    ret.first = max(ret.first, nxt.first);
    ret.second = max(ret.second, nxt.second);
  }
  a[v] += ret.first;
  a[v] -= ret.second;
  if (a[v] >= 0)
    ret.second += a[v];
  else
    ret.first += -a[v];
  return ret;
}
void solution() {
  scanf("%d", &n);
  for (int i = 0, _n = (n - 1); i < _n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  for (int i = 0, _n = (n); i < _n; ++i) {
    int val;
    scanf("%d", &val);
    a[i] = val;
  }
  pair<long long, long long> first = dfs(0, -1);
  cout << first.first + first.second << endl;
}
int main() {
  solution();
  return 0;
}
