#T138. 求最长不下降序列

求最长不下降序列

Description

Given a sequence of nn (where 1n2001 \leq n \leq 200) distinct integers, denoted as b(1),b(2),,b(n)b(1), b(2), \ldots, b(n), with b(i)b(j)b(i) \neq b(j) for iji \neq j,
if there exist indices i1<i2<i3<<iei_1 < i_2 < i_3 < \ldots < i_e such that b(i1)b(i2)b(ie)b(i_1) \leq b(i_2) \leq \ldots \leq b(i_e), then this is called a non-decreasing subsequence of length ee.

The task is to find the longest non-decreasing subsequence after the original sequence is given.

For example, consider the sequence:
13, 7, 9, 16, 38, 24, 37, 18, 44, 19, 21, 22, 63, 15.

In this case, 13, 16, 18, 19, 21, 22, 63 is a non-decreasing subsequence of length 7,
while 7, 9, 16, 18, 19, 21, 22, 63 forms a longer non-decreasing subsequence of length 8.

Input Format

  • The first line contains an integer nn.
  • The second line contains nn space-separated integers.

Output Format

  • The first line should output the maximum length max (as shown in the sample).
  • The second line should output one of the longest non-decreasing subsequences. The answer may not be unique; any valid sequence is acceptable. Special judging will be applied for this problem.

Sample Input

14
13 7 9 16 38 24 37 18 44 19 21 22 63 15

Sample Output

max=8
7 9 16 18 19 21 22 63
14
13 7 9 16 38 24 37 18 44 19 21 22 63 15

max=8