Suffix Operations
Gildong has an interesting machine that has an array $$$a$$$ with $$$n$$$ integers. The machine supports two kinds of operations:  Increase all elements of a suffix of the array by $$$1$$$.  Decrease all elements of a suffix of the array by $$$1$$$. A suffix is a subsegment (contiguous elements) of the array that contains $$$a_n$$$. In other words, for all $$$i$$$ where $$$a_i$$$ is included in the subsegment, all $$$a_j$$$'s where $$$i \lt j \le n$$$ must also be included in the subsegment.Gildong wants to make all elements of $$$a$$$ 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.
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 $$$a_3$$$ to be $$$0$$$, so that the array becomes $$$[-1,0,0]$$$. Now Gildong can use the $$$2$$$-nd operation once on the suffix starting at $$$a_2$$$, which means $$$a_2$$$ and $$$a_3$$$ are decreased by $$$1$$$, making all elements of the array $$$-1$$$.In the third case, we can set $$$a_1$$$ to $$$96$$$, so that the array becomes $$$[96,96,97,95]$$$. Now Gildong needs to:   Use the $$$2$$$-nd operation on the suffix starting at $$$a_3$$$ once, making the array $$$[96,96,96,94]$$$.  Use the $$$1$$$-st operation on the suffix starting at $$$a_4$$$ $$$2$$$ times, making the array $$$[96,96,96,96]$$$. In the fourth case, we can change the array into $$$[-3,-3,-2,1]$$$. Now Gildong needs to:   Use the $$$2$$$-nd operation on the suffix starting at $$$a_4$$$ $$$3$$$ times, making the array $$$[-3,-3,-2,-2]$$$.  Use the $$$2$$$-nd operation on the suffix starting at $$$a_3$$$ once, making the array $$$[-3,-3,-3,-3]$$$. 
Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$).Each test case contains two lines. The first line of each test case consists of an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements of the array $$$a$$$.The second line of each test case contains $$$n$$$ integers. The $$$i$$$-th integer is $$$a_i$$$ ($$$-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8$$$).It is guaranteed that the sum of $$$n$$$ in all test cases does not exceed $$$2 \cdot 10^5$$$.
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.
def min_operations_optimized_with_corners(arr):
    n = len(arr)
    total_diff = sum((abs(arr[i] - arr[i - 1]) for i in range(1, n)))
    max_reduction = 0
    for i in range(1, n - 1):
        current_diff = abs(arr[i] - arr[i - 1]) + abs(arr[i] - arr[i + 1])
        new_diff = abs(arr[i - 1] - arr[i + 1])
        reduction = current_diff - new_diff
        max_reduction = max(max_reduction, reduction)
    if n > 1:
        max_reduction = max(max_reduction, abs(arr[0] - arr[1]))
        max_reduction = max(max_reduction, abs(arr[n - 1] - arr[n - 2]))
    min_operations = total_diff - max_reduction
    return min_operations
t = int(input())
for _ in range(t):
    n = int(input())
    v = [int(x) for x in input().split()]
    print(min_operations_optimized_with_corners(v))
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(1, n):
        ans += abs(a[i] - a[i - 1])
    maxval = max(abs(a[1] - a[0]), abs(a[n - 2] - a[n - 1]))
    for i in range(1, n - 1):
        maxval = max(maxval, abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]))
    print(ans - maxval)
f = abs
for s in [*open(0)][2::2]:
    a = []
    b = []
    p = 0
    for x in s.split():
        if x != p:
            a += (int(x),)
            b += (0,)
            p = x
        b[-1] += 1
    a = [x - y << 1 for (x, y) in zip(a, a[1:])] or [0]
    print(sum(map(f, a)) - max((k < 2 and f(x) + f(y) - f(x + y) for (x, y, k) in zip([-a[0] // 2] + a, a + [-a[-1] // 2], b))) >> 1)