Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import collections
    import copy
    N,K = map(int, input().split())
    V = collections.deque(map(int, input().split()))

    answer = [[0]*(K+1) for _ in range(K+1)]


    for i in range(1,K+1):
        for j in range(1,min(i+1,N+1)):
            opt = 0
            for k in range(j+1):
                tmp = copy.deepcopy(V)
                tmp2 = collections.deque()
                for _ in range(k):
                    tmp2.append(tmp.popleft())
                for _ in range(j-k):
                    tmp2.append(tmp.pop())
                tmp2 = list(tmp2)
                tmp2.sort()
                for l in range(min(i-j,j)):
                    if tmp2[l] < 0:
                        tmp2[l] = 0
                S = sum(tmp2)
                if S > opt:
                    opt = S
            answer[i][j] = opt
    print(max(max(answer)))

main()

Fixed submission:
def main():
    import collections
    import copy
    N,K = map(int, input().split())
    V = collections.deque(map(int, input().split()))

    answer = [[0]*(K+1) for _ in range(K+1)]


    for i in range(1,K+1):
        for j in range(max(1,i//2),min(i+1,N+1)):
            opt = 0
            for k in range(j+1):
                tmp = copy.deepcopy(V)
                tmp2 = collections.deque()
                for _ in range(k):
                    tmp2.append(tmp.popleft())
                for _ in range(j-k):
                    tmp2.append(tmp.pop())
                tmp2 = list(tmp2)
                tmp2.sort()
                for l in range(min(i-j,j)):
                    if tmp2[l] < 0:
                        tmp2[l] = 0
                S = sum(tmp2)
                if S > opt:
                    opt = S
            answer[i][j] = opt
    Ans = 0
    for i in range(K+1):
        for j in range(K+1):
            if answer[i][j] > Ans:
                Ans = answer[i][j]
    print(Ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import collections
    import copy
    N,K = map(int, input().split())
    V = collections.deque(map(int, input().split()))

    answer = [[0]*(K+1) for _ in range(K+1)]


    for i in range(1,K+1):
        for j in range(1,min(i+1,N+1)):
            opt = 0
            for k in range(j+1):
                tmp = copy.deepcopy(V)
                tmp2 = collections.deque()
                for _ in range(k):
                    tmp2.append(tmp.popleft())
                for _ in range(j-k):
                    tmp2.append(tmp.pop())
                tmp2 = list(tmp2)
                tmp2.sort()
                for l in range(min(i-j,j)):
                    if tmp2[l] < 0:
                        tmp2[l] = 0
                S = sum(tmp2)
                if S > opt:
                    opt = S
            answer[i][j] = opt
    print(max(max(answer)))

main()

Fixed submission:
def main():
    import collections
    import copy
    N,K = map(int, input().split())
    V = collections.deque(map(int, input().split()))

    answer = [[0]*(K+1) for _ in range(K+1)]


    for i in range(1,K+1):
        for j in range(max(1,i//2),min(i+1,N+1)):
            opt = 0
            for k in range(j+1):
                tmp = copy.deepcopy(V)
                tmp2 = collections.deque()
                for _ in range(k):
                    tmp2.append(tmp.popleft())
                for _ in range(j-k):
                    tmp2.append(tmp.pop())
                tmp2 = list(tmp2)
                tmp2.sort()
                for l in range(min(i-j,j)):
                    if tmp2[l] < 0:
                        tmp2[l] = 0
                S = sum(tmp2)
                if S > opt:
                    opt = S
            answer[i][j] = opt
    Ans = 0
    for i in range(K+1):
        for j in range(K+1):
            if answer[i][j] > Ans:
                Ans = answer[i][j]
    print(Ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import copy
N,K = map(int, input().split())
V = collections.deque(map(int, input().split()))

answer = [[0]*(K+1) for _ in range(K+1)]


for i in range(1,K+1):
    for j in range(1,min(i+1,N+1)):
        opt = 0
        for k in range(j+1):
            tmp = copy.deepcopy(V)
            tmp2 = []
            for _ in range(k):
                tmp2.append(tmp.popleft())
            for _ in range(j-k):
                tmp2.append(tmp.pop())
            tmp2.sort()
            for l in range(min(i-j,j)):
                if tmp2[l] < 0:
                    tmp2[l] = 0
            S = sum(tmp2)
            if S > opt:
                opt = S
        answer[i][j] = opt
print(max(max(answer)))

Fixed submission:
def main():
    import collections
    import copy
    N,K = map(int, input().split())
    V = collections.deque(map(int, input().split()))

    answer = [[0]*(K+1) for _ in range(K+1)]


    for i in range(1,K+1):
        for j in range(max(1,i//2),min(i+1,N+1)):
            opt = 0
            for k in range(j+1):
                tmp = copy.deepcopy(V)
                tmp2 = collections.deque()
                for _ in range(k):
                    tmp2.append(tmp.popleft())
                for _ in range(j-k):
                    tmp2.append(tmp.pop())
                tmp2 = list(tmp2)
                tmp2.sort()
                for l in range(min(i-j,j)):
                    if tmp2[l] < 0:
                        tmp2[l] = 0
                S = sum(tmp2)
                if S > opt:
                    opt = S
            answer[i][j] = opt
    Ans = 0
    for i in range(K+1):
        for j in range(K+1):
            if answer[i][j] > Ans:
                Ans = answer[i][j]
    print(Ans)

main()