qyliang/CodeNanoFix huggingface dataset:
Buggy code:
#37 ABC128D
n,k=map(int,input().split())
v=list(map(int,input().split()))

res=[]
for x in range(min(n,k)):
	for a in range(x+1):
		b=x-a
		l=v[:a]
		r=v[n-b:]
		z=[]
		for t in l:
			if t<0:
				z.append(-t)
		for t in r:
			if t<0:
				z.append(-t)
		z.sort()
		z=z[::-1]
		res.append(sum(l)+sum(r)+sum(z[:k-x]))
		#print(l,r,z)
	print(max(res))

Fixed code:
#37 ABC128D
n,k=map(int,input().split())
v=list(map(int,input().split()))

res=[]
for x in range(min(n,k)+1):
	for a in range(x+1):
		b=x-a
		l=v[:a]
		r=v[n-b:]
		z=[]
		for t in l:
			if t<0:
				z.append(-t)
		for t in r:
			if t<0:
				z.append(-t)
		z.sort()
		z=z[::-1]
		res.append(sum(l)+sum(r)+sum(z[:k-x]))
		#print(l,r,z)
print(max(res))
Buggy code:
from collections import deque
N, K = map(int,input().split())
V = list(map(int,input().split()))

V = deque(V)

ans = []
for i in range(min(N,K)):
  for j in range(min(N,K)-i):
    Vc = V.copy()
    cal = deque([])
    for k in range(i):
      cal.append(Vc.popleft())
    for l in range(j):
      cal.append(Vc.pop())
    cal = sorted(cal)
    for m in range(K-i-j):
      if cal[0] < 0:
        cal.popleft()
      else:
        break
    ans.append(sum(cal))

print(max(ans))
    



Fixed code:
from collections import deque
N, K = map(int,input().split())
V = list(map(int,input().split()))

ans = []
for i in range(min(N,K)+1):
  for j in range(min(N,K)+1-i):
    Vc = deque(V.copy())
    cal = deque([0])
    for k in range(i):
      cal.append(Vc.popleft())
    for l in range(j):
      cal.append(Vc.pop())
    cal = deque(sorted(cal))
    for m in range(K-i-j):
      if cal[0] < 0:
        cal.popleft()
      else:
        break
    ans.append(sum(cal))

print(max(ans))
Buggy code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
U = V[::-1]
ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        hold = V[:l] + U[r:]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

Fixed code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
U = V[::-1]
ret = 0

m = min(N, K)
for l in range(m+1):
    for r in range(m+1-l):
        hold = V[:l] + U[:r]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)
Buggy code:
N, K = map(int, input().split())
jewels = [0] + list(map(int, input().split())) + [0]
ans = 0
for get in range(min(N, K)+1):
  put = get
  for left in range(get+1):
    right = get - left
    values = jewels[left:] + jewels[:N+1-right]
    sum_v = sum(values)
    minus_values = values.filter(lambda x: x<0)
    minus_values.sort()
    cnt = 0
    for v in minus_values:
      sum_v -= v
    ans = max(ans, sum_v)
print(ans)
    
  

Fixed code:
N, K = map(int, input().split())
jewels = list(map(int, input().split())) + [0]
ans = 0
for get in range(1, min(N, K)+1):
  put = min(get, K-get)
  for left in range(get+1):
    right = get - left
    values = jewels[:left] + jewels[N-right:]
    sum_v = sum(values)
    minus_values = list(filter(lambda x: x<0, values))
    minus_values.sort()
    cnt = 0
    for v in minus_values:
      if cnt < put:
        sum_v -= v
        cnt += 1
      else:
          break
    ans = max(ans, sum_v)
print(ans)
Buggy code:
import sys
from collections import deque
from copy import copy

N,K = list(map(int, input().split()))
V = deque(list(map(int, input().split())))

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store.sort()
    max_value = sum(store)
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                max_value -= e
            else:
                break
        else:
            break
    return max_value

    
for k in range(K+1):
    for l in range(K+1):
        for r in range(K+1-l):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)


Fixed code:
import sys
from collections import deque
from copy import copy

N,K = list(map(int, input().split()))
V = deque(list(map(int, input().split())))

max_value = 0

def calc_max_value(V, K, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(K-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                store.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(min(K+1,N+1)):
    for l in range(k+1):
        for r in range(k+1-l):
            max_value = max(max_value, calc_max_value(copy(V), K, k, l, r))

print(max_value)
Buggy code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
from collections import deque
v = deque()
for i in V:
    v.append(i)
res=0
for i in range(min(N,K)):
    for j in range(min(N,K)-i):
        cur = list()
        for f in V:
          v.append(f)
        ans=0
        for p in range(i):
            cur.append(v.popleft())
        for q in range(j):
            cur.append(v.pop())
        sorted(cur,reverse= True)
        for n in range(min(K-i-j,len(cur))):
            if cur[n]>0:
                ans +=cur[n]
            else:
                break
        res = max(ans,res)
print(res)

Fixed code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
from collections import deque
res=0
for i in range(min(N+1,K+1)):
    for j in range(min(N+1,K+1)-i):
        cur = list()
        v = deque()
        for f in V:
          v.append(-f)
        ans=0
        for p in range(i):
            cur.append(v.popleft())
        for q in range(j):
            cur.append(v.pop())
        cur=sorted(cur,reverse=True)
        #print(cur)
        for n in range(min(K-i-j,len(cur))):
            if cur[n]>0:
                cur[n]=0
            else:
                break
        res = max(-1*sum(cur),res)
print(res)
Buggy code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0

ans = 0
for i in range(n):
    temp += arr[i]
    s[i] = temp

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,i+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        if k-i > 0:
            up = min(k-i-1,i)
            for l in range(up):
                if a[l] < 0:
                    v -= a[l]
                else:
                    break
        if v > ans:
            ans = v

print(ans)




Fixed code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0
ans = 0

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,upper+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        up = min(k-upper,i)
        for l in range(up):
            if a[l] < 0:
                v -= a[l]
            else:
                break
        if v > ans:
            ans = v

print(ans)
Buggy code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    if i<=n:
        for j in range(i+1):
            tmp=[]
            # print("{0} {1}".format(i,j))
            for l in range(j):
                if l<n:
                    tmp.append(a[l])
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
            print(tmp)
            tmp.sort(reverse=True)
            # print(tmp)
            for m in range(k-i):
                if len(tmp)>0:
                    if tmp[-1]<0:
                        tmp.pop()
            ans=max(ans,sum(tmp))

print(ans)

Fixed code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    if i<=n:
        for j in range(i+1):
            tmp=[]
            # print("{0} {1}".format(i,j))
            for l in range(j):
                if l<n:
                    tmp.append(a[l])
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
            # print(tmp)
            tmp.sort(reverse=True)
            # print(tmp)
            for m in range(k-i):
                if len(tmp)>0:
                    if tmp[-1]<0:
                        tmp.pop()
            ans=max(ans,sum(tmp))

print(ans)
Buggy code:
import bisect
from collections import deque
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0
for pull_num in range(K+1):
  	if pull_num > N:
        continue
    push_num = K - pull_num
    #print('pull_num:{}'.format(pull_num))
    for left_num in range(pull_num+1):
        right_num = pull_num - left_num
        #print('left_num:{} right_num:{}'.format(left_num, right_num))
        left_D = D[:left_num];  right_D = D[N-right_num:]
        #print('left_D:{} right_D:{}'.format(left_D, right_D))
        temochi = left_D + right_D
        temochi.sort()
        #print('temochi:{}'.format(temochi))
        index = bisect.bisect_left(temochi, 0)
        #print('index:{}'.format(index))
        #print('push_num:{}'.format(push_num))
        if push_num >= index:
            ans = max(ans, sum(temochi[index:]))
        else:
            ans = max(ans, sum(temochi[push_num:]))
print(ans)

Fixed code:
import bisect
from collections import deque
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0
for pull_num in range(K+1):
    if pull_num > N:
        continue
    push_num = K - pull_num
    for left_num in range(pull_num+1):
        right_num = pull_num - left_num
        left_D = D[:left_num];  right_D = D[N-right_num:]
        temochi = left_D + right_D
        temochi.sort()
        index = bisect.bisect_left(temochi, 0)
        if push_num >= index:
            ans = max(ans, sum(temochi[index:]))
        else:
            ans = max(ans, sum(temochi[push_num:]))
print(ans)
Buggy code:
def v_and_num(queue):
  output = [(0,0)]
  for i in range(len(queue)+1):
    hand = queue[:i]
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

"""
left = D[:n//2+1]
right = list(reversed(D[n//2+1:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
      """

r = min(k,n)
M = 0
for a in range(r+1):
  for b in range(r+1-a):
    hand = D[:a] + D[-b:] if b!=0 else D[:a]
    print(hand)
    hand = list(sorted(hand))
    i = 0
    for i in range(min(k-a-b+1,a+b)):
      if hand[i] > 0:
        break
    buf = sum(hand[i:])
    if buf > M:
      M = buf
print(M)


Fixed code:
def v_and_num(queue):
  output = [(0,0)]
  for i in range(len(queue)+1):
    hand = queue[:i]
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

"""
left = D[:n//2+1]
right = list(reversed(D[n//2+1:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
      """

r = min(k,n)
M = 0
for a in range(r+1):
  for b in range(r+1-a):
    hand = D[:a] + D[-b:] if b!=0 else D[:a]
    hand = list(sorted(hand))
    i = 0
    for i in range(min(k-a-b+1,a+b)):
      if hand[i] > 0:
        break
    buf = sum(hand[i:])
    if buf > M:
      M = buf
print(M)
Buggy code:
N,K = map(int,input().split())
V = list(map(int,input().split()))

if N < K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while True:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        print(left,right)
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)


Fixed code:
N,K = map(int,input().split())
V = list(map(int,input().split()))

if N < K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while len(V) > i:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)
Buggy code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        if a+b<=k-a-b:
            continue
        if n<=a+b:
            continue
        in_hand=d[:a]
        in_hand+=in_hand[n-b:]
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Fixed code:
# -*- coding: utf-8 -*-
 
n,max_k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for k in range(1,max_k+1):
    for a in range(k+1):
        for b in range(k-a+1):
            if a+b<=k-a-b:
                continue
            if n<=a+b:
                in_hand=d
            else:
                in_hand=d[:a]
                in_hand+=d[n-b:]
            in_hand=sorted(in_hand)
            answer=max(answer,sum(in_hand[k-a-b:]))
print(answer)
Buggy code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,K = na()
que = na()
revque = que[::-1]

ans = 0
for a in range(min(N,K)+1):
    for b in range(min(K-a,N-a)+1):
        sum_q = que[:a] + revque[:b] if b>0 else que[:a]
        r = sum(sum_q)
        sum_q.sort()
        l = 0
        #print(sum_q)
        if sum_q:
            for i in range(min(K-(a+b),len(sum_q))):
                #sprint(i)
                if sum_q[i]>0:
                    l = i
                    break
        r = sum(sum_q[l:])
        if ans < r:
            ans = r
print(ans)

Fixed code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,K = na()
que = na()
revque = que[::-1]

ans = 0
for a in range(min(N,K)+1):
    for b in range(min(K-a,N-a)+1):
        sum_q = que[:a] + revque[:b] if b>0 else que[:a]
        r = sum(sum_q)
        sum_q.sort()
        l = 0
        #print(sum_q)
        if sum_q:
            for i in range(min(K-(a+b),len(sum_q))):
                #print(i)
                if sum_q[i]>0:
                    l = i
                    break
                else:
                    r-=sum_q[i]
        if ans < r:
            ans = r
print(ans)
Buggy code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(1, roopMax):
            for rightCount in range(0, roopMax - leftCount -1):
                leftList = baseList[0: leftCount - 1:]
                rightList = baseList[listCount - 1 - rightCount : listCount - 1]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        handList.sort()
        for each in handList:
            if each < 0 and remainOpeCount > 0:
                removeCount += 1
                remainOpeCount -= 1
        
        maxVal = max(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
print(my.maxNumber)







Fixed code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(0, roopMax):
            self.getMax(baseList[0: leftCount + 1], opeCount - leftCount - 1)

            for rightCount in range(1, roopMax - leftCount + 1):
                leftList = baseList[0: leftCount]
                rightList = baseList[listCount - rightCount : listCount]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount - rightCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        if remainOpeCount > 0:
            handList.sort()
            for each in handList:
                if each < 0:
                    removeCount += 1
                    remainOpeCount -= 1
                    if remainOpeCount < 1:
                        break
        
        maxVal = sum(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
my.roop(baseList, opeCount)
print(my.maxNumber)
Buggy code:
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0

for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()
        val=sum(have)
        
        if a+b==able:
            pass
        else:
            for i in range(min(len(have),able+1-a-b)):
                if have[i] < 0:
                    val-=have[i]
                else:
                    break
        
        if ans<val:
            ans=val

print(ans)

Fixed code:
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0
 
for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()

        for i in range(min(a+b,K-a-b)+1):
            val=sum(have[i:])
            if ans<val:
                ans=val
 
print(ans)
Buggy code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

ans=0

for a in range(50):
  for b in range(50):
    if a+b <= min(n,k):
      li=v[:a]+v[n-b:]
      sumv = sum(li)
      li.sort()
      for i in range(min(k-a-b,len(li))):
        if li[i] < 0:
          li[i] = 0:
        else:
          break:
      ans=max(ans,sum(li))
      
print(ans)
            
      

Fixed code:
n,k = map(int,input().split())
liv = list(map(int,input().split()))
ans = 0

for a in range(50):
    for b in range(50):
        if a + b <= min(n,k):
            li = liv[:a] + liv[n-b:]
            sum1 = sum(li)
            li.sort()
            for i in range(min(k-a-b,len(li))):
                if li[i] < 0:
                    li[i] = 0
                else:
                    break
            ans = max(ans,sum(li))

print(ans)
Buggy code:
import sys
import collections


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    v = collections.deque([int(value) for value in scan().rstrip().split()])
    hands = []

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop(len(v) - 1))

            hands.sort()

            c_d = 0
            for c_d in range(k - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break

            results.append(sum(hands))

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()


Fixed code:
import sys
import collections
import copy


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    values = collections.deque([int(value) for value in scan().rstrip().split()])

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            hands = [0] * n
            v = copy.deepcopy(values)

            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop())

            hands.sort()

            c_d = 0
            for c_d in range(k - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break
            results.append(sum(hands))
            hands = [0] * n

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()
Buggy code:
# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
N, K = map(int, input().split())
D = [int(x) for x in input().split()]
# S = input()

ans = 0
for l in range(N + 1):
    if l > K:
        break

    for r in range(N - l + 1):
        if l + r > K:
            break

        value_list = D[0 : l] + D[N - r : N]
        sort(value_list)
        
        for i in range(K - (l + r) + 1):
            ans = max(ans, sum(value_list[i:]))
        
print(ans)


Fixed code:
# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
N, K = map(int, input().split())
D = [int(x) for x in input().split()]
# S = input()

ans = 0
for l in range(N + 1):
    if l > K:
        break

    for r in range(N - l + 1):
        if l + r > K:
            break

        value_list = D[0 : l] + D[N - r : N]
        value_list.sort()
        
        for i in range(K - (l + r) + 1):
            ans = max(ans, sum(value_list[i:]))
        
print(ans)
Buggy code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
R = min(N,K)
for A in range(R+1):
    for B in range(R-A+1):
        s = V[:A]+V[N-B:]
        print(s)
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
print(ans)


Fixed code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
R = min(N,K)
for A in range(R+1):
    for B in range(R-A+1):
        s = V[:A]+V[N-B:]
        
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
print(ans)