#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1.5e5 + 10;
int n, sz[maxN];
vector<int> adj[maxN];
bool rem[maxN];
long long a[maxN], f[maxN], g[maxN], sum[maxN], ans = 0;
bool Q = 0;
struct Line {
  mutable long long a, b, p;
  bool operator<(const Line &l) const { return Q ? p < l.p : a < l.a; }
};
struct CHT : multiset<Line> {
  const long long inf = 1e18;
  long long div(long long a, long long b) { return floor((long double)a / b); }
  bool intersect(iterator x, iterator y) {
    if (y == end()) {
      x->p = inf;
      return 0;
    }
    if (x->a == y->a) {
      x->p = x->b > y->b ? inf : -inf;
    } else {
      x->p = div(y->b - x->b, x->a - y->a);
    }
    return x->p >= y->p;
  }
  void add(long long a, long long b) {
    auto nxt = insert({a, b, 0});
    auto prv = nxt++;
    while (intersect(prv, nxt)) {
      nxt = erase(nxt);
    }
    if ((nxt = prv) != begin() && intersect(--prv, nxt)) {
      intersect(prv, erase(nxt));
    }
    while ((nxt = prv) != begin() && (--prv)->p >= nxt->p) {
      intersect(prv, erase(nxt));
    }
  }
  long long query(long long x) {
    Q = 1;
    auto it = *lower_bound({0, 0, x});
    Q = 0;
    return it.a * x + it.b;
  }
};
void cal_size(int u, int p) {
  sz[u] = 1;
  for (auto &v : adj[u]) {
    if (v != p && !rem[v]) {
      cal_size(v, u);
      sz[u] += sz[v];
    }
  }
}
int centroid(int u, int p, const int &root) {
  for (auto &v : adj[u]) {
    if (v != p && !rem[v] && sz[v] > sz[root] / 2) {
      return centroid(v, u, root);
    }
  }
  return u;
}
void cal(int u, int p, int depth) {
  for (auto &v : adj[u]) {
    if (v != p && !rem[v]) {
      sum[v] = sum[u] + a[v];
      f[v] = f[u] + sum[v];
      g[v] = g[u] + a[v] * (depth + 1);
      cal(v, u, depth + 1);
    }
  }
}
CHT container;
void dfs(int u, int p, int depth, const int &c) {
  uax(ans, g[u] + container.query(sum[u] + a[c]));
  uax(ans, f[u] + a[c] * (depth + 1));
  for (auto &v : adj[u]) {
    if (v != p && !rem[v]) {
      dfs(v, u, depth + 1, c);
    }
  }
}
void update(int u, int p, int depth) {
  container.add(depth + 1, f[u]);
  for (auto &v : adj[u]) {
    if (v != p && !rem[v]) {
      update(v, u, depth + 1);
    }
  }
}
void proc(int u) {
  cal_size(u, 0);
  int c = centroid(u, 0, u);
  rem[c] = 1;
  for (auto &v : adj[c]) {
    if (!rem[v]) {
      proc(v);
    }
  }
  rem[c] = 0;
  for (auto &v : adj[c]) {
    if (!rem[v]) {
      sum[v] = f[v] = g[v] = a[v];
      cal(v, c, 1);
    }
  }
  container.clear();
  container.add(1, 0);
  for (int i = 0; i < (int)(adj[c].size()); ++i) {
    int v = adj[c][i];
    if (!rem[v]) {
      dfs(v, c, 1, c);
      update(v, c, 1);
    }
  }
  container.clear();
  container.add(1, 0);
  for (int i = (int)(adj[c].size()) - 1; i >= 0; --i) {
    int v = adj[c][i];
    if (!rem[v]) {
      dfs(v, c, 1, c);
      update(v, c, 1);
    }
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= (int)(n - 1); ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }
  for (int i = 1; i <= (int)(n); ++i) {
    cin >> a[i];
  }
  proc(1);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
