Description
Before the game starts, (n) coins are placed on a ring clockwise, and the (i) coin has a face value of (w_i).
At each step of the game, you need to select one of the coins, place it in the position of the next coin in the clockwise direction, and remove the coin that has been covered. Your score in this step is the absolute value of the difference between the face values of the two coins. After that, you need to continue the operation until there is only one coin left. Your final score is the sum of the scores of each step.
Please write a program to find the best strategy to maximize the final score.
Format
Input
The first line contains a positive integer (n), which represents the number of coins.
The second line contains (n) positive integers (w_1), (w_2), ..., (w_n), which represent the face value of each coin in clockwise order.
Output
Output an integer per line, which is the maximum value of the final score.
Sample
Input
4
3 4 5 6
Output
6
Hint
For the data of (100\%), (1 le w_i le 10^6).
Test point number | (n) |
---|---|
(1,2,3) | (leq 5) |
(4,5,6) | (leq 20) |
(7,8,9,10) | (leq 300) |
Sample Code
Code is not available!