#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize("Os")
#pragma GCC optimize("Og")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,sse,sse2,sse3,ssse3,sse4")
using namespace std;
template <int MAXSIZE>
struct Tree {
  typedef function<void(int, int)> EdgeCallback;
  typedef function<void(int)> NodeCallback;
  inline Tree() {
    cnt = 0;
    firstlink_id.fill(-1);
  }
  inline void add_edge(int u, int v) {
    add_edge_inner(u, v);
    add_edge_inner(v, u);
  }
  void dfs(int u, int parent, NodeCallback const& pre_callback,
           EdgeCallback const& callback, NodeCallback const& post_callback) {
    pre_callback(u);
    for (int i = firstlink_id[u]; i >= 0; i = nxtsibling_id[i]) {
      int v = dst[i];
      if (v == parent) continue;
      dfs(v, u, pre_callback, callback, post_callback);
      callback(u, v);
    }
    post_callback(u);
  }

 private:
  array<int, MAXSIZE> firstlink_id;
  array<int, 2 * MAXSIZE> nxtsibling_id;
  array<int, 2 * MAXSIZE> dst;
  int cnt;
  inline void add_edge_inner(int u, int v) {
    dst[cnt] = v;
    nxtsibling_id[cnt] = firstlink_id[u];
    firstlink_id[u] = cnt++;
  }
};
const int MAX = 100001;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int(n);
  cin >> n;
  using Tree = Tree<MAX>;
  Tree tree;
  for (int i = 0; i < n - 1; i++) {
    int(a);
    cin >> a;
    int(b);
    cin >> b;
    tree.add_edge(a, b);
  }
  array<long long, MAX + 1> v;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  struct IncDec {
    long long i = 0, d = 0;
  };
  array<IncDec, MAX + 1> r;
  tree.dfs(
      1, -1, [](int) {},
      [&r](int u, int v) {
        r[u].i = max(r[u].i, r[v].i);
        r[u].d = min(r[u].d, r[v].d);
      },
      [&r, &v](int u) {
        v[u] += r[u].i + r[u].d;
        if (v[u] < 0)
          r[u].i -= v[u];
        else
          r[u].d -= v[u];
      });
  cout << (r[1].i - r[1].d) << '\n';
  return 0;
}
