import java.util.Scanner;
import java.util.Stack;
/**
* @author supermejane
* @date 2025/10/1
* @description
*/
public class Main {
private static Stack<Integer> stack = new Stack<>();
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] a = new int[1000000];
// 注意 hasNext 和 hasNextLine 的区别
int cnt = in.nextInt();
for (int i = 0; i < cnt; i++) {
a[i] = in.nextInt();
}
int max = Integer.MIN_VALUE;
int[] maxArr = new int[cnt];
for (int i = cnt - 1; i >= 0; i--) {
if (a[i] > max) max = a[i];
maxArr[i] = max;
}
boolean flag = false;
for (int i = 0; i < cnt; i++) {
if (a[i] < maxArr[i]) stack.push(a[i]);
else {
if (!flag) {
System.out.print(a[i]);
flag = true;
} else {
System.out.print(" " + a[i]);
}
}
}
while (!stack.isEmpty()) {
if (!flag) {
System.out.print(stack.pop());
flag = true;
} else {
System.out.print(" " + stack.pop());
}
}
}
}