Buggy python submission (verdict: Runtime Error):
def solve(string):
    n, k, *v = map(int, string.split())
    lefts = [0] * (k + 1)
    rights = [0] * (k + 1)
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """
    for i in range(1, k + 1):
        _v = sorted(v[:i], reverse=True)
        lefts[i] = max(lefts[i], sum(_v))
        j = 0def solve(string):
    n, k, *v = map(int, string.split())
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """

    def s(w):
        l = [0] * (k + 1)
        for i in range(1, k + 1):
            _w = sorted(w[:i], reverse=True)
            tmp = sum(_w)
            l[i] = max(l[i], tmp)
            j = 0
            while i + j < k and len(_w) and _w[-1] < 0:
                j += 1
                tmp -= _w.pop()
                l[i + j] = max(l[i + j], tmp)
        return l

    lefts = s(v)
    rights = s(v[::-1])
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            lefts[i + j] = max(lefts[i + j], sum(_v))
    for i in range(1, k + 1):
        _v = sorted(v[-i:], reverse=True)
        rights[i] = max(rights[i], sum(_v))
        j = 0
        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            rights[i+j] = max(rights[i+j], sum(_v))
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))


Fixed submission:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Buggy python submission (verdict: Runtime Error):
def solve(string):
    n, k, *v = map(int, string.split())

    def dfs(deq, get, count, minus):
        if count == k:
            return sum(get)
        op1 = dfs(deq[1:], get + [deq[0]], count + 1, minus + (deq[0] < 0))
        op2 = dfs(deq[:-1], get + [deq[-1]], count + 1, minus + (deq[-1] < 0))
        m = min(get) if len(get) > 0 else 0
        op3 = 0 if m >= 0 else dfs(deq[:], sorted(get)[1:], count + 1, minus - 1)
        return max(op1, op2, op3)

    return str(dfs(v[:], [], 0, 0))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))


Fixed submission:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))