#C6284. Zigzag Sorting

    ID: 50027 Type: Default 1000ms 256MiB

Zigzag Sorting

Zigzag Sorting

You are given an array of integers. Your task is to rearrange the elements of the array into a zigzag order where the numbers alternate between the smallest available element and the largest available element.

More formally, let \(a_1, a_2, \ldots, a_n\) be the sorted array in non-decreasing order. You need to construct a new array \(b\) such that:

\[ b_1 = a_1,\quad b_2 = a_n,\quad b_3 = a_2,\quad b_4 = a_{n-1},\quad \ldots \]

If there is a middle element (when \(n\) is odd), append it at the end of the array.

For example, given the array [3, 1, 4, 5, 2], after sorting you get [1, 2, 3, 4, 5]. The zigzag order will be [1, 5, 2, 4, 3].

inputFormat

The input is read from stdin and consists of two lines:

  1. The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)), which is the number of elements in the array.
  2. The second line contains \(n\) space-separated integers representing the elements of the array.

outputFormat

Output the zigzag sorted array to stdout in a single line where the elements are separated by a single space.

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