#K56847. Wave Array Sorting

    ID: 30289 Type: Default 1000ms 256MiB

Wave Array Sorting

Wave Array Sorting

Given an unsorted array of integers, your task is to rearrange the array into a wave-like form. In other words, you need to reorder the array so that it satisfies the following pattern:

$a_0 \geq a_1 \leq a_2 \geq a_3 \leq a_4 \ldots$

A common strategy is to first sort the array and then swap every adjacent pair of elements. Note that there are multiple valid solutions as long as the wave condition holds. For example, if the input array is [3, 6, 5, 10, 7, 20], one valid output is "5 3 7 6 20 10". Make sure to follow the input and output format strictly.

inputFormat

The input is given via standard input (stdin). The first line contains a single integer n, representing the number of elements in the array. The second line contains n space-separated integers.

outputFormat

Output a single line (to standard output, stdout) containing n space-separated integers representing the wave-sorted array.## sample

6
3 6 5 10 7 20
5 3 7 6 20 10