#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> a, pair<int, int> b) {
  return (a.first == b.first) ? a.second < b.second : a.first < b.first;
}
long long int modInverse(long long int a) {
  long long int m = 1000000007;
  long long int y = 0, x = 1;
  while (a > 1) {
    long long int q = a / m;
    long long int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += 1000000007;
  return x;
}
long long int binpow(long long int a, long long int p) {
  if (p == 0) return 1;
  long long int ret = binpow(a, p / 2);
  ret *= ret;
  ret %= 1000000007;
  if (p % 2) {
    ret *= a;
    ret %= 1000000007;
  }
  return ret;
}
vector<vector<int>> graph(100005);
vector<long long int> v(100005);
vector<bool> vis(100005, 0);
long long int ans = 0;
pair<long long int, long long int> dfs(int i) {
  vis[i] = 1;
  long long int neg = 0, pos = 0;
  for (int j = 0; j < graph[i].size(); j++) {
    if (!vis[graph[i][j]]) {
      pair<long long int, long long int> p1 = dfs(graph[i][j]);
      neg = max(neg, p1.first);
      pos = max(pos, p1.second);
    }
  }
  long long int tmp = v[i] + neg - pos;
  if (tmp < 0)
    neg += abs(tmp);
  else
    pos += tmp;
  return {neg, pos};
}
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    graph[x - 1].push_back(y - 1);
    graph[y - 1].push_back(x - 1);
  }
  for (int i = 0; i < n; i++) cin >> v[i];
  pair<long long int, long long int> res = dfs(0);
  cout << res.first + res.second;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int t = 1;
  while (t--) {
    solve();
  }
}
