#include <bits/stdc++.h>
using namespace std;
int lsone(int n) { return (n & -n); }
void mult(long long int a[25][25], long long int b[25][25],
          long long int c[25][25], int m, int n, int p) {
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= p; j++) {
      c[i][j] = 0;
      for (int k = 1; k <= n; k++) {
        c[i][j] += (a[i][k] * b[k][j]) % 1000000007;
        c[i][j] %= 1000000007;
      }
    }
  }
}
void mat_pow(long long int a[25][25], long long int c[25][25], int n,
             long long int p) {
  if (p == 0) {
    for (int i = 1; i <= n; i++) c[i][i] = 1;
  } else if (p == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) c[i][j] = a[i][j];
    }
  } else {
    long long int d[25][25];
    mat_pow(a, d, n, p / 2);
    if (p % 2) {
      long long int e[25][25];
      mult(d, d, e, n, n, n);
      mult(e, a, c, n, n, n);
    } else {
      mult(d, d, c, n, n, n);
    }
  }
}
long long int pow1(long long int a, long long int b) {
  if (b == 0)
    return 1ll;
  else if (b == 1)
    return a;
  else {
    long long int x = pow1(a, b / 2);
    x *= x;
    x %= 1000000007;
    if (b % 2) {
      x *= a;
      x %= 1000000007;
    }
    return x;
  }
}
long long int n, a, b, v[110000];
vector<vector<int> > graph(110000);
long long int negans = 0, posans = 0;
pair<long long int, long long int> solve(int index, int par) {
  long long int neg = 0, pos = 0;
  for (int i = 0; i < graph[index].size(); i++) {
    int node = graph[index][i];
    if (node == par) continue;
    pair<long long int, long long int> temp = solve(node, index);
    if (v[node] < 0) {
      pos = max(pos, -v[node] + temp.first);
      neg = max(neg, temp.second);
    } else {
      neg = max(neg, v[node] + temp.second);
      pos = max(pos, temp.first);
    }
  }
  v[index] += (pos - neg);
  negans = max(negans, neg);
  posans = max(posans, pos);
  return make_pair(pos, neg);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) cin >> v[i];
  solve(1, -1);
  long long int ans = negans + posans;
  if (v[1] > 0)
    ans += v[1];
  else
    ans += (-v[1]);
  cout << ans << "\n";
  return 0;
}
