Cod sursa(job #858637)
#include <iostream>
#include <fstream>
using namespace std;
int a[100005];
int L[100005];
int main()
{
ifstream cin("scmax.in");
ofstream cout("scmax.out");
int i, j, n, t;
cin >> n;
for (i = 1; i <= n; i++)
cin >> a[i];
t = n;
for (i = n; i >= 1; i--)
{
L[i] = 0;
for (j = i + 1; j <= n; j++)
if (a[j] > a[i] && L[j] > L[i])
L[i] = L[j];
L[i]++;
if (L[i] > L[t])
t = i;
}
cout << L[t] << '\n';
for (i = t + 1; i <= n; i++)
{
if (a[i] > a[t] && L[i] == L[t] - 1)
{
cout << a[t] << ' ';
t = i;
}
}
cout << a[t];
return 0;
}