#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, nr, Max, imax;
int v[100005];
vector <int> last[100005];
int search(int x){
int l = 0, r = nr;
while(l <= r){
int mid = (l + r) / 2;
if(last[mid].empty())
r = mid - 1;
else if(last[mid].back() >= x)
l = mid + 1;
else
r = mid - 1;
}
return l;
}
int main(){
fin >> n;
for(int i = 1; i <= n; i++){
fin >> v[i];
int pos = search(v[i]);
last[pos].push_back(v[i]);
if(pos == nr)
nr++;
}
for(int i = 0; i < nr; i++){
if(Max < last[i].size())
Max = last[i].size(), imax = i;
}
fout << Max << '\n';
for(int i = 0; i < last[imax].size(); i++)
fout << last[imax][i] << ' ';
return 0;
}