Problem list
Log in
#icpc00140 · ICPC NWERC 2020

Hot Springs

Silver II 5.0 s 2048 MB

Iceland is famous for its geothermal activity, which supplies the country with much of its electricity and heating. It is also a source of pleasure, in the form of hot springs.

Kalle is visiting one of Iceland's famous hot springs. It contains $n$ pools of water, where the $i$th one has temperature $t_i$. Although staying in one of the warmer pools for a long time sure is relaxing, Kalle is on a very tight schedule and just wants a quick dip in each of the pools. As you may know, the nicest thing about hot baths is the contrast between hot and cold. Therefore, to get the most out of his stay, Kalle wants to find an ordering of the pools so that the difference in temperature between subsequent pools is increasing.

Given a sequence of pool temperatures $t_1, t_2, \dots, t_n$, rearrange them into a new sequence $t'_1, t'_2, \dots, t'n$ such that for all $2 \leq i \leq n-1$ it holds that $$ |t'{i-1} - t'_i| \leq |t'i - t'{i+1}|. $$

Input

The input consists of:

  • One line with an integer $n$ ($2 \le n \leq 10^5$), the number of pools.
  • One line with $n$ integers $t_1, \ldots, t_n$ ($-10^5 \leq t_i \leq 10^5$ for each $i$), the temperatures in each of the $n$ pools.

Output

Output a rearrangement of the sequence satisfying the given requirement. If no solution exists, output "impossible". If there are multiple valid solutions, you may output any one of them.

Example 1

Input
3
1 3 4
Output
4 3 1

Example 2

Input
6
0 0 1 -1 -6 3
Output
0 1 3 -1 -6 0
Source
ICPC NWERC 2020
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code