// 存储状态转移就可以把方案记录下来,即动态规划求方案
import java.util.Scanner;
public class Main {
static int NN = 1010;
static int[] s = new int[NN]; // 存输入的序列数组
static int[] g = new int[NN];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
for (int i = 1; i < N + 1; i++) {
s[i] = scanner.nextInt();
}
int[] dp = new int[N + 1];
for (int i = 1; i < N + 1; i++) {
dp[i] = 1;
for (int j = 1; j < i; j++) {
if (s[j] < s[i]) {
dp[i] = Math.max(dp[i],dp[j]+1);
if (dp[i] == dp[j] + 1){
g[i] = j;
}
}
}
}
int res = 0;
int k = 0;
for (int i = 1; i < N + 1; i++) {
res = Math.max(dp[i],res);
if (res == dp[i]){
k = i;
}
}
for (int i = 0,len = res; i < len; i++) {
System.out.println(s[k]);
k = g[k];
}
}
}