#P9928. Subsequence with Exactly k Records

    ID: 23072 Type: Default 1000ms 256MiB

Subsequence with Exactly k Records

Subsequence with Exactly k Records

You are given a permutation p of {1, 2, …, n} and an integer k. A subsequence of p is defined by indices i1, i2, …, im with 1 ≤ i1 < i2 < … < im ≤ n.

We say that the j-th element of the subsequence, pij, is record if it is the j-th smallest element among the subsequence. Equivalently, for j > 1 this means that pij > max(pi1, pi2, …, pij-1) (note that the first element is always a record).

Your task is to find any subsequence of p such that the number of record elements is exactly k. If there are multiple answers, output any one of them; if no valid subsequence exists, output -1.

Note: The records in the subsequence are exactly the positions where an element is greater than every element that has appeared before it.

The formulas in this problem should be written in LaTeX format. For example, a permutation of 1 to n should be represented as: \( 1\sim n \) and a subsequence as \( \{p_{i_1}, p_{i_2}, \cdots, p_{i_m}\} \).

inputFormat

The first line contains two integers n and k (n is the size of the permutation, and k is the desired number of records in the subsequence).

The second line contains n space-separated integers representing the permutation p of \( 1\sim n \). All elements are distinct.

outputFormat

If a valid subsequence exists, print the elements of one such subsequence in the order they appear in p, separated by spaces.

If no valid subsequence exists, print -1.

sample

5 2
3 1 2 5 4
3 1 5 4