#include <fstream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
//#define int long long
//#define int short
#define pb push_back
#define f first
#define s second
using namespace std;
ifstream cin("scmax.in");
ofstream cout("scmax.out");
const int nmax = 1e5;
int n, v[nmax + 5], mxlen = 0;
int dp[nmax + 5];
int idx[nmax + 5];
int pre[nmax + 5];
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
void handleinput(){
cin>>n;
for (int i = 1; i <= n; i++){
cin>>v[i];
}
}
void calcdp(){
dp[1] = v[1];
idx[1] = 1;
pre[1] = 0;
mxlen = 1;
for (int i = 2; i <= n; i++){
int t = lower_bound(dp + 1, dp + mxlen + 1, v[i]) - dp;
dp[t] = v[i];
idx[t] = i;
pre[i] = idx[t - 1];
mxlen = max(mxlen, t);
}
}
vector<int> get(){
int mxidx = idx[mxlen];
vector<int> rez;
while (mxidx){
rez.pb(v[mxidx]);
mxidx = pre[mxidx];
}
reverse(rez.begin(), rez.end());
return rez;
}
void handleoutput(){
calcdp();
int mxidx = idx[mxlen];
cout<<mxlen<<"\n";
for (auto& idk : get()){
cout<<idk<<" ";
}
}
signed main(){
fastio();
handleinput();
handleoutput();
}