#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int num[N];
int a[N];
int pre[N];
int pos[N];
void print(int x){
if(x==0) return;
print(pre[x]);
cout<<num[x]<<" ";
}
int main(){
int n;
while(cin>>n){
memset(a, 0x3f, sizeof(a));
int len = 0;
for(int i=1; i<=n; ++i){
cin>>num[i];
int ll = lower_bound(a, a+len, num[i]) - a;
if(ll+1 > len) len = ll+1;
a[ll] = num[i];//第ll个位置新插入的数字num[i]
pos[ll] = i;//第ll个位置插入的是第i个数字
if(ll-1>=0) pre[i] = pos[ll-1];//当前插入的第i个数字的在递增序列中的前一个数字是第几个!
}
print(pre[pos[len-1]]);
cout<<(num[pos[len-1]])<<endl;
cout<<len<<endl;
}
return 0;
}