#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int v[MAXN + 1], old_v[MAXN + 1], aib[MAXN + 1], lung[MAXN + 1];
struct per {
int val, ind;
} sorted[MAXN + 1];
bool cmp(per A, per B) {
if (A.val != B.val) return A.val < B.val;
return A.ind < B.ind;
}
void norm(int n) {
sort(sorted + 1, sorted + n + 1, cmp);
int curr = 0;
sorted[0].val = INT_MIN;
for (int i = 1; i <= n; i++) {
if (sorted[i].val > sorted[i - 1].val) {
curr++;
}
v[sorted[i].ind] = curr;
}
}
int lsb(int x) {
return (x & (-x));
}
void update(int n, int poz, int val) {
while (poz <= n) {
aib[poz] = max(aib[poz], val);
poz += lsb(poz);
}
}
int query(int poz) {
int ans = 0;
while (poz > 0) {
ans = max(ans, aib[poz]);
poz -= lsb(poz);
}
return ans;
}
int main() {
int n, i, cur_len, poz_max;
if (!(fin >> n)) return 0;
for (i = 1; i <= n; i++) {
fin >> old_v[i];
sorted[i].val = old_v[i];
sorted[i].ind = i;
}
norm(n);
poz_max = 1;
for (i = 1; i <= n; i++) {
cur_len = query(v[i] - 1) + 1;
lung[i] = cur_len;
if (lung[i] > lung[poz_max]) {
poz_max = i;
}
update(n, v[i], cur_len);
}
fout << lung[poz_max] << "\n";
vector<int> sol;
int lung_c = lung[poz_max];
int reper = INT_MAX;
for (i = poz_max; i >= 1 && lung_c > 0; i--) {
if (lung[i] == lung_c && old_v[i] < reper) {
sol.push_back(old_v[i]);
reper = old_v[i];
lung_c--;
}
}
for (int j = (int)sol.size() - 1; j >= 0; j--) {
fout << sol[j] << " ";
}
fout << "\n";
fin.close();
fout.close();
return 0;
}