#include <stdio.h>
#include <stdlib.h>
// determinare cel mai lung subsir crescator
void readArray(FILE *in, int *v, int N);
void printArray(int *v, int N);
void solveFirst(int *v, int *best, int *pos, int N);
void solveSecond(FILE *out, int *v, int *best, int *pos, int N);
void readArray(FILE *in, int *v, int N){
for(int i = 0; i < N; i++){
fscanf(in, "%d", v + i);
}
}
void printArray(int *v, int N){
for(int i = 0; i < N; i++){
printf("%d ", v[i]);
}
printf("\n");
}
void initializeArray(int *pre, int N){
// initializam vectorul pentru pozitii nevalide
for(int i = 0; i < N; i++){
pre[i] = -1;
}
}
void solveFirst(int *v, int *best, int *pos, int N){
// construirea vectorilor
best[0] = 1;
for(int i = 1; i < N; i++){
int max = 0, poss = -1;
for(int j = 0; j < i; j++){
if(best[j] > max && v[j] < v[i]){
max = best[j];
poss = j;
}
}
best[i] = 1 + max;
pos[i] = poss;
}
}
void solveSecond(FILE *out, int *v, int *best, int *pos, int N){
// construirea secventei
int max = 0, *auxArray, poss, k = 0;
for(int i = 0; i < N; i++){
if( best[i] > max ){
max = best[i];
poss = i;
}
}
fprintf(out, "%d\n", max);
auxArray = malloc(max * sizeof(int));
// in aces moment avem maximul
while(1){
auxArray[k] = v[poss];
if(pos[poss] == -1){
break;
}
poss = pos[poss];
k++;
}
for(int i = k; i >= 0; i--){
fprintf(out, "%d ", auxArray[i]);
}
}
int main(){
FILE *in, *out;
int N, *v, *best, *pos;
in = fopen("scmax.in", "r");
out = fopen("scmax.out", "w");
fscanf(in, "%d", &N);
v = malloc( N * sizeof(int));
best = malloc( N * sizeof(int));
pos = malloc( N * sizeof(int));
readArray(in, v, N);
initializeArray(pos, N);
solveFirst(v, best, pos, N);
solveSecond(out, v, best, pos, N);
fclose(in);
fclose(out);
return 0;
}