#include <bits/stdc++.h>
using namespace std;
const long long is_query = -(1LL << 62);
struct Line {
  long long m, b;
  mutable function<const Line*()> succ;
  bool operator<(const Line& rhs) const {
    if (rhs.b != is_query) return m < rhs.m;
    const Line* s = succ();
    if (!s) return 0;
    long long x = rhs.m;
    return b - s->b < (s->m - m) * x;
  }
};
struct HullDynamic : public multiset<Line> {
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end()) return 0;
      return y->m == z->m && y->b <= z->b;
    }
    auto x = prev(y);
    if (z == end()) return y->m == x->m && y->b <= x->b;
    return 1.0 * (x->b - y->b) * (z->m - y->m) >=
           1.0 * (y->b - z->b) * (y->m - x->m);
  }
  void add(long long m, long long b) {
    auto y = insert({m, b});
    y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
    if (bad(y)) {
      erase(y);
      return;
    }
    while (next(y) != end() && bad(next(y))) erase(next(y));
    while (y != begin() && bad(prev(y))) erase(prev(y));
  }
  long long eval(long long x) {
    auto l = *lower_bound((Line){x, is_query});
    return l.m * x + l.b;
  }
};
const int N = 1.5e5 + 5;
vector<int> adj[N];
int n, in[N], out[N], vert[N], tym;
int d[N], a[N], vis[N], sub[N];
long long sum[N], pref[N], suff[N], ans;
void calc(int u, int p) {
  sub[u] = 1;
  for (int v : adj[u])
    if (v != p && !vis[v]) calc(v, u), sub[u] += sub[v];
}
int centroid(int u, int p, int r) {
  for (int v : adj[u])
    if (v != p && !vis[v])
      if (sub[v] > r) return centroid(v, u, r);
  return u;
}
void dfs(int u, int p) {
  in[u] = ++tym;
  vert[tym] = u;
  d[u] = d[p] + 1;
  sum[u] = sum[p] + a[u];
  pref[u] = pref[p] + (long long)d[u] * a[u];
  suff[u] = suff[p] + sum[p] + a[u];
  ans = max({ans, suff[u], pref[u]});
  for (int v : adj[u])
    if (v != p && !vis[v]) dfs(v, u);
  out[u] = tym;
}
void process(int u) {
  tym = 0;
  dfs(u, 0);
  vector<int> child;
  for (int v : adj[u])
    if (!vis[v]) child.push_back(v);
  HullDynamic cht;
  auto doshit = [&]() {
    cht.clear();
    for (int i = 0; i < child.size(); ++i) {
      vector<pair<long long, long long> > add;
      for (int j = in [child[i]]; j <= out[child[i]]; ++j) {
        int v = vert[j];
        if (!cht.empty())
          ans = max(ans, cht.eval(d[v] - 1) + suff[v] - (long long)d[v] * a[u]);
        add.emplace_back(sum[v], pref[v]);
      }
      for (auto it : add) cht.add(it.first, it.second);
    }
  };
  doshit();
  reverse(child.begin(), child.end());
  doshit();
}
void decomp(int u) {
  calc(u, 0);
  int c = centroid(u, 0, sub[u] / 2);
  process(c);
  vis[c] = 1;
  for (int v : adj[c])
    if (!vis[v]) decomp(v);
}
int main(int argc, char const* argv[]) {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  decomp(1);
  printf("%lld\n", ans);
}
