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

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

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

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

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

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

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

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

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


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


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


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: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 140 ms):
def get_num_cases():
    return int(input())


def get_line():
    return [int(p) for p in input().strip().split()]



from itertools import accumulate
import sys


class ArraySum:

    def __init__(self, arr):
        self.sums = []
        s = 0
        for n in arr:
            s += n
            self.sums.append(s)

    def get(self, i, j):  # 0 based
        if i > j:
            return 0
        if i == 0:
            return self.sums[j]
        return self.sums[j] - self.sums[i-1]


    def get_min(self, i, j, n):  # 0 based
        if n == 0:
            return 0
        if n > (j - i + 1):
            return float('inf')
        return min(
            self.get(p, p + n - 1)
            for p in range(i, j - n + 2)
        )

    def __len__(self):
        return len(self.sums)


for _ in range(get_num_cases()):
    n, l, r = get_line()
    l -= 1
    arr = get_line()
    brr=arr[:l]+sorted(arr[l:])
    crr=sorted(arr[:r])[::-1]+arr[r:]
    print(min(sum(brr[l:r]),sum(crr[l:r])))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 155 ms):
t = int(input())
for _ in range(t):
    n, l, r = map(int, input().split())
    a = list(map(int, input().split()))
    s = sorted(a[l - 1:r], reverse=True)
    before = sorted(a[:l - 1])
    after = sorted(a[r:n])
    ans1 = 0
    ans2 = 0
    for i in range(len(s)):
        if len(before) > i:
            ans1 += min(before[i], s[i])
        else:
            ans1 += s[i]
        if len(after) > i:
            ans2 += min(after[i], s[i])
        else:
            ans2 += s[i]
    print(min(ans1, ans2))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 140 ms):
import sys
lines = sys.stdin.read().splitlines()
t = int(lines[0])

for i in range(1, t + 1):
    n,l,r = list(map(int,lines[2 * i - 1].split()))
    nums = list(map(int,lines[2 * i].split()))
    l -= 1
    r -= 1
    arr1 = nums[:r + 1]
    arr1.sort()
    summ1 = 0
    for j in range(r - l + 1):
        summ1 += arr1[j]
    arr2 = nums[l:]
    arr2.sort()
    summ2 = 0
    for j in range(r - l + 1):
        summ2 += arr2[j]
    print(min(summ1,summ2))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 296 ms):
import sys
import math
import collections
import statistics
import bisect

############ ---- Input Functions ---- ############
##---1) inp — For taking integer inputs.
def inp():
    return (int(input()))

##2) inlt — For taking List inputs.
def inlt():
    return (list(map(int, input().split())))

#3) insr — For taking string inputs. Actually it returns a List of Characters,
# instead of a string, which is easier to use in Python, because in Python,
# Strings are Immutable.
def insr():
    s = input()
    return (list(s[:len(s) - 1]))

#4) invr — For taking space seperated integer variable inputs.
def invr():
    return list(map(int, input().split()))

# freq = collections.defaultdict(int)
# print(''.join(n))
MAX = int(2e5) + 7


def solve():
    n ,l ,r = inlt()
    a = inlt()
    # a.sort()
    sumf = 0
    sums = 0
    diff = r - l + 1
    firstcase = a[:r]
    secondcase = a[l-1:]
    firstcase.sort()
    secondcase.sort()
    for i in range(diff):
        sumf += firstcase[i]
        sums += secondcase[i]
    print(min(sumf, sums))

t = int(input())
for _ in range(t):
    solve()
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 281 ms):
from collections import Counter
import sys
 
# sys.set_int_max_str_digits(200000)
import heapq
import math
from collections import defaultdict
# import sys
# sys.setrecursionlimit(100000)
mod=10**9+7
t = int(input())
for _ in range(t):
    n,l,r=map(int,input().split())
    arr=list(map(int,input().split()))
    brr=arr[:l-1]+sorted(arr[l-1:])
    crr=sorted(arr[:r])[::-1]+arr[r:]
    print(min(sum(brr[l-1:r]),sum(crr[l-1:r])))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 156 ms):
import sys

input=lambda:sys.stdin.readline().rstrip()

 

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

    n,l,r=map(int,input().split());l-=1

    arr=[*map(int,input().split())]

    brr=arr[:l]+sorted(arr[l:])

    crr=sorted(arr[:r])[::-1]+arr[r:]

    print(min(sum(brr[l:r]),sum(crr[l:r])))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 156 ms):
test = int(input())
for _ in range(test):
    n, l, r = map(int, input().split())
    a = list(map(int, input().split()))
   
    left_sorted = sorted(a[:r])
    right_sorted = sorted(a[l-1:])
    sum_1 = sum(left_sorted[:r-l+1])
    sum_2 = sum(right_sorted[:r-l+1])
    print(min(sum_1, sum_2))
    # ans = 0
    # for i in range(r-l+1):
    #     ans += a[i]
    # print(ans)
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 155 ms):
t=int(input())

for i in range(t):
    n,l,r=list(map(int,input().split()))
    l-=1
    a=list(map(int,input().split()))
    b=a[:l]+sorted(a[l:])
    c=sorted(a[:r])[::-1]+a[r:]
    print(min(sum(c[l:r]),sum(b[l:r])))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 202 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

















def solve():

    n, l, r = II()

    arr = IL()



    left = (arr[:l-1])

    right =(arr[r:])

    segment = (arr[l-1:r])

    



    



    ans1 = sorted(left + segment)[:r-l+1]

    ans2 = sorted(right + segment)[:r-l+1]





    if sum(ans1) < sum(ans2):

        print(sum(ans1))

    else:

        print(sum(ans2))



   

   

T = I()

for _ in range(T):

    solve()
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 156 ms):
from math import sqrt


def C(n, m):
    ans = 1
    for i in range(1, m + 1):
        ans = ans * (n - m + i) / i
    return ans
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
def main():
    n, l, r = map(int, input().split())
    #n = int(input())
    a = [int(i) for i in input().split()]
    b = sorted(a[0:r])
    mi = 1e18
    ans = 0
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    ans = 0
    b = sorted(a[l-1:n])
    for i in range(r-l+1):
        ans += b[i]
    mi = min(mi, ans)
    print(mi)

t = 1
t = int(input())
for i in range(t):
    main()


Solution 2 (runtime: 436 ms):
T = int(input())
answer = []

for _ in range(T):
    n, l, r = [int(num) for num in input().split()]
    arr = [int(num) for num in input().split()]

    left = arr[:l-1]
    right = arr[r:]
    mid = arr[l-1:r]

    left.sort()
    right.sort()
    mid.sort()

    p1 = 0
    p2 = len(mid)-1
    total = 0
    while p1<len(left) and p2>=0:
        profit = left[p1] - mid[p2]
        if profit>=0:
            break
        total += profit
        p1 += 1
        p2 -= 1
    
    delta1 = total

    p1 = len(mid)-1
    p2 = 0
    total = 0
    while p2<len(right) and p1>=0:
        profit = right[p2] - mid[p1]
        if profit>=0:
            break
        total += profit
        p1 -= 1
        p2 += 1
    
    delta2 = total


    answer.append(sum(mid)+min(delta1, delta2))


for el in answer:
    print(el)