#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
int up[3][10010],down[3][10010];
int main(){
freopen("maxxl.in","r",stdin);
freopen("maxxl.out","w",stdout);
int n;
memset(up,-1,sizeof(up));
memset(down,-1,sizeof(down));
cin>>n;
for (int i=0,j=n-1;i<n;i++,j--){
int temp;
cin>>temp;
up[0][i]=temp;
up[1][i]=1;
down[0][j]=temp;
down[1][j]=1;
}
for (int i=0;i<n;i++){
for (int j=0;j<i;j++){
if (up[0][j]<=up[0][i]&&up[1][j]+1>up[1][i]) {
up[1][i]=up[1][j]+1;
up[2][i]=j;
}
}
}
int max_up=0;
int iter_up;
for (int i=0;i<n;i++){
if (up[1][i]>max_up) {
max_up=up[1][i];
iter_up=i;
}
}
cout<<max_up<<endl ;
int i;
vector <int> ans;
i=iter_up;
for (i;i!=-1;i=up[2][i])
ans.push_back(up[0][i]);
for (vector <int>::iterator iter=ans.end()-1;iter>=ans.begin();iter--)
cout<<*iter<<" ";
return 0;
}