#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void maximize(T& a, T b) {
  if (b > a) a = b;
}
struct node {
  int y;
  node* next;
};
struct graph {
  node *map[110000], D[2 * 110000], *top;
  void insert(int x, int y) {
    top->y = y;
    top->next = map[x];
    map[x] = top++;
  }
  graph() : top(D) {}
} G;
int scan() {
  char ch;
  int seg = 1;
  while (!isdigit(ch = getchar()))
    if (ch == '-') seg = -1;
  int x = ch - '0';
  while (isdigit(ch = getchar())) x = x * 10 + ch - '0';
  return x * seg;
}
long long num[110000], dp[110000][2];
int n;
bool vis[110000];
void init() {
  n = scan();
  for (int i = 1; i < n; ++i) {
    int x = scan(), y = scan();
    G.insert(x, y);
    G.insert(y, x);
  }
  for (int i = 1; i <= n; ++i) num[i] = scan();
}
void DP(int x) {
  vis[x] = 1;
  for (node* i = G.map[x]; i; i = i->next) {
    int y = i->y;
    if (vis[y]) continue;
    DP(y);
    maximize(dp[x][0], dp[y][0]);
    maximize(dp[x][1], dp[y][1]);
  }
  long long t = num[x] + dp[x][0] - dp[x][1];
  ;
  if (t > 0)
    dp[x][1] += t;
  else
    dp[x][0] -= t;
}
long long work() {
  DP(1);
  return dp[1][0] + dp[1][1];
}
int main() {
  init();
  cout << work() << endl;
}
