#include <bits/stdc++.h>
using namespace std;
template <class T>
T inline Abs(const T& x) {
  return x < 0 ? -x : x;
}
template <class T>
T inline Sqr(const T& x) {
  return x * x;
}
const int maxn = 101000;
vector<int> e[maxn];
int b[maxn];
int ans = 0;
long long c[maxn];
long long st[maxn];
set<int, int> s;
void Dfs(int v, long long& dec, long long& inc) {
  b[v] = 1;
  long long myinc = 0, mydec = 0;
  for (int i = 0; i < e[v].size(); ++i)
    if (!b[e[v][i]]) {
      Dfs(e[v][i], mydec, myinc);
    }
  long long r = c[v] - (mydec - myinc);
  if (r > 0)
    mydec += r;
  else
    myinc -= r;
  if (myinc > inc) inc = myinc;
  if (mydec > dec) dec = mydec;
}
void Update(int v) {}
int main(int argc, char* argv[]) {
  int n;
  cin >> n;
  for (int i = 0; i + 1 < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    int u;
    scanf("%d", &u);
    c[i] = u;
  }
  long long dec = 0, inc = 0;
  Dfs(1, dec, inc);
  cout << dec + inc << endl;
  return 0;
}
