All problems

Problem 301

Wave Array

Easy · 10 pts · Array, Sorting

Problem

A wave array goes up and down like a wave: the first element is at least as large as the second, the second is at most the third, the third is at least the fourth, and so on. Many arrangements are waves, so in this problem you must build one specific wave. First sort the array in ascending order. Then swap every pair of neighbours: the elements at positions 0 and 1, then positions 2 and 3, and so on. If N is odd, the last element stays where it is. Print the resulting array.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the N elements of the wave array, separated by spaces.

Constraints

1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000

Example input

5
3 1 5 2 4

Example output

2 1 4 3 5

Explanation

Sorted, the array is 1 2 3 4 5. Swapping the pairs (1,2) and (3,4) gives 2 1 4 3 5, and 5 stays last because N is odd. The values go down, up, down, up as required.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Stock Trading with K Transactions Next: Alternate Positive and Negative