文件目录

#include <iostream>
#include <stack>
using namespace std;
int n, b[205], dp[205], fat[205];
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> b[i]; // 5 3 1
    fill(dp, dp + 205, 1);
    int maxi = 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j < i; j++)
            if (b[j] <= b[i] && dp[i] < dp[j] + 1)
            {
                dp[i] = dp[j] + 1;
                fat[i] = j;
                if (dp[i] > dp[maxi])
                    maxi = i;
            }
    cout << "max=" << dp[maxi] << endl;
    stack<int> s;
    for (int i = maxi; i != 0; i = fat[i])
        s.push(b[i]);
    while (!s.empty())
    {
        cout << s.top() << " ";
        s.pop();
    }
    return 0;
}