#C2051. Maximize Mood Difference

    ID: 45325 Type: Default 1000ms 256MiB

Maximize Mood Difference

Maximize Mood Difference

You are given an integer n representing the number of seats arranged in a circle and a list of n integers which denote the mood levels of each seat. Your task is to rearrange the mood levels around the circular table in such a way that the maximum absolute difference between every two adjacent mood levels is maximized.

More formally, if the rearranged order is \(a_1, a_2, \ldots, a_n\) then you need to maximize \[ \max\{|a_1 - a_2|, |a_2 - a_3|, \ldots, |a_{n-1} - a_n|, |a_n - a_1|\} \] and output the maximum difference along with the corresponding arrangement.

Example: For input 5 and mood levels 3 8 2 10 6, one optimal arrangement is 2 10 3 8 6 with a maximum difference of 8.

inputFormat

The input is read from standard input. It consists of two lines:

  • The first line contains an integer n denoting the number of seats.
  • The second line contains n space-separated integers representing the mood levels of each seat.

outputFormat

The output is printed to standard output and consists of two lines:

  • The first line contains an integer representing the maximum absolute difference between adjacent mood levels in the optimally rearranged sequence.
  • The second line contains the rearranged sequence of mood levels (space separated) that yields this maximum difference.
## sample
5
3 8 2 10 6
8

2 10 3 8 6

</p>