#P4650. Rearrange Cards for Target False Information Count

    ID: 17896 Type: Default 1000ms 256MiB

Rearrange Cards for Target False Information Count

Rearrange Cards for Target False Information Count

You are given N cards stacked in a pile. The i-th card carries an integer \(a_i\). The meaning of \(a_i\) is as follows: when the cards are arranged in some order (from top to bottom), let the number of cards with incorrect information that appear below a given card be \(F\). Then the card’s own information is correct if \(F \ge a_i\); otherwise, its information is incorrect. (Note that the bottom card has no cards below it, so for it \(F=0\).)

Your task is to rearrange the order of the cards (i.e. choose a permutation of the input cards) so that exactly K cards turn out to have incorrect information, according to the above rule. If there are multiple valid arrangements, you may output any one of them. If no such rearrangement exists, output -1.

Evaluation rule (bottom‐up): Suppose the final order of the cards (from top to bottom) is \(p_0, p_1, \dots, p_{N-1}\). Define a boolean value for each card by processing from the bottom up. Let \(F(i)\) be the number of cards with false information among cards in positions \(i+1, i+2, \dots, N-1\) (with \(F(N)=0\)). Then for the card at position \(i\), if \(F(i) \ge a_{p_i}\) it is considered correct; otherwise, it is incorrect. The goal is to have exactly K cards that are incorrect (i.e. that do not satisfy \(F(i) \ge a_{p_i}\)).

The formulas above are expressed in \(\LaTeX\) format.

inputFormat

The first line contains two integers N and K (1 ≤ N ≤ 10^5, 0 ≤ K ≤ N) — the number of cards and the required number of cards with incorrect information, respectively.

The second line contains N integers \(a_1, a_2, \dots, a_N\) (0 ≤ a_i ≤ N) where \(a_i\) is the number on the i-th card.

outputFormat

If there exists a valid rearrangement, output N integers on one line — the numbers on the cards in the new order from top to bottom. The resulting order must yield exactly K cards with incorrect information when the rule is applied from bottom to top.

If no valid rearrangement exists, output -1.

sample

3 1
0 1 1
1 1 0

</p>