Problem p03032 (python)

Fixed submission:
from copy import deepcopy
N, K = map(int, input().split())
V = list(map(int, input().split()))
Lsum = [0]
Rsum = [0]
Lnegs = [[]]
Rnegs = [[]]
for i in range(N):
    Lsum.append(Lsum[i]+V[i])
    Rsum.append(Rsum[i]+V[N-i-1])
    Lnegs_cp = deepcopy(Lnegs[i])
    if V[i] < 0:
        Lnegs_cp.append(V[i])
        # Lnegs_cp = sorted(Lnegs_cp)
    Lnegs.append(Lnegs_cp)
    Rnegs_cp = deepcopy(Rnegs[i])
    if V[N-i-1] < 0:
        Rnegs_cp.append(V[N-i-1])
        # Rnegs_cp = sorted(Rnegs_cp)
    Rnegs.append(Rnegs_cp)

ans = 0

for dig in range(min(N+1, K+1)):
    n_del = K - dig
    for ldig in range(dig+1):
        rdig = dig - ldig
        LRsum = Lsum[ldig] + Rsum[rdig]
        negs = []
        for neg in Lnegs[ldig]:
            negs.append(neg)
        for neg in Rnegs[rdig]:
            negs.append(neg)
        negs = sorted(negs)
        for i in range(min(n_del, len(negs))):
            LRsum -= negs[i]
        ans = max(ans, LRsum)

print(ans)

# for llim in range(N+1):
#     rlim = N - llim
#     lmax = -1001001001
#     for k0 in range(min(K, llim)):
#         kh = min(K, llim) - k0
#         s0 = Lsum[k0]
#         s1 = 0
#         for l in range(min(len(Lnegs[k0]),kh)):
#             s1 -= Lnegs[k0][l]
#         lmax = max(lmax, s0+s1)
#         rmax = -1001001001
#         for k1 in range(min(K - k0, rlim)):
#             kh = min(K - k0, rlim) - k1
#             s0 = Rsum[k1]
#             s1 = 0
#             for l in range(min(len(Rnegs[k1]),kh)):
#                 s1 -= Rnegs[k1][l]
#             rmax = max(rmax, s0+s1)
#         ans = max(ans, lmax + rmax)

# print(ans)

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
N, K = map(int, input().split())
V = list(map(int, input().split()))
Lsum = [0]
Rsum = [0]
Lnegs = [[]]
Rnegs = [[]]
for i in range(N):
    Lsum.append(Lsum[i]+V[i])
    Rsum.append(Rsum[i]+V[N-i-1])
    Lnegs_cp = deepcopy(Lnegs[i])
    if V[i] < 0:
        Lnegs_cp.append(V[i])
        Lnegs_cp = sorted(Lnegs_cp)
    Lnegs.append(Lnegs_cp)
    Rnegs_cp = deepcopy(Rnegs[i])
    if V[N-i-1] < 0:
        Rnegs_cp.append(V[N-i-1])
        Rnegs_cp = sorted(Rnegs_cp)
    Rnegs.append(Rnegs_cp)

ans = 0

for dig in range(K+1):
    n_del = K - dig
    for ldig in range(dig+1):
        rdig = dig - ldig
        LRsum = Lsum[ldig] + Rsum[rdig]
        negs = []
        for neg in Lnegs[ldig]:
            negs.append(neg)
        for neg in Rnegs[rdig]:
            negs.append(neg)
        negs = sorted(negs)
        for i in range(min(n_del, len(negs))):
            LRsum -= negs[i]
        ans = max(ans, LRsum)

print(ans)

# for llim in range(N+1):
#     rlim = N - llim
#     lmax = -1001001001
#     for k0 in range(min(K, llim)):
#         kh = min(K, llim) - k0
#         s0 = Lsum[k0]
#         s1 = 0
#         for l in range(min(len(Lnegs[k0]),kh)):
#             s1 -= Lnegs[k0][l]
#         lmax = max(lmax, s0+s1)
#         rmax = -1001001001
#         for k1 in range(min(K - k0, rlim)):
#             kh = min(K - k0, rlim) - k1
#             s0 = Rsum[k1]
#             s1 = 0
#             for l in range(min(len(Rnegs[k1]),kh)):
#                 s1 -= Rnegs[k1][l]
#             rmax = max(rmax, s0+s1)
#         ans = max(ans, lmax + rmax)

# print(ans)