#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int N = 100010;
vector<int> g[N];
int c[N];
int n;
pair<long long, long long> dfs(int x, int ult) {
  long long pos, neg;
  pos = neg = 0;
  for (int i = 0; i < g[x].size(); ++i) {
    int y = g[x][i];
    if (y == ult) continue;
    pair<long long, long long> foo = dfs(y, x);
    pos = max(foo.first, pos);
    neg = max(foo.second, neg);
  }
  c[x] -= (pos - neg);
  if (c[x] < 0)
    neg -= c[x];
  else
    pos += c[x];
  return make_pair(pos, neg);
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) cin >> c[i];
  pair<long long, long long> foo = dfs(0, -1);
  cout << foo.first + foo.second << endl;
  return 0;
}
