#include <bits/stdc++.h>
#define LAST_ONE_BIT ((x ^ (x - 1)) & x)
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, best, v[100100], aib[100100], sorted[100100], ant[100100], sol[100100];
void afisare(int poz) {
if (poz == 0)
return;
afisare(ant[poz]);
fout << v[poz] << ' ';
}
int query(int x) {
int maxx = 0;
for (; x; x -= LAST_ONE_BIT)
if (sol[aib[x]] > sol[maxx] || sol[aib[x]] == sol[maxx] && v[aib[x]] < v[maxx])
maxx = aib[x];
return maxx;
}
void update(int x, int poz) {
for (; x <= n; x += LAST_ONE_BIT)
if (sol[poz] > sol[aib[x]] || sol[poz] == sol[aib[x]] && v[poz] < v[aib[x]])
aib[x] = poz;
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++)
fin >> v[i];
for (int i = 1; i <= n; i++)
sorted[i] = v[i];
sort(sorted + 1, sorted + n + 1);
int h = 0;
for (int i = 1; i <= n; i++)
if (sorted[h] != sorted[i])
sorted[++h] = sorted[i];
for (int i = 1; i <= n; i++) {
int poz_in_sorted = lower_bound(sorted + 1, sorted + h + 1, v[i]) - sorted;
ant[i] = query(poz_in_sorted - 1);
sol[i] = sol[ant[i]] + 1;
update(poz_in_sorted, i);
if (sol[i] > sol[best])
best = i;
}
fout << sol[best] << '\n';
afisare(best);
return 0;
}