#K86697. Reordering Sequence Based on Subarray Minimums

    ID: 36922 Type: Default 1000ms 256MiB

Reordering Sequence Based on Subarray Minimums

Reordering Sequence Based on Subarray Minimums

You are given two integers K and N and a sequence A consisting of N integers. Your task is to determine if it is possible to reorder the sequence such that it can be partitioned into contiguous subarrays of size K (with no leftover elements) and in each subarray, the first element is the minimum element within that subarray.

If N is not divisible by K or if N is less than K, then the answer is NO. Otherwise, output the reordered sequence (which is the sequence sorted in ascending order) as a single line of space-separated integers. Formally, the condition for each valid contiguous subarray (of size K) is given by:

\( min\{a_{i}, a_{i+1}, \dots, a_{i+K-1}\} = a_{i}\ \) for every subarray starting at an index that is a multiple of K.

If no valid reordering exists, output NO.

inputFormat

The input is read from standard input and has the following format:

K N
A[0] A[1] ... A[N-1]

Where:

  • K is the size of each subarray.
  • N is the total number of elements in the sequence.
  • A is a sequence of N space-separated integers.

outputFormat

Output to standard output a single line. If a valid reordering exists, output the reordered sequence as space-separated integers. Otherwise, output NO.

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

</p>