qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
input = sys.stdin.readline
N,K = map(int,input().split())
V = list(map(int,input().split()))

D = []
ans = 0

for i in range(K+1):
    for j in range(K-i+1):
        if i + j > N:
            break
        D.append(V[:i]+V[N-j:])

for d in D:
    for x in range(K-len(d)):
        if d==[]:
            break
        elif min(d)<0:
            d.pop(d.index(min(d)))

        elif min(d)>0:
            break
        
        if d!=[]:
            s = sum(d)
            if s>0 and s>ans:
                ans = s

print(s)

Fixed code:
import sys
input = sys.stdin.readline
N,K = map(int,input().split())
V = list(map(int,input().split()))
D = []
ans = 0
for i in range(K+1):
    for j in range(K-i+1):
        if i + j > N:
            break
        D.append(V[:i]+V[N-j:])
for d in D:
    for x in range(K-len(d)):
        if d==[]:
            break
        elif min(d)<0:
            d.pop(d.index(min(d)))

        elif min(d)>0:
            break        
    if d!=[]:
        s = sum(d)
        if s>0 and s>ans:
            ans = s
print(ans)
Buggy code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l[:]
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        for i in range(0,min(K-(A+B),A+B)):
            if ll[i] < 0:
                ll[i] = 0
            else:
                break
        print(A,B,ll)
        ans = max(sum(ll),ans)

print(ans)


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

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l[:]
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        for i in range(0,min(K-(A+B),A+B)):
            if ll[i] < 0:
                ll[i] = 0
            else:
                break
        ans = max(sum(ll),ans)

print(ans)
Buggy code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(k+1):
        nl = min(n,l)
        vl = v[:nl]
        s = k-nl
        for r in range(k+1-l):
            vr = v[max(0,n-r):]
            nr = len(vr)
            ns = s-nr
            nv = vl+vr
            nv.sort(reverse = True)
            while ns > 0 and nv and nv[0] < 0:
                nv.pop()
                ns -= 1
            su = sum(nv)
            if ans < su:
                ans = su
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(min(k,n)+1):
        vl = v[:l]
        for r in range(min(k,n)+1-l):
            vr = v[n-r:]
            s = max(0,k-l-r)
            nv = vl+vr
            nv.sort(reverse = True)
            while s > 0 and nv and nv[-1] < 0:
                nv.pop()
                s -= 1
            su = sum(nv)
            if ans < su:
                ans = su
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
Buggy code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
for i in range(k+1):
    a_cp=a.copy()

    tmp=a_cp[:i]
    a_cp[i:]
    for j in range(k+1-i):
        cal=tmp+a_cp[min(n-j,0):]
        
        heapq.heapify(cal)
        ans=max(ans,sum(cal))
        for _ in range(k-i-j):
            if len(cal)>0:
                heapq.heappop(cal)
                ans=max(ans,sum(cal))
            else:
                break
print(ans)

Fixed code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
for i in range(k+1):
    tmp=a[:i]
    for j in range(k+1-i):
        if i+j>n:
            cal=a.copy()
            zan=k-n
        else:
            cal=tmp+a[n-j:]
            zan=k-i-j
        
        heapq.heapify(cal)
        ans=max(ans,sum(cal))
        for _ in range(zan):
            if len(cal)>0:
                heapq.heappop(cal)
                ans=max(ans,sum(cal))
            else:
                break
print(ans)
Buggy code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
mini = min(n, k)
value = 0
for a in range(mini + 1):
  for b in range(mini - a + 1):
    c = k - a - b
    j = v[:a] + v[n-b:]
    j.sort(reverse=True)
    for _ in range(c):
      if j[-1] < 0:
        j.pop()
      else:
        break
    value = max(value, sum(j))
print(value)

Fixed code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
mini = min(n, k)
value = 0
for a in range(mini + 1):
  for b in range(mini - a + 1):
    c = k - a - b
    j = v[:a] + v[n-b:]
    j.sort(reverse=True)
    for _ in range(c):
      if j and j[-1] < 0:
        j.pop()
      else:
        break
    value = max(value, sum(j))
print(value)
Buggy code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

ans = 0
for a in range(n+1):
    for b in range(n+1):
        if a+b >= n:
            continue
        left = lst1[:a]
        right = lst1[n-b:]
        res = left + right
        rest = k - (a + b)
        if rest <= 0:
            pass
        else:
            res.sort()
            while rest:
                if len(res) == 0:
                    break
                if res[0] < 0:
                    del res[0]
                    rest -=1
                else:
                    break
        ans = max(ans,sum(res))

print(ans)








Fixed code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

ans = 0
for a in range(n+1):
    for b in range(n+1):
        if a+b > min(n,k):
            continue
        left = lst1[:a]
        right = lst1[n-b:]
        res = left + right
        rest = k - (a + b)
        if rest > 0:
            res.sort()
            while rest:
                if len(res) == 0:
                    break
                if res[0] < 0:
                    del res[0]
                    rest -=1
                else:
                    break
        ans = max(ans,sum(res))

print(ans)
Buggy code:
import bisect
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    m = 0
    for i in range(min(N, K)+1):
        for j in range(K - i):
            t = V[:i+1] + V[N-j:]
            t.sort()
            ind = bisect.bisect_left(t, 0)
            d = min((i+j), K - i - j, ind)
            m = max(m, sum(t[d:]))
    print(m)
main()


Fixed code:
import bisect
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    m = 0
    for i in range(min(N, K)+1):
        for j in range(min(K - i, N-i) + 1):
            t = V[:i] + V[N-j:]
            t.sort()
            ind = bisect.bisect_right(t, 0)
            d = min((i+j), K - i - j, ind)
            m = max(m, sum(t[d:]))
    print(m)
main()
Buggy code:
from collections import deque
import heapq

N, K=map(int, input().split())
V=list(map(int, input().split()))
lim=min(N, K)
ans=0
for num in range(lim+1):
    for A in range(num+1):
        B=num-A
        d=deque(V)
        hands=[]
        while d and len(h)<A:
            hands.append(d.popleft())
        while d and len(h)<A+B:
            hands.append(d.pop())
        heapq.heapify(hands)
        cnt=K-num
        while len(hands) and hands[0]<0 and cnt>0:
            cnt-=1
            heapq.heappop(hands)
        ans=max(ans, sum(hands))
print(ans)

Fixed code:
from collections import deque
import heapq

N, K=map(int, input().split())
V=list(map(int, input().split()))
lim=min(N, K)
ans=0
for num in range(lim+1):
    for A in range(num+1):
        B=num-A
        d=deque(V)
        hands=[]
        while d and len(hands)<A:
            hands.append(d.popleft())
        while d and len(hands)<A+B:
            hands.append(d.pop())
        heapq.heapify(hands)
        cnt=K-num
        while len(hands) and hands[0]<0 and cnt>0:
            cnt-=1
            heapq.heappop(hands)
        ans=max(ans, sum(hands))
print(ans)
Buggy code:
N,K = map(int,input().split())
V = [int(v) for v in input().split()]

ans = 0
for i in range(min(N+1,K+1)):
  que = V[:i]
  for j in range(min(N+1-i,K+1-i)):
    que += V[N-j:]
    que.sort()
    for k in range(min(K+1-i-j,i+j+1)):
      ans = max(ans,sum(que[k:]))
print(ans)

Fixed code:
N,K = map(int,input().split())
V = [int(v) for v in input().split()]

ans = 0
for i in range(min(N+1,K+1)):
  que1 = V[:i]
  for j in range(min(N+1-i,K+1-i)):
    que2 = que1 + V[N-j:]
    que2.sort()
    for k in range(min(K+1-i-j,i+j+1)):
      ans = max(ans,sum(que2[k:]))
print(ans)
Buggy code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            continue
        elif (N-i) == 0:
            continue
        elif i+j > N:
            right = V[-(N-i):]
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

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

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            right = []
        elif (N-i) == 0:
            right = []
        elif i+j > N:
            continue
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            if sum(sort_merge[k:]) == 990000000:
                print(sort_merge)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)
Buggy code:
import heapq
import copy

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_sum = 0
for a in range(0, K+1):
    for b in range(0, K+1-a):
        sum_list=copy.copy(V[:a]+V[N-b:])
        heapq.heapify(sum_list)
        for i in range(K-a-b):
            print(i)
            if sum_list:
                q=heapq.heappop(sum_list)
                if q>0:
                    heapq.heappush(sum_list, q)
                    break
            else:
                break
        # print(a, b,K-a-b,V[:a]+V[N-b:], sum_list)
        max_sum = max(max_sum, sum(sum_list))
print(max_sum)

Fixed code:
import heapq
import copy

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_sum = 0
for a in range(0, min(K+1, N+1)):
    for b in range(0, min(K+1-a, N+1-a)):
        sum_list=copy.copy(V[:a]+V[N-b:])
        heapq.heapify(sum_list)
        for i in range(K-a-b):
            if sum_list:
                q=heapq.heappop(sum_list)
                if q>0:
                    heapq.heappush(sum_list, q)
                    break
            else:
                break
        # print(a, b,K-a-b,V[:a]+V[N-b:], sum_list, sum(sum_list))
        # print(a, b,K-a-b)
        max_sum = max(max_sum, sum(sum_list))
print(max_sum)
Buggy code:
from collections import deque

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

for a in range(min(K,N)+1):
  for b in range(min(K-a+1,N-a+1)):
    print([a,b])
    hou = []
    Vd = deque(V)
    for _ in range(a):
      hou.append(Vd.popleft())
    for _ in range(b):
      hou.append(Vd.pop())
    hou.sort()
    for _ in range(K-a-b):
      if hou:
        if hou[0]>0:
          break
        hou.pop(0)
    score = max(score,sum(hou))
    
print(score)

Fixed code:
from collections import deque

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

for a in range(min(K,N)+1):
  for b in range(min(K-a+1,N-a+1)):
    hou = []
    Vd = deque(V)
    for _ in range(a):
      hou.append(Vd.popleft())
    for _ in range(b):
      hou.append(Vd.pop())
    hou.sort()
    for _ in range(K-a-b):
      if hou:
        if hou[0]>0:
          break
        hou.pop(0)
    score = max(score,sum(hou))
    
print(score)
Buggy code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(sum_a_b - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            print(score)
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)

Fixed code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(k + 1 - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)
Buggy code:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
gems = list(map(int, stdin.readline().rstrip().split()))
total = -99999999999
for i in range(min(n, k)+1):
    for j in range(min(n, k)+1):
        if i + j <= min(n, k):
            tmp = gems[:i] + gems[n-j:]
            tmp.sort()
            total = max(total, sum(tmp))
            for l in range(min(k-i-j+1, len(tmp))):
                if tmp[l] < 0:
                    total -= tmp[l]
                else:
                    break

print(total)

Fixed code:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
gems = list(map(int, stdin.readline().rstrip().split()))
total = -99999999999
for i in range(min(n, k)+1):
    for j in range(min(n, k)+1):
        if i + j <= min(n, k):
            tmp = gems[:i] + gems[n-j:]
            tmp.sort()
            for l in range(min(k-i-j, len(tmp))):
                if tmp[l] < 0:
                    tmp[l] = 0
            total = max(total, sum(tmp))


print(total)
Buggy code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        dp[i][j] = [V[:i]+V[N-j:]+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K)):
    for j in range(min(N,K)):
        if i+j!=0:
            t = K - (i + j)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC,0))

Fixed code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N+1,K+1)):
    for j in range(min(N+1,K-i+1)):
        if i<N-j:
            dp[i][j] = [V[:i]+V[N-j:]+[0]]
        else:
            dp[i][j] = [V+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N+1,K+1)):
    for j in range(max(0,min(N+1,K-i+1))):
        if i+j!=0:
            t = max(K - (i + j),0)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
if ansC:
    print(max(ansC))
else:
    print("0")
Buggy code:
import heapq
n,k=map(int,input().split())
v=[int(x) for x in input().split()]

print(n,k)
print(v)

p = list()
m = min(n,k)
t = 0

for h in range(1,m+1):
  for i in range(0,h+1):
    a = b = 0
    p.clear()
    for j in range(0,i):
      if v[j]<0:
        p.append(v[j])
      a += v[j]
    for j in range(1,h-i+1):
      if v[-j]<0:
        p.append(v[-j])
      b += v[-j]
    p.sort(reverse=True)
    n = a+b
    for i in range(min(k-h,len(p))):
      n -= p.pop()
    t = max(t,n)
print(t if t > 0 else 0)


Fixed code:
import heapq
n,k=map(int,input().split())
v=[int(x) for x in input().split()]

p = list()
m = min(n,k)
t = 0

for h in range(1,m+1):
  for i in range(0,h+1):
    a = b = 0
    p.clear()
    for j in range(0,i):
      if v[j]<0:
        p.append(v[j])
      a += v[j]
    for j in range(1,h-i+1):
      if v[-j]<0:
        p.append(v[-j])
      b += v[-j]
    p.sort(reverse=True)
    n = a+b
    for i in range(min(k-h,len(p))):
      n -= p.pop()
    t = max(t,n)
print(t if t > 0 else 0)
Buggy code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,k=map(int,input().split())
v1=list(map(int,input().split()))
v2=v1[::-1]
r=min(n,k)
ans=0
for i in range(r+1):
    for j in range(i+1):
        tmp=v1[:j]+v2[:i-j]
        tmp.sort()
        for k in range(min(k-i,i)):
            if tmp[k]<0:
                tmp[k]=0
        ans=max(ans,sum(tmp))

print(ans)

Fixed code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,k=map(int,input().split())
v1=list(map(int,input().split()))
v2=v1[::-1]
r=min(n,k)
ans=0
for i in range(r+1):
    for j in range(i+1):
        tmp=v1[:j]+v2[:i-j]
        tmp.sort()
        for l in range(min(k-i,i)):
            if tmp[l]<0:
                tmp[l]=0
        ans=max(ans,sum(tmp))

print(ans)
Buggy code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(min(n+1,k+1)):
    for r in range(min(n+1,k+1)):
        if l+r>n:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(min(w,len(sl))):
            if sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)


Fixed code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(min(n+1,k+1)):
    for r in range(min(n+1,k+1)):
        if l+r>k or l+r>n:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(min(w,len(sl))):
            if sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)
Buggy code:
N, K = map(int, input().split())
V = list(map(int, input().split()))

def query(l, r):
    if (l+r > min(N, K)):
        return 0
    rest = K-l-r
    lst = V[:l] + V[N-r+1:]
    lst.sort()
    print(l, r, lst, sum([x for i, x in enumerate(lst) if x > 0 or i >= rest]))
    return sum([x for i, x in enumerate(lst) if x > 0 or i >= rest])

ans = 0
for l in range(N):
    for r in range(N):
        ans = max(ans, query(l, r))
print(ans)

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

def query(l, r):
    if (l+r > K):
        return 0
    rest = K-l-r
    lst = V[:l] + V[N-r:]
    lst.sort()
    return sum([x for i, x in enumerate(lst) if x > 0 or i >= rest])

ans = 0
for l in range(N+1):
    for r in range(N+1-l):
        ans = max(ans, query(l, r))
print(ans)
Buggy code:
import heapq

N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = -float('inf')
for ai in range(K+1):
  for bi in range(K+1):
    if ai+bi > min(K,N): continue
    have = []
    if ai != 0: [heapq.heappush(have, v) for v in V[:ai]]
    if bi != 0: [heapq.heappush(have, v) for v in V[-bi:]]
    for i in range(K-ai-bi+1):
      if len(have) != 0: heapq.heappop(have)
    ans = max(ans,sum(have))
print(ans)

Fixed code:
import heapq

N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = -float('inf')
for ai in range(K+1):
  for bi in range(K+1):
    if ai+bi > min(K,N): continue
    have_plus = []
    have_neg = []
    if ai != 0: [heapq.heappush(have_plus, v) if v >= 0 else heapq.heappush(have_neg, v) for v in V[:ai]]
    if bi != 0: [heapq.heappush(have_plus, v) if v >= 0 else heapq.heappush(have_neg, v) for v in V[N-bi:]]
    for i in range(K-ai-bi):
      if len(have_neg) != 0:
        heapq.heappop(have_neg)
    ans = max(ans,sum(have_plus)+sum(have_neg))
print(ans)
Buggy code:
from itertools import combinations
from bisect import bisect_left
N, K = map(int,input().split())
A = list(map(int,input().split()))

ans=0

for i,j in combinations(range(N+2),2):
  l = sorted(A[0:i]+A[j-1:N])
  if len(l)<=K:
    m = bisect_left(l,0)
  	ans = max(ans,sum(l[min(K-len(l),m):]))
print(ans)


Fixed code:
from itertools import combinations
from bisect import bisect_left
N, K = map(int,input().split())
A = list(map(int,input().split()))

ans=0

for i,j in combinations(range(N+2),2):
  l = sorted(A[0:i]+A[j-1:N])
  if len(l)<=K:
    m = bisect_left(l,0)
    ans = max(ans,sum(l[min(K-len(l),m):]))
print(ans)
Buggy code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=0
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        while len(s)>=c+1 and t<=k:
            if s[c]<0:
                p-=s[c]
                c+=1
                t+=1
            else:
                break
        g=max(g,p)
print(g)

Fixed code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=0
for i in range(0,min(n,k)+1):
    for j in range(0,min(k-i,n-i)+1):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        while len(s)>=c+1 and t+1<=k:
            if s[c]<0:
                p-=s[c]
                c+=1
                t+=1
            else:
                break
        g=max(g,p)
print(g)