#include <bits/stdc++.h>
using namespace std;
struct AnsType {
  long long P, M;
};
int Get() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && (c != '-'))
    ;
  bool Flag = (c == '-');
  if (Flag) c = getchar();
  int X = 0;
  while (c >= '0' && c <= '9') {
    X = X * 10 + c - 48;
    c = getchar();
  }
  return Flag ? -X : X;
}
vector<int> G[100000];
int Value[100000];
AnsType DP(int X, int Last) {
  long long P0 = 0, M0 = 0;
  for (int i = 0; i < G[X].size(); i++) {
    int Y = G[X][i];
    if (Y == Last) continue;
    AnsType Temp = DP(Y, X);
    P0 = max(P0, Temp.P);
    M0 = max(M0, Temp.M);
  }
  long long V = Value[X] + P0 - M0;
  if (V >= 0)
    return (AnsType){P0, M0 + V};
  else
    return (AnsType){P0 - V, M0};
}
int main() {
  int N = Get();
  for (int i = 0; i < N - 1; i++) {
    int X = Get() - 1, Y = Get() - 1;
    G[X].push_back(Y);
    G[Y].push_back(X);
  }
  for (int i = 0; i < N; i++) Value[i] = Get();
  AnsType Ans = DP(0, -1);
  cout << Ans.P + Ans.M << endl;
  return 0;
}
