https://www.acmicpc.net/problem/17298
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static int N;
static int[] series;
static void input() {
Reader scanner = new Reader();
N = scanner.nextInt();
series = new int[N];
for(int i = 0; i < N; i++) {
series[i] = scanner.nextInt();
}
}
static void solution() {
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < N; i++) {
while(!stack.isEmpty() && (series[stack.peek()] < series[i])) series[stack.pop()] = series[i];
stack.push(i);
}
while(!stack.isEmpty()) series[stack.pop()] = -1;
for(int i = 0; i < N; i++) sb.append(series[i]).append(' ');
System.out.println(sb.toString());
}
public static void main(String[] args) {
input();
solution();
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}