Problem p03032 (python)

Fixed submission:
from heapq import heapify, heappop

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(min(K, N) + 1):
    for to_remove in range(K + 1):
        if pick + to_remove > K: break
        for left in range(pick + 1):
            right = pick - left

            t = sum_L[left] + sum_R[right]

            q = vl[:left] + vr[:right]
            heapify(q)

            cnt = 0
            while cnt < to_remove and q:
                t -= heappop(q)
                cnt += 1
            ans = max(ans, t)
print(ans)

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

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K + 1):
    put = K - pick
    if pick < put: continue
    for left in range(pick + 1):
        right = pick - left

        t = sum_L[left] + sum_R[right]

        q = vl[:left] + vr[:right]
        heapify(q)

        cnt = 0
        while cnt < put and q:
            x = heappop(q)
            t -= x
            cnt += 1
        ans = max(ans, t)
print(ans)

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

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K + 1):
    put = K - pick
    if pick < put: continue
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = vl[:left] + vr[:right]
        heapify(q)
        while m_put > 0:
            t -= heappop(q)
            m_put -= 1
        ans = max(ans, t)
print(ans)

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

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K, -1, -1):
    put = K - pick
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = vl[:left] + vr[:right]
        heapify(q)
        while m_put > 0:
            t -= heappop(q)
            m_put -= 1
        ans = max(ans, t)
print(ans)

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

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

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

v.reverse()
slice_R = [v[:i] for i in range(N + 1)]
sum_R = [0]
for vv in v:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K, 0, -1):
    put = K - pick
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0:
            t -= heappop(q)
            m_put -= 1
        ans = max(ans, t)
print(ans)

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

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

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(range(K, 0, -1), 0):
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

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

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

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(range(K, 0, -1), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

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

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

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(range(K, 0, -1), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

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

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

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

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

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

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])

        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]
# print(slice_L)
# print(slice_R)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        ff = False
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        sl_i = 0
        sr_i = 0
        # print(m_put)
        while m_put > 0:
            # print(right, sr_i, slice_R[right])
            if sl_i == left:
                if sr_i == right:
                    ff = True
                    break
                t -= slice_R[right][sr_i]
                sr_i += 1
            elif sr_i == right:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] < slice_R[right][sr_i]:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] > slice_R[right][sr_i]:
                # print('test')
                t -= slice_R[right][sr_i]
                sr_i += 1
            else:
                if sl_i + 1 == left:
                    if sr_i == right:
                        ff = True
                        break
                    t -= slice_R[right][sr_i]
                    sr_i += 1
                elif sr_i + 1 == right:
                    t -= slice_L[left][sl_i]
                    sl_i += 1
                else:
                    flg = False
                    k = 0
                    while slice_L[left][sl_i + k] == slice_R[right][sr_i + k]:
                        k += 1
                        if sl_i + k == left:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
                            flg = True
                            break
                        elif sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                            flg = True
                            break
                    if not flg:
                        if sl_i + k == left and sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] < slice_R[right][sr_i + k]:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] > slice_R[right][sr_i + k]:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
            m_put -= 1
        # print(pick, left, right, t, slice_R[right], slice_L[left])
        if ff: continue
        ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]
# print(slice_L)
# print(slice_R)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        sl_i = 0
        sr_i = 0
        # print(m_put)
        while m_put > 0:
            # print(right, sr_i, slice_R[right])
            if sl_i == left:
                if sr_i == right: break
                t -= slice_R[right][sr_i]
                sr_i += 1
            elif sr_i == right:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] < slice_R[right][sr_i]:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] > slice_R[right][sr_i]:
                # print('test')
                t -= slice_R[right][sr_i]
                sr_i += 1
            else:
                if sl_i + 1 == left:
                    if sr_i == right: break
                    t -= slice_R[right][sr_i]
                    sr_i += 1
                elif sr_i + 1 == right:
                    t -= slice_L[left][sl_i]
                    sl_i += 1
                else:
                    flg = False
                    k = 0
                    while slice_L[left][sl_i + k] == slice_R[right][sr_i + k]:
                        k += 1
                        if sl_i + k == left:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
                            flg = True
                            break
                        elif sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                            flg = True
                            break
                    if not flg:
                        if sl_i + k == left and sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] < slice_R[right][sr_i + k]:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] > slice_R[right][sr_i + k]:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
            m_put -= 1
        # print(pick, left, right, t, slice_R[right], slice_L[left])
        ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]
# print(slice_L)
# print(slice_R)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        sl_i = 0
        sr_i = 0
        # print(m_put)
        while m_put > 0:
            # print(right, sr_i, slice_R[right])
            if sl_i == left:
                if sr_i == right: break
                t -= slice_R[right][sr_i]
                sr_i += 1
            elif sr_i == right:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] < slice_R[right][sr_i]:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] > slice_R[right][sr_i]:
                # print('test')
                t -= slice_R[right][sr_i]
                sr_i += 1
            else:
                if sl_i + 1 == left:
                    if sr_i == right: break
                    t -= slice_R[right][sr_i]
                    sr_i += 1
                elif sr_i + 1 == right:
                    t -= slice_L[left][sl_i]
                    sl_i += 1
                else:
                    flg = False
                    k = 0
                    while slice_L[left][sl_i + k] == slice_R[right][sr_i + k]:
                        k += 1
                        if sl_i + k == left:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
                            flg = True
                            break
                        elif sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                            flg = True
                            break
                    if not flg:
                        if sl_i + k == left and sr_i + k == right: break
                        if slice_L[left][sl_i + k] < slice_R[right][sr_i + k]:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] > slice_R[right][sr_i + k]:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
            m_put -= 1
        # print(pick, left, right, t, slice_R[right], slice_L[left])
        ans = max(ans, t)
print(ans)