https://www.acmicpc.net/problem/17298

A[i]의 오큰수는 오른쪽에 있으면서 A[i]보다 큰 수 중에서 가장 왼쪽에 있는 수 없다면 -1을 내뱉는다
import sys
N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().split()))
answer = [-1] * N
stk = []
for item in enumerate(A):
index, height = item
if stk:
while stk and height > stk[-1][1]:
current_index, _ = stk.pop()
answer[current_index] = height
else:
stk.append((index, height))
else:
stk.append((index, height))
print(*answer)