#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a<b)?b:a)
#define abs(a) ((a<0)?-a:a)
#define REP(i,a,b) \
for (int i=a; i<=b; i++)
#define INF 10000000000001
using namespace std;
//#define TEST
#ifdef TEST
ifstream fin("input.txt");
ofstream fout("output.txt");
#else
ifstream fin("scmax.in");
ofstream fout("scmax.out");
#endif // TEST
#define MAXN 2000000001
#define pb push_back
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> pp;
int n;
int a[100001];
int b[100001];
int m=0;
struct nn
{
int mi,ma;
};
vector<nn> bt;
void init()
{
int k;
k = (int)log2(n+1);
if (1<<k !=n+1) k++;
nn t;
t.mi = 0;
t.ma=0;
bt.resize(1<<k,t);
}
void update(int code, int l, int r, int id, int key)
{
if (l==r) {bt[code].mi=l;bt[code].ma=l;}
if (l>id) return ;
if (r<id) return;
if (l==r)
{
b[id] = key;
} else
{
update( code*2, l, (l+r)/2, id, key);
update(code*2+1, (l+r)/2+1,r ,id, key);
if (b[bt[code*2].mi]<b[bt[code*2+1].mi])
bt[code].mi = bt[code*2].mi;
else bt[code].mi = bt[code*2+1].mi;
if (b[bt[code*2].ma]>b[bt[code*2+1].ma])
bt[code].ma = bt[code*2].ma;
else bt[code].ma = bt[code*2+1].ma;
}
}
int mm=0;
void query(int code, int l, int r, int q)
{
if (b[bt[code].ma]<q) return;
if (b[bt[code].mi]>=q) {mm = min(mm,bt[code].mi); }
if (l==r) return;
query(code*2,l,(l+r)/2,q);
query(code*2+1,(l+r)/2+1,r,q);
}
int main()
{
fin>>n;
init();
for (int i=0; i<n; i++)
{
fin>>a[i];
b[i+1]=MAXN;
}
for (int i=1; i<=n; i++)
update(1,0,n,i,MAXN);
update(1,0,n,0,-1);
update(1,0,n,1,a[0]);
for (int i=1; i<n; i++)
{
int k;
mm=n;
query(1,0,n,a[i]);
k=mm;
k--;
update(1,0,n,k+1,a[i]);
}
for (int i=0; i<=n; i++)
if (b[i]<2000000000) m=i;
fout<<m;
fout<<'\n';
for (int i=1; i<=m; i++)
fout<<b[i]<<' ';
return 0;
}