#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
// dp[i] 表示以a[i]结尾的最长不下降子序列长度
vector<int> dp(n, 1);
// pre[i] 记录以a[i]结尾的最长序列中,前一个元素的下标
vector<int> pre(n, -1);
int max_len = 1; // 最长序列长度
int last_idx = 0; // 最长序列最后一个元素的下标
// 动态规划计算dp数组和前驱数组
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (a[j] < a[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
pre[i] = j;
}
}
// 更新最长序列信息
if (dp[i] > max_len) {
max_len = dp[i];
last_idx = i;
}
}
// 回溯获取最长序列
vector<int> res;
while (last_idx != -1) {
res.push_back(a[last_idx]);
last_idx = pre[last_idx];
}
reverse(res.begin(), res.end()); // 反转得到正确顺序
// 输出结果
cout << max_len << endl;
for (int i = 0; i < res.size(); ++i) {
if (i > 0) cout << " ";
cout << res[i];
}
cout << endl;
return 0;
}
( )