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

Solution 1 (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()


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

Solution 1 (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()


Solution 2 (runtime: 108 ms):
t = int(input())
for _ in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
    if n == 1:
        print(nums[0])
    else:
        num_sum = sum(nums)
        max_diff = 0
        diff_num = nums
        while len(diff_num) > 1:
            diff = 0
            for i in range(1, len(diff_num)):
                new_a = diff_num[i] - diff_num[i-1]
                diff += new_a
                diff_num[i-1] = new_a
            diff_num.pop(-1)
            max_diff = max(max_diff, abs(diff))

        print(max(num_sum, max_diff))
Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (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()


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: 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()


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: 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()


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: 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()


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: 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()


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: 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()


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: 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()


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: 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()


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