#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
template <typename T, typename second>
ostream& operator<<(ostream& s, pair<T, second>& p) {
  s << '{' << p.first << ", " << p.second << '}';
  return s;
}
template <typename T>
ostream& operator<<(ostream& s, vector<T>& v) {
  if (v.empty()) {
    s << "[]";
    return s;
  }
  s << '[';
  for (int i = 0; i < v.size() - 1; i++) s << v[i] << ", ";
  s << v.back() << ']';
  return s;
}
int64_t inp[100001];
vector<int> adj[100001];
int n, a, b;
inline void merge(pair<int64_t, int64_t>& p1, pair<int64_t, int64_t> p2) {
  p1.first = max(p2.first, p1.first);
  p1.second = max(p2.second, p1.second);
}
pair<int64_t, int64_t> dfs(int st, int par) {
  pair<int64_t, int64_t> ans = {0, 0};
  for (auto x : adj[st]) {
    if (x != par) {
      merge(ans, dfs(x, st));
    }
  }
  int64_t delta = ans.first - ans.second;
  if (delta < inp[st])
    ans.first += inp[st] - delta;
  else
    ans.second += delta - inp[st];
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &inp[i]);
  }
  pair<int64_t, int64_t> ans = dfs(1, 0);
  printf("%lld\n", ans.first + ans.second);
  return 0;
}
