#K50287. Longest Non-Decreasing Subsequence

    ID: 28831 Type: Default 1000ms 256MiB

Longest Non-Decreasing Subsequence

Longest Non-Decreasing Subsequence

You are given an array of integers. Your task is to find the longest non-decreasing subsequence in the array. A subsequence is formed by deleting some or no elements from the array without changing the order of the remaining elements.

For instance, given the array [5, 3, 4, 8, 6], one valid answer is [3, 4, 8] since 3 ≤ 4 ≤ 8 and its length is maximum among all possible non-decreasing subsequences.

Recall that a sequence (A = [a_1, a_2, \dots, a_n]) is non-decreasing if (a_i \le a_{i+1}) for all (1 \le i < n).

inputFormat

Input is read from standard input (stdin). The first line contains an integer (T) representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer (n), the number of elements in the array.
- The second line contains (n) space-separated integers representing the array elements.

outputFormat

For each test case, output the longest non-decreasing subsequence on a single line. The elements should be printed in order and separated by a space. For an empty array, output an empty line.## sample

1
5
5 3 4 8 6
3 4 8

</p>