import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] agrs) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Stack<Integer> stack = new Stack<>();
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());        
        String[] arr = br.readLine().split(" ");
        String[] ans = new String[n];
        
        stack.push(0);
        for(int i=1; i<n; i++) {
            while(!stack.isEmpty() &&
                Integer.parseInt(arr[stack.peek()])<Integer.parseInt(arr[i])) {
                    ans[stack.peek()] = arr[i];
                    stack.pop();
            }
            
            stack.push(i);
        }
        
        while(!stack.isEmpty()) { ans[stack.pop()] = "-1"; }
        
        for(String s : ans) { sb.append(s).append(" "); }
        
        System.out.println(sb);
    }
}

'Problem Solving > BOJ' 카테고리의 다른 글

[Silver 3] 1935번 후위 표기식2  (0) 2022.03.25
[Gold 3] 17299번 오등큰수  (0) 2022.03.22
[Silver 3] 10799번 쇠막대기  (0) 2022.03.22
[Silver 3] 17413번 단어 뒤집기 2  (0) 2022.03.22
[Silver 4] 1158번 요세푸스 문제  (0) 2022.03.22