#include <bits/stdc++.h>
using namespace std;
long long int modm(long long int a, long long int b, long long int mod) {
  return ((((long long int)(a) % mod) * ((long long int)(b) % mod)) % mod);
}
long long int moda(long long int a, long long int b, long long int mod) {
  return ((((long long int)(a) % mod) + ((long long int)(b) % mod)) % mod);
}
long long int mods(long long int a, long long int b, long long int mod) {
  return (((long long int)(a) % mod + mod - ((long long int)(b) % mod)) % mod);
}
long long inv(long long a, long long b) {
  return 1 < a ? b - inv(b % a, a) * b / a : 1;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
const long long int N_ = 100023, mod = 1000000007;
bool cmp(pair<long long int, long long int> a,
         pair<long long int, long long int> b) {
  if (a.second != b.second)
    return a.second < b.second;
  else
    return a.first > b.first;
}
vector<long long int> g[N_];
long long int v[N_], cnt_add[N_], cnt_sub[N_];
void dfs(long long int u, long long int p) {
  long long int val = v[u], ma = 0, ms = 0;
  for (long long int v : g[u]) {
    if (v != p) {
      dfs(v, u);
      ma = max(ma, cnt_add[v]);
      ms = max(ms, cnt_sub[v]);
    }
  }
  val += ma - ms;
  if (val > 0)
    cnt_sub[u] += val;
  else
    cnt_add[u] -= val;
  cnt_add[u] += ma;
  cnt_sub[u] += ms;
}
void solve() {
  long long int n;
  cin >> n;
  for (long long int i = 0; i < (n - 1); ++i) {
    long long int u, v;
    cin >> u >> v;
    g[u].push_back(v), g[v].push_back(u);
  }
  for (long long int i = 0; i < (n); ++i) cin >> v[i + 1];
  dfs(1, 0);
  cout << cnt_add[1] + cnt_sub[1] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int T;
  T = 1;
  for (long long int t = 0; t < (T); ++t) {
    solve();
  }
  return 0;
}
