#K50317. Optimal Baton Passing

    ID: 28838 Type: Default 1000ms 256MiB

Optimal Baton Passing

Optimal Baton Passing

You are given a list of integers representing the stamina ratings of team members. The team must arrange themselves in a circular order in order to minimize the total exhaustion, which is defined as the sum of the absolute differences between consecutive stamina ratings (including the difference between the last and the first member).

The task is to determine the minimum possible total exhaustion achievable by any arrangement. A key observation is that on a number line, the optimal route that visits all points and returns to the starting point is obtained by simply going from the smallest value to the largest and then returning back to the smallest. Therefore, the minimum exhaustion is given by:

\(2 \times (\max(a) - \min(a))\)

For example, if the ratings are [10, 20, 10, 15], the minimum exhaustion is \(2\times(20-10)=20\). If all ratings are equal, the exhaustion is 0.

inputFormat

The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), the number of team members. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) where \(0 \le a_i \le 10^9\), representing the stamina ratings.

outputFormat

Output a single integer, the minimum total exhaustion achievable by arranging the team members optimally in a circle.

## sample
4
10 20 10 15
20