#

탑 - 프로그래머스

def solution(heights):
    answer = []
    for idx, ele in enumerate(heights):
        flag = False
        for i in range(idx,-1,-1):
            if heights[i] > ele:
                answer.append(i+1)
                flag = True
                break
        if flag == False:
            answer.append(0)
    return answer

탑 - BOJ

n = int(input())
heights = list(map(int, input().split()))
answer = []
stack = []

for idx in range(n):
    while stack:
        if stack[-1][1] > heights[idx]:
            answer.append(stack[-1][0] + 1)
            break
        stack.pop()
    if not stack:
        answer.append(0)
    stack.append([idx, heights[idx]])

[print(i, end=" ") for i in answer]