#C4079. Smallest Lexicographic Array via Subarray Reversal

    ID: 47577 Type: Default 1000ms 256MiB

Smallest Lexicographic Array via Subarray Reversal

Smallest Lexicographic Array via Subarray Reversal

You are given an array of N integers and an integer K. You are allowed to perform the following operation any number of times:

  • Select any contiguous subarray of length \(K\) and reverse it.

Your task is to obtain the lexicographically smallest array possible by applying the operation any number of times.

Note: When \(K = 1\), no rearrangement is possible and the array remains unchanged. However, when \(K \geq 2\), it can be shown that any permutation of the array is achievable, so the lexicographically smallest array is simply the sorted order.

Input constraints: \(N\) is the size of the array, and \(K\) is the length for the subarray reversal operation.

Examples:

Input: 5 3
       4 3 2 1 5
Output: 1 2 3 4 5

Input: 5 1 5 4 3 2 1 Output: 5 4 3 2 1

</p>

inputFormat

The first line contains two integers \(N\) and \(K\), representing the number of elements in the array and the length of the subarray to be reversed, respectively. The second line contains \(N\) space-separated integers representing the array elements.

Input example:

5 3
4 3 2 1 5

outputFormat

Output the lexicographically smallest array obtained after performing the allowed operations. The output should be a single line with \(N\) space-separated integers.

Output example:

1 2 3 4 5
## sample
5 3
4 3 2 1 5
1 2 3 4 5