#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << '\n';
  err(++it, args...);
}
const long long inf = 1e18L + 5;
const long long mod = 1e9 + 7;
std::vector<long long> g[100001];
std::vector<long long> v;
pair<long long, long long> dfs(long long u, long long p) {
  long long increase = 0;
  long long decrease = 0;
  for (long long i = 0; i < g[u].size(); ++i) {
    if (g[u][i] != p) {
      pair<long long, long long> x = dfs(g[u][i], u);
      increase = max(increase, x.first);
      decrease = max(decrease, x.second);
    }
  }
  long long temp = v[u] + increase - decrease;
  pair<long long, long long> ans;
  if (temp > 0) {
    ans = make_pair(increase, decrease + temp);
  } else {
    ans = make_pair(increase + abs(temp), decrease);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long testcases = 1;
  while (testcases--) {
    long long n;
    cin >> n;
    for (long long i = 0; i < n - 1; ++i) {
      long long a, b;
      cin >> a >> b;
      g[a].push_back(b);
      g[b].push_back(a);
    }
    v.resize(n + 1);
    for (long long i = 1; i <= n; ++i) {
      cin >> v[i];
    }
    pair<long long, long long> p = dfs(1, -1);
    cout << p.first + p.second << '\n';
  }
  return 0;
}
