#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const int N = 2e5 + 15;
int n, m, k, q, a[N];
vector<int> g[N];
int sz[N];
bool used[N];
int d[N];
long long ans;
template <typename T>
void Min(T &a, T b) {
  a = min(a, b);
}
template <typename T>
void Max(T &a, T b) {
  a = max(a, b);
}
struct node {
  long long k, b;
  node() { k = b = -1; }
  inline long long val(long long x) { return k * x + b; }
  node(long long k, long long b) : k(k), b(b) {}
};
node t[N << 2];
inline void update(int v, int tl, int tr, node nw) {
  while (t[v].k != -1 && tl <= tr) {
    int mid = tl + tr >> 1;
    if (t[v].val(mid) < nw.val(mid)) swap(t[v], nw);
    if (nw.val(tl) > t[v].val(tl)) {
      v = v << 1;
      tr = mid - 1;
    } else {
      v = v << 1 | 1;
      tl = mid + 1;
    }
  }
  if (t[v].k == -1) t[v] = nw;
}
inline long long get(int v, int tl, int tr, int x) {
  long long res = 0;
  while (t[v].k != -1 && tl <= tr) {
    int mid = tl + tr >> 1;
    Max(res, t[v].val(x));
    if (x <= mid) {
      v = v << 1;
      tr = mid - 1;
    } else {
      v = v << 1 | 1;
      tl = mid + 1;
    }
  }
  return res;
}
inline int centroid(int v, int p, int n) {
  for (int to : g[v])
    if (!used[to] && to != p && 2 * sz[to] > n) return centroid(to, v, n);
  return v;
}
int dfs_sz(int v, int p = -1) {
  sz[v] = 1;
  for (int to : g[v])
    if (to != p && !used[to]) {
      d[to] = d[v] + 1;
      sz[v] += dfs_sz(to, v);
    }
  return sz[v];
}
void calc(int v, int p = -1, long long sum = 0, long long val = 0) {
  sum += a[v];
  val += sum;
  Max(ans, get(1, 0, n, d[v]) + val);
  for (int to : g[v])
    if (to != p && !used[to]) calc(to, v, sum, val);
}
void mark(int v, int p = -1, long long sum = 0, long long val = 0) {
  val += (long long)a[v] * (d[v] + 1);
  sum += a[v];
  update(1, 0, n, *new node(sum, val));
  for (int to : g[v])
    if (to != p && !used[to]) mark(to, v, sum, val);
}
void clr(int v, int tl, int tr) {
  if (tl > tr || t[v].k == -1) return;
  t[v].k = -1, t[v].b = -1;
  int mid = tl + tr >> 1;
  clr(v << 1, tl, mid - 1);
  clr(v << 1 | 1, mid + 1, tr);
}
void solve(int c) {
  used[c] = true;
  d[c] = 0;
  dfs_sz(c);
  clr(1, 0, n);
  Max(ans, (long long)a[c]);
  for (int to : g[c])
    if (!used[to]) {
      calc(to, c, a[c], a[c]);
      mark(to, c, 0, 0);
    }
  clr(1, 0, n);
  for (int i = 1; i <= g[c].size(); ++i) {
    int to = g[c][g[c].size() - i];
    if (!used[to]) {
      calc(to, c, a[c], a[c]);
      mark(to, c, 0, 0);
    }
  }
  for (int to : g[c])
    if (!used[to]) solve(centroid(to, c, sz[c]));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  dfs_sz(1);
  solve(centroid(1, -1, n));
  cout << ans << '\n';
  return 0;
}
