#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+1;
int n, a[N];
int dp[N], parent[N], best[N], bestidx[N], k;
int mx=1, start=1;
// dp[i] -> lung max subsir ce se termina in i
// best[i] -> ultimul element in sir; st este lungimea
// parent[i] -> de unde vin
int cb(int el){ // best[m]<el el<=best[m+1]
int l=1, r=k, m=l+r>>1, poz=k+1;
while(l<=r){
m=l+r>>1;
if(best[m] >= el){
poz = m;
r = m-1;
}
else{
l = m+1;
}
}
return poz;
}
void get_best(int start){
if(parent[start]){
get_best(parent[start]);
}
cout<<a[start]<<' ';
}
signed main()
{
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1; i<=n; ++i){
cin>>a[i];
}
dp[1] = bestidx[1] = 1;
best[1] = a[1];
parent[1] = 0;
k = 0;
for(int i=1; i<=n;++i){
int poz = cb(a[i]);
best[poz] = a[i];
dp[i] = poz;
bestidx[poz] = i;
parent[i] = (poz == 1 ? 0 :bestidx[poz-1]);
if(poz == k+1) ++k;
if(mx<dp[i]){
mx = dp[i];
start = i;
}
}
cout<<mx<<'\n';
get_best(start);
return 0;
}