ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 375 ms):
inp = lambda: int(input())
minp = lambda: map(int, input().split())
linp = lambda: list(map(int, input().split()))
tinp = lambda: tuple(map(int, input().split()))
import math

def solve():
    n, l, r = minp()
    left = []
    mid = []
    right = []
    for i, v in enumerate(minp()):
        if i < l-1:
            left.append(v)
        elif i < r:
            mid.append(v)
        else:
            right.append(v)
    left.sort()
    mid.sort()
    right.sort()
    
    ans1 = ans2 = sum(mid)
    a = 0
    b = len(mid)-1
    while a < len(left) and b >= 0 and left[a] < mid[b]:
        ans1 -= mid[b]-left[a]
        a+=1; b-=1
    a = 0
    b = len(mid)-1
    while a < len(right) and b >= 0 and right[a] < mid[b]:
        ans2 -= mid[b]-right[a]
        a+=1; b-=1
    
    print(min(ans1, ans2))

    
        

for _ in range(inp()):
    solve()
Problem: 2063B - Subsequence Update
Language: python

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





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

for case in range(t):

    s=0

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

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

    a=seq[:l-1]+sorted(seq[l-1:])

    b=sorted(seq[:r])[::-1]+seq[r:]

    print(min(sum(a[l-1:r]),sum(b[l-1:r])))
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 327 ms):
############ ---- Functions for inputs ---- ############
import math
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter

def number(): return (int(input()))
def numbers(): return (map(int, input().split()))
def array(): return (list(map(int, input().split())))
def string(): return input()
############ ---- Functions for inputs ---- ############

t = number()

for _ in range(t):
  n, l, r = numbers()
  nums = array()
  left = nums[:l - 1]
  right = nums[r:]
  middle = nums[l - 1:r]
  option1 = sorted(left + middle)
  option2 = sorted(right + middle)
  print(min(sum(option1[:r - l + 1]), sum(option2[:r - l + 1])))
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 140 ms):
for i in range(int(input())):

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

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

    l-=1

    r-=1

    l1 = a[l:]

    l2 = a[:r+1]

    l1.sort()

    l2.sort()

    print(min(sum(l1[:r-l+1]),sum(l2[:r-l+1])))
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 390 ms):
for _ in range(int(input())):
    n, l, r = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    
    lst1 = sorted(a[0:l - 1])
    lst2 = sorted(a[l - 1:r], reverse = True)
    if r < n:
        lst3 = sorted(a[r:])
    else:
        lst3 = []
    
    res = sum(lst2)
    for i in range(min(len(lst1), len(lst2))):
        if lst1[i] < lst2[i]:
            res += (lst1[i] - lst2[i])
        else:
            break
    
    res1 = sum(lst2)
    for i in range(min(len(lst3), len(lst2))):
        if lst3[i] < lst2[i]:
            res1 += (lst3[i] - lst2[i])
        else:
            break
    
    print(min(res, res1))
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 358 ms):
from collections import defaultdict, Counter
from random import randint
def intlist(): return list(map(int, (x for x in input().split())))


def check(mid, portion):
    mid = sorted(mid)[::-1]
    portion.sort()
    su = 0
    len_mid = len(mid)
    len_portion = len(portion)
    i, j = 0, 0
    while j < len_mid and i < len_portion:
        if mid[j] <= portion[i]:
            break 
        su += portion[i]
        i += 1
        j += 1
    while j < len_mid:
        su += mid[j]
        j += 1
    return su


for _ in range(int(input())):
    n, l, r = intlist()
    nums = intlist()
    first = nums[:l-1]
    mid = nums[l-1:r]
    last = nums[r:]
    print(min(check(mid, first), check(mid, last)))
Problem: 2063B - Subsequence Update
Language: python

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





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

for _ in range(t):

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

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

    l=l-1

    r=r-1

    l1=a[:r+1]

    l2=a[l:]

    l1.sort()

    l2.sort()

    print(min(sum(l1[:r-l+1]),sum(l2[:r-l+1])))
Problem: 2063B - Subsequence Update
Language: python

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





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

 

for i in range(t):

    [n, l, r] = [int(val) for val in input().split()]

    nums = [int(val) for val in input().split()]

    nums_one = [int(val) for val in nums[:r]]

    nums_two = [int(val) for val in nums[l-1:]]

    nums_one.sort()

    nums_two.sort()

    print(min(sum(nums_one[:r-l+1]), sum(nums_two[:r-l+1])))

    # ret = 0

    # for j in range(r-l+1):

    #     ret_one += nums[j]

    # print(ret)
Problem: 2063B - Subsequence Update
Language: python

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





Solution 2 (runtime: 343 ms):
import math

from collections import deque, defaultdict, Counter

import heapq



def solution():

  n, l ,r = list(map(int, input().split(" ")))

  l -= 1

  r -= 1

  a = list(map(int, input().split(" ")))

  segment = a[l:r+1]

  res = sum(segment)

  left, right = a[:l], a[r+1:]

  segment.sort()

  left.sort()

  right.sort()

  i, j = 0, len(segment) - 1

  while i < len(left) and j >= 0:

    if left[i] < segment[j]:

      segment[j] = left[i]

      i += 1

      j -= 1

    else:

      break

  res = min(res, sum(segment))

  segment = a[l:r+1]

  segment.sort()

  i, j = 0, len(segment) - 1

  while i < (len(right)) and j >= 0:

    if right[i] < segment[j]:

      segment[j] = right[i]

      i += 1

      j -= 1

    else:

      break

  res = min(res, sum(segment))

  return res





t = int(input())

for i in range(t):

  print(solution())
Problem: 2063B - Subsequence Update
Language: python

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





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

for _ in range(t):

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

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

   



    l-=1

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





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

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

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

    arra1=arra[:r]

    arra2=arra[l-1:]

    sum1=0

    sum2=0

    arra1.sort()

    arra2.sort()

    for i in range(r-l+1):

        sum1+=arra1[i]

    for i in range(r-l+1):

        sum2+=arra2[i]

    print(min(sum1,sum2))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 171 ms):
t=int(input())
for _ in range(t):
    n,l,r=map(int,input().split())
    if n==1:
        s=input()
        print(int(s))
        continue
    lis=[int(i) for i in input().split()]
    x=lis[:l-1]
    y=lis[r:]
    if len(x)>0:
        x.sort()
    if len(y)>0:
        y.sort()
    o=lis[l-1:r]
    o.sort()
    low=0
    high=len(o)-1
    repsum=0
    while low<l-1 and high>=0:
        if x[low]<o[high]:
            repsum+=o[high]-x[low]
            low+=1
            high-=1
        else:
            break
    low=0
    high=len(o)-1
    rep2=0
    while high>=0 and low<len(y):
        if y[low]<o[high]:
            rep2+=o[high]-y[low]
            low+=1
            high-=1
        else:
            break
    u=max(rep2,repsum)
    print(sum(o)-u)
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 187 ms):
t = int(input())
for _ in range(t):
    cont = 0
    n, l, r = list(map(int, input().split()))
    lis = list(map(int, input().split()))
    lis1 = []
    lis2 = []
    min1 = 0
    min2 = 0
    for i in range(r):
        lis1.append(lis[i])
    for i in range(l - 1,n):
        lis2.append(lis[i])
    lis1 = sorted(lis1)
    lis2 = sorted(lis2)
    for i in range(r - l + 1):
        min1 += lis1[i]
    for i in range(r - l + 1):
        min2 += lis2[i]
    print(min(min1,min2))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 156 ms):
def summa(x,y):
    summma = 0
    count = r + 1 - l
    i = 0
    j = 0
    x.sort()
    y.sort()
    while count > 0:
        if x[i] >= y[j]:
            summma += y[j]
            j += 1
            count -= 1

        else:
            summma += x[i]
            i += 1
            count -= 1

    return(summma)

N=10**9
for _ in range(int(input())):
    n,l,r =map(int,input().split())
    numbers = list(map(int,input().split()))
    left = numbers[ : l-1]+[N]
    right = numbers[r: ]+[N]
    middle = numbers[l-1:r]

    uno_ = summa(left,middle)
    dos_ = summa(right,middle)

    print(min(uno_,dos_))
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 140 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: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 249 ms):
t = int(input())
for T in range(0,t):
    st = input()
    S = st.split()
    n = int(S[0])
    l = int(S[1])
    r = int(S[2])
    num = r - l + 1
    
    An = input()
    A = An.split()
    B = []
    C = []
    for i in range(0,r):
        B.append(int(A[i]))
    for i in range(l-1,n):
        C.append(int(A[i]))
    BB = sorted(B)
    CC = sorted(C)

    sum1 = 0
    sum2 = 0
    for i in range(0,num):
        sum1 += int(BB[i])
        sum2 += int(CC[i])
    
    if(sum1 > sum2):
        print(sum2)
    else:
        print(sum1)
Problem: 2063B - Subsequence Update
Language: python

Solution 1 (runtime: 343 ms):
from sys import stdin
input = stdin.buffer.readline

for _ in range(int(input())):
    n, l, r = map(int, input().split())
    *a, = map(int, input().split())
    
    seg = sorted(a[l - 1:r])
    pref = sorted(a[:l - 1])
    suf = sorted(a[r:])
    
    s = sum(seg)
    
    s1 = s
    k = 0
    while k < min(len(seg), len(pref)) and seg[-k - 1] > pref[k]:
        s1 += pref[k] - seg[-k - 1]
        k += 1
    
    s2 = s
    k = 0
    while k < min(len(seg), len(suf)) and seg[-k - 1] > suf[k]:
        s2 += suf[k] - seg[-k - 1]
        k += 1
    
    print(min(s1, s2))


Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
    n, l, r = map(int, input().split())
    arr = list(map(int, input().split()))
    left = sorted(arr[:r])
    right = sorted(arr[l-1:])
    print(min(sum(left[:r-l+1]), sum(right[:r-l+1])))