# include <bits/stdc++.h>
# define maxn 100005
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
//# define int ll
using namespace std;
int n;
int a[maxn];
int c[maxn];
vector<pair<int,int>>vec;
int prv[maxn];
pair<int,int> t[maxn * 4];
pair<int,int> mx = {0,0};
void upd(int nod,int l,int r,int pos,pair<int,int>val)
{
if(l == r)
{
t[nod] = val;
return;
}
int mid = l + r >> 1;
if(pos <= mid)
upd(nod << 1,l,mid,pos,val);
else upd(nod << 1 | 1,mid + 1,r,pos,val);
if(t[nod << 1].first > t[nod << 1 | 1].first)
t[nod] = t[nod << 1];
else t[nod] = t[nod << 1 | 1];
}
pair<int,int>qry(int nod,int tl,int tr,int l,int r)
{
if(l > r) return {0,0};
if(l == tl && r == tr) return t[nod];
int mid = tl + tr >> 1;
auto it1 = qry(nod << 1,tl,mid,l,min(mid,r));
auto it2 = qry(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r);
if(it1.first > it2.first) return it1;
else return it2;
}
int32_t main(){_
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
cin >> n;
for(int i = 1;i<=n;i++)
{
cin >> a[i];
vec.pb(mp(a[i],i));
}
sort(vec.begin(),vec.end());
int k = 1;
for(int i = 0;i<n;i++)
{
if(i && vec[i].first != vec[i - 1].first)
k++;
c[vec[i].second] = k;
}
for(int i = 1;i<=4*maxn;i++) t[i] = {0,0};
for(int i = 1;i<=n;i++)
{
auto x = qry(1,1,n,1,c[i] - 1);
prv[i] = x.second;
upd(1,1,k+1,c[i],{x.first + 1,i}); //
if(x.first + 1 > mx.first) mx = {x.first + 1,i};
}
cout << mx.first << '\n';
vector<int>ppap;
int spsm = mx.second;
while(spsm)
{
ppap.pb(a[spsm]);
spsm = prv[spsm];
}
reverse(ppap.begin(),ppap.end());
for(auto it : ppap) cout << it << ' ';
return 0;
}