Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0:
                if hq[0] < 0:
                    S -= heappop(hq)

        ans = max(ans, S)

print(ans)


Fixed submission:
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K + 1):
    getV = min(N, K - back)
    for left in range(getV + 1):
        r = getV - left
        S = 0
        hq = []
        for i in range(left):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0 and hq[0] < 0:
                S -= heappop(hq)

        ans = max(ans, S)

print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K // 2 + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0 and hq[0] < 0:
                S -= heappop(hq)

        ans = max(ans, S)

print(ans)


Fixed submission:
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K + 1):
    getV = min(N, K - back)
    for left in range(getV + 1):
        r = getV - left
        S = 0
        hq = []
        for i in range(left):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0 and hq[0] < 0:
                S -= heappop(hq)

        ans = max(ans, S)

print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K // 2 + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            S -= heappop(hq)

        ans = max(ans, S)

print(ans)


Fixed submission:
from heapq import heappush, heappop


N, K = map(int, input().split())

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

ans = 0
for back in range(K + 1):
    getV = min(N, K - back)
    for left in range(getV + 1):
        r = getV - left
        S = 0
        hq = []
        for i in range(left):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0 and hq[0] < 0:
                S -= heappop(hq)

        ans = max(ans, S)

print(ans)