This problem author has OCD (obsessive–compulsive disorder). One day his teacher gave him an array of elements.
Let's define as the sum of absolute difference of adjacent elements.
Guess what? He doesn't like the array so much. Because he found that the value of is too high. He wants to have a value as low as possible.
But again he has OCD. He doesn't want to move the elements of the array too much. He rearranges the array such that every element is either at it's own position, or at it's left position, or at it's right position. In other words, the -th number should be at one of the three possible positions: , , or . No two numbers should stay at the same position. And no position should be left blank.
Note that for the first element he can't move the element to left, and for the last element he can't move the element to right.
Now he asks you, after rearranging the numbers, what's the lowest value of ?
The input will start with the integer () denoting the size of the array. On the next line there will be integers which are elements of the array. Each integer () of the array will be separated by a single space.
Print the lowest value of .
Input | Output |
---|---|
5 2 1 4 3 5 | 4 |
In the given sample test case, the first element moves to right, second element moves to left, third element moves to right, fourth element moves to left. So the new array will look like For this new array, (which is the minimum possible value of ). |