#C6251. Lexicographically Smallest Array via Prefix Reversal

    ID: 49991 Type: Default 1000ms 256MiB

Lexicographically Smallest Array via Prefix Reversal

Lexicographically Smallest Array via Prefix Reversal

You are given an array of integers and an integer ( k ). In one operation, you can choose an index ( i ) (where ( 0 \le i < n )) and reverse the subarray from the beginning to ( i ) (i.e. the prefix ( a[0 \ldots i] )). Formally, if the array is ( a = [a_0, a_1, \ldots, a_{n-1}] ), then performing the operation on index ( i ) transforms the array into ( [a_i, a_{i-1}, \ldots, a_0, a_{i+1}, \ldots, a_{n-1}] ).

Your task is to perform at most ( k ) such operations to obtain the lexicographically smallest possible array. If at any point the array is already in its lexicographically smallest possible configuration (i.e. the first element is equal to ( \min(a) )), no further operations will be performed.

Note: The lexicographical order compares arrays element by element. For example, ([1, 2, 3]) is lexicographically smaller than ([1, 3, 2]).

inputFormat

The first line of input contains two integers ( n ) and ( k ), where ( n ) is the number of elements in the array and ( k ) is the maximum number of allowed operations. The second line contains ( n ) space-separated integers representing the array.

outputFormat

Output the lexicographically smallest array achievable after performing at most ( k ) prefix reversal operations. Print the array as ( n ) space-separated integers on a single line.## sample

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