Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from collections import deque

def state_brute_force(state, count):
    state_list = [state for _ in range(count)]
    for s in product(*state_list):
        yield s


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

ans = 0

pop_l = deque.popleft
pop_r = deque.pop
add_l = deque.appendleft
add_r = deque.append

stop_condition = sum([item for item in D if item > 0])
if stop_condition > 0:
    for cmb in state_brute_force([pop_l, pop_r, add_l, add_r], K):
        q = deque(D)
        now = []
        value = 0
        for operation in cmb:
            if operation == add_l or operation == add_r:
                if len(now) == 0:
                    break
                item = min(now)
                now.remove(item)
                operation(q, item)
            if operation == pop_l or operation == pop_r:
                if len(q) == 0:
                    break
                now.append(operation(q))
        value = max(value, sum(now))
        ans = max(ans, value)
        if ans == stop_condition:
            break

print(ans)




Fixed submission:
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from collections import deque
from copy import deepcopy

def state_brute_force(state, count):
    state_list = [state for _ in range(count)]
    for s in product(*state_list):
        yield s


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

ans = 0

pop_l = deque.popleft
pop_r = deque.pop
add_l = deque.appendleft
add_r = deque.append

stop_condition = sum([item for item in D if item > 0])

cache = {}

for cmb in state_brute_force([pop_l, pop_r, add_l, add_r], K):
    q = deque(D)
    now = []
    op_counter = 0
    value = 0
    operations = []
    for operation in cmb:
        operations.append(operation)
        ca = cache.get(tuple(operations))
        if ca is not None:
            q = deque(ca[0])
            now = deepcopy(ca[1])
            continue
        if operation == add_l or operation == add_r:
            if len(now) == 0:
                break
            item = min(now)
            now.remove(item)
            operation(q, item)
        if operation == pop_l or operation == pop_r:
            if len(q) == 0:
                break
            now.append(operation(q))
    cache[tuple(operations)] = (list(q), now)
    value = max(value, sum(now))
    ans = max(ans, value)
    if ans == stop_condition:
        break

print(ans)




Fixed submission:
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from collections import deque

def state_brute_force(state, count):
    state_list = [state for _ in range(count)]
    for s in product(*state_list):
        yield s


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

ans = 0

pop_l = deque.popleft
pop_r = deque.pop
add_l = deque.appendleft
add_r = deque.append

stop_condition = sum([item for item in D if item > 0])

for cmb in state_brute_force([pop_l, pop_r, add_l, add_r], K):
    q = deque(D)
    now = []
    value = 0
    for operation in cmb:
        if operation == add_l or operation == add_r:
            if len(now) == 0:
                break
            item = min(now)
            now.remove(item)
            operation(q, item)
        if operation == pop_l or operation == pop_r:
            if len(q) == 0:
                break
            now.append(operation(q))
    value = max(value, sum(now))
    ans = max(ans, value)
    if ans == stop_condition:
        break

print(ans)




Fixed submission:
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from collections import deque

def state_brute_force(state, count):
    state_list = [state for _ in range(count)]
    for s in product(*state_list):
        yield s


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

ans = 0

pop_l = deque.popleft
pop_r = deque.pop
add_l = deque.appendleft
add_r = deque.append

stop_condition = sum([item for item in D if item > 0])

for cmb in state_brute_force([pop_l, pop_r, add_l, add_r], K):
    q = deque(D)
    now = []
    value = 0
    for operation in cmb:
        if operation == add_l or operation == add_r:
            if len(now) == 0:
                break
            item = min(now)
            now.remove(item)
            operation(q, item)
        if operation == pop_l or operation == pop_r:
            if len(q) == 0:
                break
            now.append(operation(q))
        value = max(value, sum(now))
    ans = max(ans, value)
    if ans == stop_condition:
        break

print(ans)




Fixed submission:
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from collections import deque

def state_brute_force(state, count):
    state_list = [state for _ in range(count)]
    for s in product(*state_list):
        yield s


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

ans = 0

pop_l = deque.popleft
pop_r = deque.pop
add_l = deque.appendleft
add_r = deque.append

stop_condition = sum([item for item in D if item > 0])

for cmb in state_brute_force([pop_l, pop_r, add_l, add_r], K):
    q = deque(D)
    now = []
    value = 0
    for operation in cmb:
        if operation == add_l or operation == add_r:
            if len(now) == 0:
                break
            item = min(now)
            now.remove(item)
            operation(q, item)
        if operation == pop_l or operation == pop_r:
            if len(q) == 0:
                break
            now.append(operation(q))
        value = max(value, sum(now))
    ans = max(ans, value)
    if ans == stop_condition:
        break

print(ans)




Fixed submission:
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)