from collections import Counter
N = int(input())
arr = list(map(int, input().split()))
arr_counter = Counter(arr)
count_arr = [0] * len(arr)
for i in range(len(arr)):
count_arr[i] = arr_counter[arr[i]]
ans = [-1] * len(arr)
stack = []
for i in range(len(arr)):
while stack and count_arr[stack[-1]] < count_arr[i]:
ans[stack.pop()] = arr[i]
stack.append(i)
print(' '.join(list(map(str, ans))))
백준 오큰수 문제와 큰 차이가 없다. 다만 Counter를 써서 값을 한번 꼬아준 것 뿐이다.