#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int inf = 10000000;
vector<int> g[100005];
long long p[100005], m[100005], c[100005];
bool vis[100005];
void dfs(int u) {
  vis[u] = 1;
  long long pp = 0, mm = 0;
  for (int i = 0; i < (((int)(g[u]).size())); i++) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
    pp = ((pp) > (p[v]) ? (pp) : (p[v]));
    mm = ((mm) > (m[v]) ? (mm) : (m[v]));
  }
  long long dif = c[u] - (pp - mm);
  if (dif > 0)
    pp += dif;
  else
    mm -= dif;
  p[u] = pp;
  m[u] = mm;
}
int main() {
  int n;
  int a, b;
  cin >> n;
  for (int i = 0; i < (n - 1); i++) {
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < (n); i++) cin >> c[i];
  dfs(0);
  cout << p[0] + m[0] << endl;
  return 0;
}
