#P11221. Sequence Deletion Operations

    ID: 13290 Type: Default 1000ms 256MiB

Sequence Deletion Operations

Sequence Deletion Operations

Given a sequence of positive integers \(a_0, a_1, \dots, a_{N-1}\) and a positive integer \(k\), perform exactly \(N\) deletion operations until the sequence becomes empty.

For each operation, let the current sequence have length \(n\). Define the set \[ S = \{0, k, 2k, \dots, k \lfloor \frac{n-1}{k} \rfloor\} \] among which the element to delete is determined as follows:

  • Find \(v = \max_{i \in S} a_i\).
  • Let \(p\) be the smallest index in \(S\) for which \(a_p = v\).
  • Delete the element \(a_p\) from the sequence (all subsequent elements shift one position to the left).

Output the deleted numbers in the order they are removed.

inputFormat

The first line contains two integers \(N\) and \(k\) separated by a space.

The second line contains \(N\) positive integers \(a_0, a_1, \dots, a_{N-1}\) separated by spaces.

outputFormat

Print \(N\) integers in one line separated by spaces. The \(i\)-th integer is the number deleted during the \(i\)-th operation.

sample

5 2
1 3 2 5 4
4 2 5 1 3