#include <bits/stdc++.h>

using namespace std;
const int INF = 1e9;

int main()
{
   int N;
   cin >> N;

   vector<int> a(N);
   for (int i = 0; i < N; i++)
      cin >> a[i];

   vector<int> d(N + 1, INF);
   vector<int> pos(N + 1, -1);
   vector<int> p(N, -1);

   d[0] = -INF;
   int ans = 0;
   for (int i = 0; i < N; i++)
   {
      int l = upper_bound(d.begin(), d.end(), a[i]) - d.begin();
      if (d[l - 1] < a[i] && a[i] < d[l])
      {
         d[l] = a[i];
         pos[l] = i;
         p[i] = (l > 1 ? pos[l - 1] : -1);
         ans = max(ans, l);
      }
   }

   vector<int> subseq;
   int cur = pos[ans];
   while (cur != -1)
   {
      subseq.push_back(a[cur]);
      cur = p[cur];
   }
   reverse(subseq.begin(), subseq.end());

   cout << ans << endl;
   for (int i = 0; i < ans; i++)
   {
      if (i) cout << " ";
      cout << subseq[i];
   }
   cout << endl;

   return 0;
}
