#K32742. Maximize Absolute Difference

    ID: 24933 Type: Default 1000ms 256MiB

Maximize Absolute Difference

Maximize Absolute Difference

Given a list of n plant heights, rearrange them into a sequence that maximizes the sum of the absolute differences between adjacent plants. Formally, suppose we have a sequence A = [a₁, a₂, …, aₙ]. We want to maximize the sum \(\sum_{i=1}^{n-1} |a_{i+1} - a_i|\). It can be proven that a greedy strategy is effective: first sort the list and then choose the largest and smallest elements alternately.

Your task is to implement an algorithm that reads the number of plants and their corresponding heights from the standard input, processes them as described, and then outputs the rearranged sequence in which the heights are space-separated.

inputFormat

The input is given in two lines:

  • The first line contains an integer n, representing the number of plants.
  • The second line contains n space-separated integers representing the heights of the plants.

outputFormat

Output a single line containing n space-separated integers representing the rearranged sequence that maximizes the sum of absolute differences between consecutive elements.

## sample
5
4 2 1 6 5
6 1 5 2 4