#include <bits/stdc++.h>
using namespace std;
int v[100001], aib[100001], l[100001], cv[100001];
int n;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
void normalizare() {
int i, g=0;
vector <pair<int,int> > aux(n);
for (i=0;i<n;i++) {
aux[i].first=cv[i];
aux[i].second=i;
}
sort(aux.begin(),aux.end());
v[aux[0].second] = ++g;
for (i=1;i<n;i++) {
if (aux[i].first>aux[i-1].first) {
g++;
}
v[aux[i].second]=g;
}
}
void update(int i, int x) {
int l=i;
while (l<=n) {
aib[l]=max(aib[l],x);
l+=l&(-l);
}
}
int query(int i) {
int mx=-1;
while (i>0) {
mx=max(mx,aib[i]);
i-=i&(-i);
}
return mx;
}
void refsir(int lung, int lim, int poz) {
if (lung==0) {
return;
}
if (cv[poz]<lim&&l[poz]==lung) {
refsir(lung-1,cv[poz],poz-1);
fout<<cv[poz]<<' ';
}
else {
refsir(lung,lim,poz-1);
}
}
int main() {
int i, poz=0;
fin>>n;
for (i=0; i<n; i++) {
fin>>v[i];
cv[i]=v[i];
}
normalizare();
for (i=0; i<n; i++) {
l[i]=query(v[i])+1;
if (l[i]>l[poz]) {
poz=i;
}
update(v[i],l[i]);
}
fout<<l[poz]<<'\n';
refsir(l[poz],1e9,poz);
return 0;
}