#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") int N; vector A, B; vector> G; namespace lca { vector dep; // vector cs; vector poss, seq; vector mn_; vector mn; void dfs(int u, int pi) { poss[u] = seq.size(); seq.push_back(u); for (const int i : G[u]) if (pi != i) { const int v = A[i] ^ B[i] ^ u; dep[v] = dep[u] + 1; // cs[v] = cs[u] + C[i]; dfs(v, i); seq.push_back(u); } } inline int shallower(int u, int v) { return (dep[u] <= dep[v]) ? u : v; } void build() { poss.resize(N); dep.assign(N, 0); // cs.assign(N, 0); seq.clear(); dfs(0, -1); const int E = (31 - __builtin_clz(2 * N - 1)) + 1; mn_.resize(E * (2 * N - 1)); mn.resize(E); for (int e = 0; e < E; ++e) { mn[e] = mn_.data() + e * (2 * N - 1); } for (int j = 0; j < 2 * N - 1; ++j) { mn[0][j] = seq[j]; } for (int e = 0; e < E - 1; ++e) { for (int i = 0; i + (1 << (e + 1)) <= 2 * N - 1; ++i) { mn[e + 1][i] = shallower(mn[e][i], mn[e][i + (1 << e)]); } } } int lca(int u, int v) { int j0 = poss[u], j1 = poss[v]; if (j0 > j1) swap(j0, j1); ++j1; const int e = 31 - __builtin_clz(j1 - j0); return shallower(mn[e][j0], mn[e][j1 - (1 << e)]); } inline int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } // inline Int cost(int u, int v) { // return cs[u] + cs[v] - 2 * cs[lca(u, v)]; // } } // lca //////////////////////////////////////////////////////////////////////////////// vector W; int main() { for (; ~scanf("%d", &N); ) { W.resize(N); for (int u = 0; u < N; ++u) { scanf("%d", &W[u]); } A.resize(N - 1); B.resize(N - 1); for (int i = 0; i < N - 1; ++i) { scanf("%d%d", &A[i], &B[i]); --A[i]; --B[i]; } G.assign(N, {}); for (int i = 0; i < N - 1; ++i) { G[A[i]].push_back(i); G[B[i]].push_back(i); } lca::build(); map> uss; for (int u = 0; u < N; ++u) { uss[-W[u]].push_back(u); } vector ans(N, 0); { int d = 0; int a = -1, b = -1; for (const auto &kv : uss) { for (const int u : kv.second) { if (!~a) { a = b = u; } else { const int dau = lca::dist(a, u); const int dbu = lca::dist(b, u); if (dau >= dbu) { if (chmax(d, dau)) b = u; } else { if (chmax(d, dbu)) a = u; } } } for (const int u : kv.second) { ans[u] = max(lca::dist(a, u), lca::dist(b, u)); } } } for (int u = 0; u < N; ++u) { if (u) printf(" "); printf("%d", ans[u]); } puts(""); } return 0; }