Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
v = list(map(int, input().split()))
# 再帰関数でいけそうだぞい
def dfs(que, cost, value):
    if cost <= 0:
        return value
    elif len(que) > 1:
        if que[0] < 0:
            tmp1 = max(dfs(que[1:], cost-1, value+que[0]), dfs(que[1:], cost-2, value))
        else:
            tmp1 = dfs(que[1:], cost-1, value+que[0])
        if que[-1] < 0:
            tmp2 = max(dfs(que[:-1], cost-1, value+que[-1]), dfs(que[:-1], cost-2, value))
        else:
            tmp2 = dfs(que[:-1], cost-1, value+que[-1])
    else:
        tmp1 = value + que[0]
        tmp2 = value + que[-1]
    return max(tmp1, tmp2)

ans = dfs(v, k, 0)
print(ans)


Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n - a, k - a) + 1):
        v_use = v[:a] + v[-b:] if b >= 1 else v[:a]
        t = k - a - b
        s = 0
        for j in sorted(v_use):
            if j < 0 and t > 0:
                s += -j
                t -= 1
        ans = max(ans, sum(v_use) + s)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
v = list(map(int, input().split()))
# 再帰関数でいけそうだぞい
def dfs(que, cost, value):
    if cost <= 0:
        return value
    elif len(que) > 1:
        if que[0] < 0:
            tmp1 = max(dfs(que[1:], cost-1, value+que[0]), dfs(que[1:], cost-2, value))
        else:
            tmp1 = dfs(que[1:], cost-1, value+que[0])
        if que[-1] < 0:
            tmp2 = max(dfs(que[:-1], cost-1, value+que[-1]), dfs(que[:-1], cost-2, value))
        else:
            tmp2 = dfs(que[:-1], cost-1, value+que[-1])
    else:
        tmp1 = value + que[0]
        tmp2 = value + que[-1]
    return max(tmp1, tmp2)

ans = dfs(v, k, 0)
print(ans)


Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n - a, k - a) + 1):
        v_use = v[:a] + v[-b:] if b >= 1 else v[:a]
        t = k - a - b
        s = 0
        for j in sorted(v_use):
            if j < 0 and t > 0:
                s += -j
                t -= 1
        ans = max(ans, sum(v_use) + s)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
v = list(map(int, input().split()))
# 再帰関数でいけそうだぞい
def dfs(que, cost, value):
    if cost <= 0:
        return value
    elif len(que) > 1:
        tmp1 = max(dfs(que[1:], cost-1, value+que[0]), dfs(que[1:], cost-2, value))
        tmp2 = max(dfs(que[:-1], cost-1, value+que[-1]), dfs(que[:-1], cost-2, value))
    else:
        tmp1 = value + que[0]
        tmp2 = value + que[-1]
    return max(tmp1, tmp2)

ans = dfs(v, k, 0)
print(ans)


Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n - a, k - a) + 1):
        v_use = v[:a] + v[-b:] if b >= 1 else v[:a]
        t = k - a - b
        s = 0
        for j in sorted(v_use):
            if j < 0 and t > 0:
                s += -j
                t -= 1
        ans = max(ans, sum(v_use) + s)

print(ans)