// #pragma GCC target "prefer-vector-width=512" // #pragma GCC optimize "Ofast" #include using namespace std; #define ll long long #define rep(i,N) for(int i=0;i<(int)N;i++) #define rrep(i,s,e) for(int i=s;i<(int)e;i++) #define all(x) x.begin(),x.end() #define vecin(N,A) rep(i,N) cin>>A[i] #define vvecin(H,W,A) rep(i,H) rep(j,W) cin>>A[i][j] int main(){ int N;cin>>N; vectorP(N); vecin(N,P); vectorq; priority_queue,vector>,greater>>Q; vector>>M(N); vectorseen(N,false); rep(i,N){ M[i]={P[i],{i-1,i+1}}; } rep(i,N-1){ Q.push({P[i],i}); } while(!Q.empty()){ int val = Q.top().first; int ite = Q.top().second; Q.pop(); if(seen[ite])continue; seen[ite]=true; int nit = M[ite].second.second; if(nit>=N)continue; q.push_back(val); q.push_back(M[nit].first); seen[nit]=true; if(M[ite].second.first>=0)M[M[ite].second.first].second.second=M[nit].second.second; if(M[nit].second.second