#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
const int inf = 2e9 + 5;
int best[nmax+5], lg[nmax+5], n, v[nmax+5];
// best[i] - valoarea minima cu care se termina un sir de lungimea i
int Query(int val) {
int pos = 0;
for(int p=20; p>=0; p--)
if(pos + (1<<p) <= n and best[pos + (1<<p)] < val)
pos += (1<<p);
return pos + 1;
}
int main() {
ifstream f("scmax.in");
ofstream g("scmax.out");
f >> n;
for(int i=1; i<=n; i++) best[i] = inf;
int mx = 0;
for(int i=1; i<=n; i++) {
f >> v[i];
int w = Query(v[i]);
lg[i] = w;
best[w] = v[i];
mx = max(mx, w);
}
g << mx << "\n";
vector<int> sol;
for(int i=n; i>=1; i--) {
if(lg[i] == mx) {
sol.emplace_back(v[i]);
mx--;
}
}
for(int i=sol.size()-1; i>=0; i--) g << sol[i] << " ";
return 0;
}