#include <bits/stdc++.h>
using namespace std;
struct Dbg {
  template <typename T>
  Dbg& operator,(const T& v) {
    cerr << v << ", ";
    return *this;
  }
} Dbg;
template <typename T>
ostream& operator<<(ostream& _o_, const vector<T>& _v_) {
  if (!_v_.empty()) {
    _o_ << '[';
    copy(_v_.begin(), _v_.end(), ostream_iterator<T>(_o_, ", "));
    _o_ << "\b\b]";
  }
  return _o_;
}
template <class C>
void mini(C& _a, C _b) {
  _a = min(_a, _b);
}
template <class C>
void maxi(C& _a, C _b) {
  _a = max(_a, _b);
}
vector<int> g[100005];
int v[100005];
long long dp[100005][2];
void dfs(int u, int p) {
  if (g[u].size() == 1 && u != 1) {
    if (v[u] > 0)
      dp[u][0] = v[u];
    else
      dp[u][1] = v[u];
  }
  for (auto v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    maxi(dp[u][0], dp[v][0]);
    maxi(dp[u][1], dp[v][1]);
  }
  long long cur = v[u] - dp[u][0] + dp[u][1];
  if (cur > 0)
    dp[u][0] += cur;
  else
    dp[u][1] -= cur;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  dfs(1, 0);
  cout << dp[1][0] + dp[1][1];
  return 0;
}
