Buggy python submission (verdict: Runtime Error):
def A(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(0)

def B(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(len(lst)-1)

def C(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.insert(0 , hand.pop( hand.index(min(hand)) ))

def D(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.append(hand.pop( hand.index(min(hand))) )

import random

if __name__ == "__main__":
    N,K = map(int, input().split())
    V = input().split()
    V = [int(i) for i in V]
    hand = []

    for l in range(10000):
        V_copy = [i for i in V]
        hand_copy = []
        for i in range(K):
            rand = random.randint(1,4)
            if rand == 1:
                hand_copy.append(A(V_copy,hand_copy))
            elif rand == 2:
                hand_copy.append(B(V_copy,hand_copy))
            elif rand == 3:
                C(V_copy,hand_copy)
            else:
                D(V_copy,hand_copy)
        hand.append(hand_copy)

    x = []
    for i in range(10000):
        x.append(sum(hand[i]))
    print(max(x))

Fixed submission:
def MAP(): return map(int,input().split())

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for i in range(1,operation+1):
    for j in range(i+1):
        A = j   #左側から取り出す回数
        B = i-j    #右側から取り出す回数
        t = v[:A]
        if B>0:
            t+=v[-B:]
        t.sort()

        l = min(k-i,i-1)
        for ll in range(l):
            if t[ll] >= 0:
                l = ll
                break
        s = sum(t[l:])
        ans = max(ans,s)

print(ans)

Buggy python submission (verdict: Runtime Error):
def A(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(0)

def B(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(len(lst)-1)

def C(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.insert(0 , hand.pop( hand.index(min(hand)) ))

def D(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.append(hand.pop( hand.index(min(hand))) )

import random

if __name__ == "__main__":
    N,K = map(int, input().split())
    V = input().split()
    V = [int(i) for i in V]
    hand = []

    for l in range(1000):
        V_copy = [i for i in V]
        hand_copy = []
        for i in range(K):
            rand = random.randint(1,4)
            if rand == 1:
                hand_copy.append(A(V_copy,hand_copy))
            elif rand == 2:
                hand_copy.append(B(V_copy,hand_copy))
            elif rand == 3:
                C(V_copy,hand_copy)
            else:
                D(V_copy,hand_copy)
        hand.append(hand_copy)

    x = []
    for i in range(1000):
        x.append(sum(hand[i]))
    print(max(x))

Fixed submission:
def MAP(): return map(int,input().split())

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for i in range(1,operation+1):
    for j in range(i+1):
        A = j   #左側から取り出す回数
        B = i-j    #右側から取り出す回数
        t = v[:A]
        if B>0:
            t+=v[-B:]
        t.sort()

        l = min(k-i,i-1)
        for ll in range(l):
            if t[ll] >= 0:
                l = ll
                break
        s = sum(t[l:])
        ans = max(ans,s)

print(ans)