Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r >= N:
            ans = max(ans, R[-1] - sum(R_m[-1]))
            break
        if r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
        else:
            m = sum(M)
        a = L[l] + R[r] - m
        ans = max(ans, a)

print(ans)

Fixed submission:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r >= N:
            ans = max(ans, R[-1] - sum(R_m[-1]))
            break
        if r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
        else:
            m = sum(M)
        a = L[l] + R[r] - m
        ans = max(ans, a)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):    # 消せるマイナスの数
    for l in range(K+1-i):
        r = K - i - l
        if l + r > N or r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
            a = L[l] + R[r] - m
            ans = max(ans, a)

print(ans)

Fixed submission:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r >= N:
            ans = max(ans, R[-1] - sum(R_m[-1]))
            break
        if r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
        else:
            m = sum(M)
        a = L[l] + R[r] - m
        ans = max(ans, a)

print(ans)