#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, len, v[100100], aib[400100], sol[100100], ant[100100];
void update(int nod, int st, int dr, int poz, int value) {
if (st == dr) {
aib[nod] = value;
return;
}
int mij = (st + dr) / 2;
if (poz <= mij)
update(2 * nod, st, mij, poz, value);
else
update(2 * nod + 1, mij + 1, dr, poz, value);
aib[nod] = max(aib[2 * nod], aib[2 * nod + 1]);
}
int query(int nod, int st, int dr, int value) {
if (st == dr) {
if (value > aib[nod])
st++;
return st;
}
int mij = (st + dr) / 2;
if (aib[2 * nod + 1] == 0)
return query(2 * nod, st, mij, value);
if (value > aib[2 * nod])
return query(2 * nod + 1, mij + 1, dr, value);
return query(2 * nod, st, mij, value);
}
void afisare(int poz) {
if (poz == 0)
return;
afisare(ant[poz]);
fout << v[poz] << ' ';
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++)
fin >> v[i];
for (int i = 1; i <= n; i++) {
int poz = max(1, query(1, 0, n, v[i]));
len = max(len, poz);
if (v[i] < v[sol[poz]] || len == poz) {
ant[i] = sol[poz - 1];
sol[poz] = i;
update(1, 0, n, poz, v[i]);
}
}
fout << len << '\n';
afisare(sol[len]);
return 0;
}