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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        

    

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: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1171 ms):
def solution():
    n = int(input())
    u_v = [list(map(lambda x: int(x)-1, input().split())) for _ in range(n-1)]
    adj = [set() for _ in range(n)]
    for u, v in u_v:
        adj[u].add(v)
        adj[v].add(u)
    _, _, i = max((len(adj[u]), sum(int(len(adj[u]) > len(adj[v])) for v in adj[u]), u) for u in range(n))
    for v in adj[i]:
        adj[v].remove(i)
    j = max((j for j in range(n) if j != i), key=lambda x: len(adj[x]))
    return len(adj[i])+len(adj[j])-1

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

Solution 2 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()