#K75262. Wave Sort

    ID: 34381 Type: Default 1000ms 256MiB

Wave Sort

Wave Sort

Given an array of integers, rearrange the array into a wave-like pattern. In other words, the array must satisfy the conditions:

(a_0 \ge a_1), (a_1 \le a_2), (a_2 \ge a_3), (a_3 \le a_4), and so on.

For example, if the input array is [3, 6, 5, 10, 7, 20], one valid wave-like ordering is [6, 3, 10, 5, 20, 7].

The conditions can be summarized as: for every even index (i), if (a_{i+1}) exists then (a_i \ge a_{i+1}); for every odd index (i), if (a_{i+1}) exists then (a_i \le a_{i+1}).

inputFormat

The input is given via standard input in the following format:

(n)
(a_1\ a_2\ ...\ a_n)

Here, the first line contains an integer (n) representing the number of elements, and the second line contains (n) space-separated integers.

outputFormat

Output the wave sorted array on one line with each element separated by a single space via standard output.## sample

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