ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 656 ms):
from random import randint
class MyDict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, (1 << 32) - 1) 
        self.default = func
        self.dict = {}
    def _hash(self, key):
        return self.random ^ hash(key)
    def __getitem__(self, key):
        mykey = self._hash(key)
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
    def __setitem__(self, key, value):
        self.dict[self._hash(key)] = value
    def __delitem__(self, key):
        mykey = self._hash(key)
        if mykey in self.dict:
            del self.dict[mykey]
        else:
            raise KeyError(key)
    def get(self, key, default=None):
        return self.dict.get(self._hash(key), default)
    def keys(self):
        return [self._hash(k) for k in self.dict]
    def values(self):
        return self.dict.values()
    def items(self):
        return [(self._hash(k), v) for k, v in self.dict.items()]
    def __contains__(self, key):
        return self._hash(key) in self.dict
    def __iter__(self):
        return iter(self.keys())
    def __len__(self):
        return len(self.dict)
    def __str__(self):
        return str({self._hash(k): v for k, v in self.dict.items()})
    def __repr__(self):
        return f"MyDict({self.default}, {self.dict})"

import heapq
def solve():
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    st = []
    for i in b :
        st.append(-i)

    heapq.heapify(st)

    freq = MyDict()
    for i in a:
        freq[i] += 1

    for i in range(2*n-m):
        if not st:
            print("NO")
            return
        val = - heapq.heappop(st)
        if freq[val] != 0:
            freq[val] -= 1

        else:
            heapq.heappush(st,-(val // 2))
            heapq.heappush(st,-((val+2-1)//2))
    if st :
        print("NO")
    else:
        print("YES")

for __ in range(int(input())):
    solve()

Solution 2 (runtime: 843 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 656 ms):
from random import randint
class MyDict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, (1 << 32) - 1) 
        self.default = func
        self.dict = {}
    def _hash(self, key):
        return self.random ^ hash(key)
    def __getitem__(self, key):
        mykey = self._hash(key)
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
    def __setitem__(self, key, value):
        self.dict[self._hash(key)] = value
    def __delitem__(self, key):
        mykey = self._hash(key)
        if mykey in self.dict:
            del self.dict[mykey]
        else:
            raise KeyError(key)
    def get(self, key, default=None):
        return self.dict.get(self._hash(key), default)
    def keys(self):
        return [self._hash(k) for k in self.dict]
    def values(self):
        return self.dict.values()
    def items(self):
        return [(self._hash(k), v) for k, v in self.dict.items()]
    def __contains__(self, key):
        return self._hash(key) in self.dict
    def __iter__(self):
        return iter(self.keys())
    def __len__(self):
        return len(self.dict)
    def __str__(self):
        return str({self._hash(k): v for k, v in self.dict.items()})
    def __repr__(self):
        return f"MyDict({self.default}, {self.dict})"

import heapq
def solve():
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    st = []
    for i in b :
        st.append(-i)

    heapq.heapify(st)

    freq = MyDict()
    for i in a:
        freq[i] += 1

    for i in range(2*n-m):
        if not st:
            print("NO")
            return
        val = - heapq.heappop(st)
        if freq[val] != 0:
            freq[val] -= 1

        else:
            heapq.heappush(st,-(val // 2))
            heapq.heappush(st,-((val+2-1)//2))
    if st :
        print("NO")
    else:
        print("YES")

for __ in range(int(input())):
    solve()

Solution 2 (runtime: 874 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 921 ms):
import sys
from collections import Counter
from heapq import heapify, heappop, heappush

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")

def cud(n, d):
    if d.get(str(n),0):
        d[str(n)] -= 1
        return True
    if n == 1:
        return False
    return cud(n // 2, d) and cud((n // 2) + (n&1),d)
    

def solve(test_case):
    N,M = multi_int_input()
    A = list_input()
    B = list_input()
    
    b = sorted(B, reverse=True)
    
    d = {}
    for i in A:
        i = str(i)
        d[i] = d.get(i,0) + 1
        
    for i in b:
        if not cud(i, d):
            P1('No')
            return
        
        
        
    #print(d)
    if any(d.values()):
        P1('No')
    else:
        P1('Yes')
        
                


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))

Solution 2 (runtime: 843 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 921 ms):
import sys
from collections import Counter
from heapq import heapify, heappop, heappush

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")

def cud(n, d):
    if d.get(str(n),0):
        d[str(n)] -= 1
        return True
    if n == 1:
        return False
    return cud(n // 2, d) and cud((n // 2) + (n&1),d)
    

def solve(test_case):
    N,M = multi_int_input()
    A = list_input()
    B = list_input()
    
    b = sorted(B, reverse=True)
    
    d = {}
    for i in A:
        i = str(i)
        d[i] = d.get(i,0) + 1
        
    for i in b:
        if not cud(i, d):
            P1('No')
            return
        
        
        
    #print(d)
    if any(d.values()):
        P1('No')
    else:
        P1('Yes')
        
                


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))

Solution 2 (runtime: 874 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")
Problem: 2061D - Kevin and Numbers
Language: python

Solution 1 (runtime: 843 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")

Solution 2 (runtime: 874 ms):
import sys

input = sys.stdin.readline

from collections import Counter



from random import getrandbits



RANDOM = getrandbits(32)



class Int(int):

    def __init__(self, x):

        int.__init__(x)

    def __hash__(self):

        return super(Int, self).__hash__() ^ RANDOM



def find(x):

    if cnt[Int(x)]:

        cnt[Int(x)] -= 1

        return True

    if x == 0 or x == 1: return False

    

    return find((x + 1) // 2) and find(x // 2)



def solve():

    global cnt

    n, m = map(int, input().split())

    a = list(map(Int, input().split()))

    b = list(map(int, input().split()))

    b.sort(reverse=True)



    if sum(a) != sum(b):

        return False

    

    cnt = Counter(a)

    for i in range(m):

        if cnt[Int(b[i])]:

            cnt[Int(b[i])] -= 1

            continue

        if not find(b[i]):

            return False



    return max(cnt.values()) == 0



for _ in range(int(input())):

    print("Yes" if solve() else "No")
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


Solution 2 (runtime: 202 ms):
def solve():
    import sys, math
    data = sys.stdin.read().strip().split()
    if not data:
        return
    t = int(data[0])
    index = 1
    out_lines = []
    for _ in range(t):
        if index >= len(data):
            break
        n = int(data[index])
        index += 1
        # Read the sequence (we use 0-indexing)
        arr = list(map(int, data[index:index+n]))
        index += n
        # If the sequence length is 1, answer is the only element.
        if n == 1:
            out_lines.append(str(arr[0]))
            continue
        # Candidate 0: no operations, i.e. the sum of all elements.
        cand0 = sum(arr)
        best = cand0
        # For r = 1 to n-1, consider doing r difference operations.
        # In 0-indexed notation:
        #   L(r) = sum_{j=0}^{r-1} (-1)^(r-1-j) * binom(r-1, j) * arr[j]
        #   R(r) = sum_{j=0}^{r-1} (-1)^(r-1-j) * binom(r-1, j) * arr[n-r+j]
        # and candidate = |R(r)-L(r)|
        for r in range(1, n):
            L_val = 0
            R_val = 0
            for j in range(r):
                sign = 1 if ((r-1-j) % 2 == 0) else -1
                comb = math.comb(r-1, j)
                L_val += sign * comb * arr[j]
                R_val += sign * comb * arr[n - r + j]
            diff = R_val - L_val
            candidate = diff if diff >= 0 else -diff
            if candidate > best:
                best = candidate
        out_lines.append(str(best))
    sys.stdout.write("\n".join(out_lines))

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

Solution 1 (runtime: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


Solution 2 (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)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


Solution 2 (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)
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 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])
	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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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: 109 ms):
n = int(input())

for _ in range(n):
    num = int(input())
    arr = list(map(int, input().split()))
    ans = sum(arr)
    for j in range(num - 1):
        for i in range(num - j - 1):
            arr[i] = arr[i + 1] - arr[i]
        ans = max(ans, abs(sum(arr[:num - j - 1])))
    print(ans)


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)