#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double pi = acos(-1.0);
vector<int> G[100005];
long long int v[100005];
bool vis[100005];
pair<long long int, long long int> ind[100005];
void DFS(int u, int dad) {
  vis[u] = true;
  for (int i = 0; i < (int)G[u].size(); i++) {
    if (!vis[G[u][i]]) {
      DFS(G[u][i], u);
      ind[u].first =
          ((ind[G[u][i]].first) > (ind[u].first) ? (ind[G[u][i]].first)
                                                 : (ind[u].first));
      ind[u].second =
          ((ind[G[u][i]].second) > (ind[u].second) ? (ind[G[u][i]].second)
                                                   : (ind[u].second));
    }
  }
  long long int x = (((v[u] + ind[u].first - ind[u].second) *
                      ((v[u] + ind[u].first - ind[u].second) < 0)) > 0
                         ? ((v[u] + ind[u].first - ind[u].second) *
                            ((v[u] + ind[u].first - ind[u].second) < 0))
                         : (-((v[u] + ind[u].first - ind[u].second) *
                              ((v[u] + ind[u].first - ind[u].second) < 0))));
  long long int y = (((v[u] + ind[u].first - ind[u].second) *
                      ((v[u] + ind[u].first - ind[u].second) > 0)) > 0
                         ? ((v[u] + ind[u].first - ind[u].second) *
                            ((v[u] + ind[u].first - ind[u].second) > 0))
                         : (-((v[u] + ind[u].first - ind[u].second) *
                              ((v[u] + ind[u].first - ind[u].second) > 0))));
  ind[u].first = x + ind[u].first;
  ind[u].second = y + ind[u].second;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, x, y;
  while (cin >> n) {
    for (int i = 0; i < 100005; i++) G[i].clear();
    memset(ind, 0, sizeof(ind));
    memset(vis, (false), sizeof(vis));
    for (int i = 0; i < n - 1; i++) {
      cin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    for (int i = 0; i < n; i++) cin >> v[i + 1];
    DFS(1, 0);
    cout << (long long int)ind[1].first + ind[1].second << endl;
  }
  return 0;
}
