stl stack O(n)
#include <iostream>
#include <stack>
using namespace std;
int n,array[100010];
int main(){
cin >> n;
stack<int>st; st.push(-1); // -1 做指示器
for (int i = 0; i < n; i++) {
cin >> array[i];
while(array[i] <= st.top() && st.top() != -1) st.pop();
cout << st.top() << " ";
st.push(array[i]);
}
return 0;
}