Suffix Operations
시간 제한1초메모리 제한512 MB
배열의 한 원소를 최대 한 번 바꿀 수 있을 때, 접미사에 +1/-1을 적용해 모든 원소를 같게 만드는 최소 연산 횟수를 구한다.
문제
Gildong has an interesting machine that has an array with integers. The machine supports two kinds of operations:
- Increase all elements of a suffix of the array by .
- Decrease all elements of a suffix of the array by .
A suffix is a subsegment (contiguous elements) of the array that contains . In other words, for all where is included in the subsegment, all ’s where must also be included in the subsegment.
Gildong wants to make all elements of equal --- he will always do so using the minimum number of operations necessary. To make his life even easier, before Gildong starts using the machine, you have the option of changing one of the integers in the array to any other integer. You are allowed to leave the array unchanged. You want to minimize the number of operations Gildong performs. With your help, what is the minimum number of operations Gildong will perform?
Note that even if you change one of the integers in the array, you should not count that as one of the operations because Gildong did not perform it.
입력
Each test contains one or more test cases. The first line contains the number of test cases ().
Each test case contains two lines. The first line of each test case consists of an integer () --- the number of elements of the array .
The second line of each test case contains integers. The -th integer is ().
It is guaranteed that the sum of in all test cases does not exceed .
출력
For each test case, print one integer --- the minimum number of operations Gildong has to perform in order to make all elements of the array equal.
힌트
In the first case, all elements of the array are already equal. Therefore, we do not change any integer and Gildong will perform zero operations.
In the second case, we can set to be , so that the array becomes . Now Gildong can use the -nd operation once on the suffix starting at , which means and are decreased by , making all elements of the array .
In the third case, we can set to , so that the array becomes . Now Gildong needs to:
- Use the -nd operation on the suffix starting at once, making the array .
- Use the -st operation on the suffix starting at times, making the array .
In the fourth case, we can change the array into . Now Gildong needs to:
- Use the -nd operation on the suffix starting at times, making the array .
- Use the -nd operation on the suffix starting at once, making the array .