#include // #include // #include #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define YES cout<< "Yes"<< endl #define NO cout<< "No"<< endl #define Rep(i,s,n) for(int i = (int)(s); i < (int)(n); i++) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() template bool chmax(T& x, const U& y){ if (x < y) { x = y; return true; } return false; } template bool chmin(T& x, const U& y){ if (y < x) { x = y; return true; } return false; } #define UNIQUE(a) a.erase(unique(a.begin(),a.end()),a.end()) using namespace std; // using mint = atcoder::modint998244353; // using mint2 = atcoder::modint1000000007; // using namespace atcoder; using ll = long long; using V = vector; using P = pair; using i128 = __int128; void solve(){ int n; cin >> n; vector s(n); rep(i,n) cin >> s[i]; vector> g(n); vector seen(n); rep(i,n-1){ int a,b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } ll ans = -1; auto f = [&](auto f,int v,int pv,ll sum) -> void{ //cout << v << " " << pv << " " << sum << endl; seen[v] = true; chmax(ans,sum); for(auto nv : g[v]){ if(nv == pv) continue; if(s[nv] > s[v]){ f(f,nv,v,sum+s[nv]); } } }; rep(i,n){ if(!seen[i]) f(f,i,-1,s[i]); } cout << ans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t; t = 1; //cin >> t; rep(i,t) solve(); }