// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//trie
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#define MAX_INT 0x3f3f3f3f;
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
/*
struct trieNode {
int Min, left, right;
trieNode* leftNode, *rightNode;
trieNode() {
Min = 0;
left = 0;
right = 0;
leftNode = nullptr;
rightNode = nullptr;
//ctor
}
~trieNode() {
//dtor
}
void buildTree(trieNode* current, int left, int right) {
}
};
*/
int segTree1[1000005];
void constructTree(vector<int> input, int segTree[], int low, int high, int pos) {
if (low == high) {
segTree[pos] = input[low];
return;
}
int mid = (low + high) / 2;
constructTree(input, segTree, low, mid, 2 * pos + 1);
constructTree(input, segTree, mid + 1, high, 2 * pos + 2);
segTree[pos] = min(segTree[2 * pos + 1], segTree[2 * pos + 2]);
}
int rangeMinQuery(int segTree[], int low, int high, int qleft, int qright, int pos) {
if (qleft <= low && high <= qright)
return segTree[pos];
else {
if (qleft > high || qright < low) {
return MAX_INT;
}
int mid = (low + high) / 2;
return min(rangeMinQuery(segTree, low, mid, qleft, qright, 2 * pos + 1),
rangeMinQuery(segTree, mid + 1, high, qleft, qright, 2 * pos + 2));
}
}
vector<int>v1;
//rangeMinQuery using sparse table
//endof
//subsir crescator maximal
void initLIS(vector<int> LIS) {
int a = LIS.size();
for (int i = 0; i < a; i++) {
LIS[i] = 1;
}
}
vector<int>result[100005];
int SCM(vector<int> array, vector<int> LIS) {
int n = array.size();
int Max = -1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (array[i] > array[j] && LIS[j] + 1 > LIS[i]) {
LIS[i] = LIS[j] + 1;
Max = max(Max, LIS[i]);
result[i].push_back(array[j]);
}
}
}
return Max;
}
//endof
int main()
{
/*
int n, m,a,b;
fin >> n >> m;
for (int i = 0; i < n; i++) {
fin >> a;
v1.push_back(a);
}
constructTree(v1, segTree1, 0, n - 1, 0);
for (int i = 0; i < m; i++) {
fin >> a >> b;
fout << rangeMinQuery(segTree1, 0, n - 1, a - 1, b - 1, 0) << '\n';
}
*/
int n, m, a, b;
fin >> n;
vector<int>LIS(n, 1);
for (int i = 0; i < n; i++) {
fin >> a;
v1.push_back(a);
}
fout << SCM(v1, LIS) << '\n';
int posMax = -1;
int vMax = -1;
for (int i = 0; i < n; i++) {
if (vMax < LIS[i]) {
posMax = i;
}
}
for (int i = 0; i < result[posMax].size(); i++) {
fout << result[posMax][i] << ' ';
}
fout << v1[posMax];
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file