ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 125 ms):
def reverse_list(lst):
    """Returns a reversed copy of the given list."""
    return lst[::-1]

def diffSequence(n, lst:list):
    lstDup = lst.copy()
    if n == 1:
        return lstDup
    else :
        for i in range(n-1,0,-1):
            lstDup[i] = lstDup[i]-lstDup[i-1]
        return lstDup[1:]

def sumList(n, lst):
    s = 0
    for i in range(n):
        s += lst[i]
    return s


def maxSum(n, lst):
    lesSommes = [sumList(n, lst)]
    while n > 1 :
        s1 = sumList(n, lst)
        s2 = sumList(n-1, diffSequence(n, lst))
        s3 = sumList(n-1, diffSequence(n, reverse_list(lst)))
        mx = max(s1,s2,s3)
        if s2 == mx:
            lst = diffSequence(n, lst)
            n-=1
            lesSommes.append(s2)
        elif s3 == mx:
            lst = diffSequence(n, reverse_list(lst))
            n-=1
            lesSommes.append(s3)
        else :
            lst = diffSequence(n, lst)
            n -= 1
            lesSommes.append(s1)
    return max(lesSommes)


# print(reverse_list(lst))
# print(diffSequence(2, reverse_list(lst)))
# print(sumList(1, diffSequence(2, reverse_list(lst))))

sols = []

t = int(input())
for i in range(t):
    n = int(input())
    elts = input()
    lst = [int(elts.split()[i]) for i in range(n)]
    sols.append(maxSum(n, lst))

for i in range(t):
    print(sols[i])

# lst = [9, 7, 9, -9, 9, -8, 7, -8, 9]
# print(maxSum(9, lst))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 109 ms):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
t = int(input())  # Number of test cases

for _ in range(t):
    n = int(input())  # Length of the sequence
    v = list(map(int, input().split()))  # The sequence

    max_sum = sum(v)  # Track the maximum sum encountered

    while len(v) > 1:
        # Try without reversing
        diff_v = [v[i] - v[i - 1] for i in range(1, len(v))]
        max_sum = max(max_sum, sum(diff_v))  # Update max sum

        # Try with reversing
        v.reverse()
        rev_diff_v = [v[i] - v[i - 1] for i in range(1, len(v))]
        max_sum = max(max_sum, sum(rev_diff_v))  # Update max sum

        # Pick the better option
        if sum(diff_v) > sum(rev_diff_v):
            v = diff_v
        else:
            v = rev_diff_v

    print(max_sum)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 234 ms):
'''
Observations:

- After a reversal, you must replace the sequence with its difference sequence as otherwise nothing has been done to the sum.
- The different sequence replacement reduces the length of the sequence by 1 so we can only do it a fixed number of times.

Therefore, if we brute-force:
Options are:
1. Nothing (current sum)
2. Reverse + difference
3. Difference
'''

from math import inf
from functools import lru_cache

def get_difference_sequence(path):
    return [path[i] - path[i - 1] for i in range(1, len(path))]

@lru_cache(None)
def backtrack(length, path):
    if length == 0:
        return -inf
    
    # Convert tuple back to list for processing
    path = list(path)
    
    # Reverse + Difference
    reverse_path = path[::-1]
    diff_path = get_difference_sequence(reverse_path)
    curr = sum(diff_path)
    ans = max(curr, backtrack(length - 1, tuple(diff_path)))
    
    # Difference only
    diff_path = get_difference_sequence(path)
    curr = sum(diff_path)
    
    return max(ans, curr, backtrack(length - 1, tuple(diff_path)))

t = int(input())

while t > 0:
    n = int(input())
    a = tuple(map(int, input().split()))
    s = sum(a)
    
    if n == 1:
        print(s)
    else:
        print(max(s, backtrack(n, a)))
    
    t -= 1
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 93 ms):
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))


    def reduction(n, l):
        new_l = []
        summ=0
        for i in range(n - 1):
            new_l.append(l[i + 1] - l[i])
            summ += l[i+1]-l[i]
        return n - 1, new_l, summ


    def two(l):
        if l[0] * l[1] >= 0:
            return abs(l[0] + l[1])
        else:
            return abs(l[0] - l[1])

    if n==1:
        print(l[0])

    elif n==2:
        if l[0] >= 0 and l[1] >= 0:
            print(l[0] + l[1])
        else:
            print(abs(l[0] - l[1]))


    else:
        count =sum(l)
        while n >2:
            n, l, summ = reduction(n,l)
            count = max(count, abs(summ))
            
        print(max(two(l),count))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 78 ms):
import sys

# Fast Input
def inp(): return sys.stdin.readline().strip()
def inplist(): return list(map(int, inp().split()))

# Debugging function
def debug(*args):
    print("DEBUG:", *args, file=sys.stderr)

def solve():
    n = int(input())
    a = list(map(int, input().split()))

    if n == 1:
        print(a[0])
        return
    ans = sum(a)
    while n > 1:
        n -= 1
        a = a[::-1]
        a = [a[i+1] - a[i] for i in range(n)]
        ans = max(ans, abs(sum(a)))
    print(ans)

def main():
    t = 1
    t = int(inp())  # Uncomment if multiple test cases exist
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 108 ms):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = sum(a)
    while len(a) > 1:
        ans = max(ans, abs(a[0] - a[-1]))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 108 ms):
t=int(input())
while t:
	t-=1
	n = int(input())
	a = list(map(int, input().split()))

	total = sum(a)
	while n > 1:
		left = sum(a[:n-1])-sum(a[1:n])
		right = sum(a[1:n])-sum(a[:n-1])

		total = max(total, left, right)
		if left >= right:
			a.reverse()

		for i in range(0, n-1):
			a[i] = a[i+1]-a[i]
		
		n-=1
		a = a[0:n]
		
	print(max(total, a[0]))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 171 ms):
import sys
data = sys.stdin.read().splitlines()

line = 2
while (line < len(data)):
    arr = list(map(int, data[line].split()))
    new_arr = arr
    diff = sum(arr)
    # print(arr, "...diff=", diff)
    if len(arr) == 2:
        diff = max(abs(arr[0]-arr[1]), diff)
    elif len(arr) > 2:
        for i in range(len(arr)-2):
            new_arr = [new_arr[i+1]-new_arr[i] for i in range(len(new_arr)-1)]
            diff = max(abs(new_arr[0]-new_arr[-1]), diff, abs(sum(new_arr)))
            # print(new_arr, "...diff=", diff)
    print(diff)
    line += 2
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 124 ms):
from collections import Counter

t = int(input())


def solve():
    n = int(input())
    a = list(map(int, input().split()))
    if n == 1:
        print(a[0])
        return
    ans = sum(a)
    while len(a) > 1:
        b = [0] * (len(a) - 1)
        for i in range(len(a) - 1, 0, -1):
            b[i - 1] = a[i] - a[i - 1]
        a = b.copy()
        ans = max(ans, abs(sum(a)))
    print(ans)


for _ in range(t):
    solve()
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 93 ms):
import sys

inp = sys.stdin.readline

def I(): return int(inp().strip())
def IS(): return inp().strip()
def II(): return map(int, inp().split())
def IL(): return list(map(int, inp().split()))
def SIL(): return sorted(map(int, inp().split()))
from collections import defaultdict, Counter, deque
from array import array
from math import gcd, isqrt



def solve():
    n = I()
    arr = IL()

    neg = False
    pos = False

    if n == 1:
        print((arr[0]))
        return

    for i in range(n):
        if arr[i] < 0:
            neg = True
        
        if arr[i] > 0:
            pos = True
    
    res1 = sum(arr)
    res2 = float("-inf")
    
    while len(arr)!=1:
        new = []
        for i in range(1, len(arr)):
            new.append(arr[i]-arr[i-1])
        res2 = max(res2, abs(sum(new)))
        arr = new.copy()

    print (max(res1, res2))
    
 


T = I()
for _ in range(T):
    solve()
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 93 ms):
m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)
        

Solution 2 (runtime: 77 ms):
for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    if n==1:
        print(a[0])
        continue
    
    sm = sum(a)
    while(n>1):
        n -=1
        a = [a[i+1]-a[i] for i in range(n)]
        sm = max(sm,abs(sum(a)))
    print(sm)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 281 ms):
import sys
from collections import defaultdict,deque,Counter
from heapq import heappush,heappop,heapify
from math import inf,comb,ceil,log,lcm,gcd,isqrt
from functools import cache
from itertools import accumulate,combinations,permutations
from bisect import bisect_left,bisect_right,insort_left
from random import randint

input=lambda:sys.stdin.readline().rstrip()
inp=lambda:list(map(int,input().split()))
I=lambda:int(input())
fact=[cur:=1]+[cur:=(cur*i)for i in range(1,100)]
def comb(n,k):
    return fact[n]//(fact[n-k]*fact[k])
def f(n,b):
    o=0
    for i in range(n):
        if b:
            if i%2:
                o-=comb(n-1,i)*a[i]
            else:
                o+=comb(n-1,i)*a[i]
        else:
            if i%2:
                o-=comb(n-1,i)*a[~i]
            else:
                o+=comb(n-1,i)*a[~i]

    return o
"""def bf(ox):
    
    n=len(ox)
    o=sum(ox)

    for i in range(1<<(n-1)):
        x=ox.copy()
        for v in range(n-1):
            if (1<<v)&i:
                x=[x[ii]-x[ii+1]for ii in range(len(x)-1)]
            else:
                x=[x[ii+1]-x[ii]for ii in range(len(x)-1)]
            o=max(o,sum(x))
    return o"""

t=I()
for _ in range(t):
    n=I()
    a=inp()
    #n=randint(1,5)
    #a=[randint(-1000,1000)for _ in range(n)]
    o=sum(a)
    for i in range(n-1):
        c1=f(i+1,1)
        c2=f(i+1,0)
        if i==0:
            o=max(o,abs(a[-1]-a[0]))
        else:
            o=max(abs(c1)+abs(c2),o)
    #print(sum(abs(e)for e in a))
    o=max(o,f(n,0),f(n,1))
    print(o)
    #assert o==bf(a),(bf(a),o,a)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 93 ms):
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    not_op = sum(a)
    while(len(a)>2):
        t=[]
        for i in range(1,len(a)):
            t.append(a[i]-a[i-1])
        a=t
        not_op=max(not_op,sum(a))
        neg_a = map(lambda x:-x,a)
        not_op=max(not_op,sum(neg_a))
    if len(a)==1:
        print(a[0])
    else:
        res=max(a[0]-a[1],a[1]-a[0])
        print(max(res,not_op))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 125 ms):
tc = int(input())

for _ in range(tc):
    n = int(input())
    a = list(map(int, input().split()))
    res = sum(a)
    if n == 1:
        print(a[0])
    else:
        while len(a) > 2:
            b = []
            for i in range(len(a) - 1):
                b.append(a[i] - a[i + 1])
            res = max(res, sum(b))
            a = a[::-1]
            b = []
            for i in range(len(a) - 1):
                b.append(a[i] - a[i + 1])
            res = max(res, sum(b))
            a = b
            # print(_, a, len(a))
        res = max(a[1] - a[0], a[0] - a[1], a[0] + a[1], res)
        print(res)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 77 ms):






for _ in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	if n==1 :
		print(a[0])
	else:
		m=sum(a)
		for i in range(n):
			s=0
			for j in range(n-i-1):
				s+=(-a[j]+a[j+1])
				a[j]=-a[j]+a[j+1]
			m=max(m,s,-s)
		print(m)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 156 ms):
# cook your dish here
def get(a):
    s=0
    l=[]
    for i in range(1,len(a)):
        l.append(a[i]-a[i-1])
        
    return l
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    ans=sum(l)
    while(len(l)!=1):
        t1=get(l)
        t2=get(l[::-1])
        if sum(t1)>=sum(t2):
            l=t1 
        else:
            l=t2 
        ans=max(ans,sum(l))
    print(max(ans,l[0]))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 140 ms):

def reduce_list(arr):
    new_arr = [0] * (len(arr) - 1)

    for idx in range(1, len(arr)):
        new_arr[idx-1] = arr[idx] - arr[idx-1]

    return new_arr

def sum(arr):
    sum_v = 0
    for v in arr:
        sum_v += v
    return sum_v


def min_op(arr):
    if len(arr) == 1:
        return sum(arr)

    max_v = sum(arr)

    rev_arr = arr.copy()
    rev_arr.reverse()

    arr = reduce_list(arr)
    rev_arr = reduce_list(rev_arr)

    sum_reduce = sum(arr)
    sum_rev_reduce = sum(rev_arr)

    if sum_reduce > sum_rev_reduce:
        max_v = max(max_v, min_op(arr))
    else:
        max_v = max(max_v, min_op(rev_arr))

    return max_v

t = int(input())

for x in range(0, t):
    s = int(input())
    v = list(map(int, input().split(" ")))

    m = min_op(v)
    print(m)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 109 ms):
for _ in range(int(input())):

    n = int(input())
    arr = list(map(int, input().split()))

    ans = sum(arr)

    while len(arr) > 1:
        arr = [arr[i+1]-arr[i] for i in range(len(arr)-1)]
        ans = max(ans, abs(sum(arr)))
    
    print(ans)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 109 ms):
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    s=a
    d=sum(a)
    while len(s)>=1:
        c=[]
        for i in range(len(s)-1):
            c.append(s[i+1]-s[i])
        d=max(d,abs(sum(c)))
        s=c
    if n==1:
        d=sum(a)    
    print(d)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 124 ms):
from itertools import pairwise

numTestCases = int(input())
while numTestCases > 0:
    numTestCases -= 1
    int(input())
    a = [int(s) for s in input().split()]
    res = sum(a)
    while len(a) > 1:
        if a[-1] < a[0]:
            a = list(reversed(a))
        res = max(res, a[-1] - a[0])
        a = [y-x for x,y in pairwise(a)]
    print(res)


Solution 2 (runtime: 109 ms):
def input_to_list():
    return list(map(int, input().split()))


def function(length,lists):
    lists_rot = lists[::-1]
    ans,temp_ans = 0,0
    for x in lists:
        ans += x
    lists_add = [0] * (length)
    lists_add[0] = 1
    n_sign = 1
    for maxmun in range(length - 1):
        n_sign = n_sign * -1
        add = 0
        temp_ans = 0
        for index in range(maxmun + 1):
            x = lists_add[index]
            lists_add[index] += - add
            temp_ans += lists_add[index] * (lists[index] + lists_rot[index] * n_sign)
            add = x
        ans = max(ans,temp_ans,-temp_ans)

    return ans

times = int(input())
for x in range(times):
    ans = 0
    length = int(input())
    lists = input_to_list()
    print( function(length,lists) )