#include using namespace std; const int N = 1010; int a[N], res[N]; stack stk; int n; /** 输入数据: 7 50 30 10 5 3 1 20 输出数据: -1 50 30 10 5 3 30 */ int main() { cin >> n; for (int i = 1; i <= n; i++)cin >> a[i]; for (int i = n; i >= 1; i--) { //a[i]左侧元素,栈顶元素找到了左侧比自己大的最近元素 while (!stk.empty() && a[i] >= a[stk.top()]) { res[stk.top()] = i; stk.pop(); } stk.push(i); } for (int i = 1; i <= n; i++) if (res[i])cout << a[res[i]] << " "; else cout << "-1 "; return 0; }