![](https://velog.velcdn.com/images/super-hwang/post/29bb3dd5-a94b-4848-9b94-82b27a198be1/image.jpg)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
Stack<int[]> stack = new Stack<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
int top = Integer.parseInt(st.nextToken());
while (!stack.isEmpty()) {
if (stack.peek()[1] >= top) {
System.out.print(stack.peek()[0] + " ");
break;
}
stack.pop();
}
if (stack.isEmpty()) {
System.out.print(0 + " ");
}
stack.push(new int[] {i + 1, top});
}
}
}
![](https://velog.velcdn.com/images/super-hwang/post/e8236127-d946-4320-a997-90f6cb22cc3e/image.png)