#include #include using namespace std; using namespace atcoder; using mint = modint998244353; //using mint = modint1000000007; using ll = long long; using P = pair; using T = tuple; templatebool chmax(T_& a, const T_& b) { if (a < b) { a = b;return true; } else { return false; } } templatebool chmin(T_& a, const T_& b) { if (a > b) { a = b;return true; } else { return false; } } #ifdef LOCAL template ostream& operator<<(ostream& o, const pair& p) { return o << "(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& o, const tuple& t) { o << "("; apply([&o](auto&&... a) { int c = 0; (((o << (c++ ? ", " : "") << a)), ...); }, t); return o << ")"; } template auto operator<<(ostream& o, const V& v) -> std::enable_if_t && !std::is_same_v, decltype(v.begin(), o)> { o << "{"; int c = 0; for (auto& x : v) o << (c++ ? ", " : "") << x; return o << "}"; } #define dbg(...) cerr<<"["<<#__VA_ARGS__<<"]: ",([](auto&&... a){((cerr<> n; vector s(n, 0); priority_queue, greater

> pq; vector d(n, 0); for (int i = 0; i < n; i++) { cin >> s[i]; pq.emplace(s[i], i); d[i] = s[i]; } vector> g(n); for (int i = 0; i < n - 1; i++) { int u, v;cin >> u >> v; u--;v--; g[u].emplace_back(v); g[v].emplace_back(u); } while (pq.size()) { auto [u, v] = pq.top();pq.pop(); if (d[v] != u)continue; for (auto nx : g[v]) { if (s[v] < s[nx]) { if (chmax(d[nx], s[nx] + d[v])) { pq.emplace(d[nx], nx); } } } } ll ans = 0; for (int i = 0; i < n; i++) { chmax(ans, d[i]); } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } }