#P3487. Lexicographically Maximum Subsequence

    ID: 16742 Type: Default 1000ms 256MiB

Lexicographically Maximum Subsequence

Lexicographically Maximum Subsequence

Given a sequence (a_i) where (1 \leq a_i \leq 10^9) for (1 \leq i \leq n) with (n \leq 1.5\times10^7), and an integer (k) (with (k \leq n) and (k \leq 10^6)), find a subsequence (a_{b_i}) of length (k) satisfying:

  1. (1 \leq b_1 < b_2 < \cdots < b_k) (i.e. indices in the subsequence are in increasing order),
  2. Among all subsequences of length (k), the sequence (a_{b_1}, a_{b_2}, \ldots, a_{b_k}) is lexicographically maximum.

    A lexicographical comparison is made by comparing corresponding elements from left to right until a difference is found.

inputFormat

The first line contains two integers (n) and (k).
The second line contains (n) integers: (a_1, a_2, \ldots, a_n).

outputFormat

Output (k) integers representing the lexicographically maximum subsequence of the given sequence.

sample

5 3
1 2 3 4 5
3 4 5