#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; using ull = unsigned long long; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int q=1; while(q--){ int n; cin >> n; vector a(n); for(int i=0;i> a[i]; } if(a[n-1] res; vector v; v.push_back(a[n-1]); for(int i=n-2;i>=0;i--){ if(v.size()==1&&v[0]>a[i]){ res.push_back(a[i]); continue; } if(v.back()>a[i]){ res.push_back(v.back()); v.pop_back(); v.push_back(a[i]); while(v.size()>2){ int sz=v.size(); int t=v.back(); if(v[sz-2]>v[sz-1]){ v.pop_back(); res.push_back(v.back()); v.pop_back(); v.push_back(t); } else{ break; } } if(v.size()==2&&v[0]>v[1]){ res.push_back(v.back()); v.pop_back(); } } else{ v.push_back(a[i]); } } for(int i:res){ printf("%d ",i); } printf("\n"); } }