import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
Stack<Integer> stack = new Stack<>();
for(int i=0;i<arr.length;i++){
while(!stack.isEmpty()){
if(stack.peek() > arr[i]){
System.out.print(arr[i]+" ");
stack.pop();
}
}
stack.push(arr[i]);
}
while(!stack.isEmpty()) {
int rv = stack.pop();
System.out.println(rv+" ");
}
}
}