#P1750. 出栈序列

出栈序列

Description

Given a sequence of nn elements, you need to push its elements in order into a stack of capacity cc and pop them. Arranging the elements according to their pop order yields a new sequence. There can be many such sequences; among all new sequences, output the one whose first element is the smallest (if there are multiple with the smallest first element, make the second as small as possible; if still multiple, make the third smallest, and so on).

Input Format

The first line contains two integers n,cn, c.

The second line contains nn numbers, the values of the nn elements in the sequence.

Output Format

Output nn numbers, the sequence that meets the requirement.

6 3
5 2 3 8 7 4
2 3 5 4 7 8

Hint

For 40%40 \% of the testdata, n12n \le 12. For 100%100 \% of the testdata, 1cn100001 \le c \le n \le 10000, and each element lies in [1,2×109][1, 2 \times {10}^9].

Translated by ChatGPT 5