#include <bits/stdc++.h>
using namespace std;
void _print(long long t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(double t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.first);
  cerr << ",";
  _print(p.second);
  cerr << "}";
}
template <class T>
void _print(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
const long long N = 1e5 + 4;
const long long mod = 1e9 + 7;
vector<long long> adj[N];
long long moves = 0;
long long arr[N];
long long add[N], sub[N];
void dfs(long long i, long long par) {
  for (auto a : adj[i]) {
    if (a == par) continue;
    dfs(a, i);
    add[i] = max(add[a], add[i]);
    sub[i] = max(sub[i], sub[a]);
  }
  arr[i] -= add[i];
  arr[i] += sub[i];
  add[i] += max(0ll, arr[i]);
  sub[i] += max(0ll, -arr[i]);
}
void Main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n - 1; i++) {
    long long a;
    cin >> a;
    long long b;
    cin >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  for (long long i = 0; i < n; i++) cin >> arr[i];
  dfs(0, -1);
  cout << add[0] + sub[0] << "\n";
}
int32_t main() {
  srand(time(0));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  for (long long i = 1; i <= t; i++) {
    Main();
  }
}
