Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
V=list(map(int,input().split()))
V_=list(reversed(V))
ans=0
for k in range(1,K+1):
    k_=min(k,N)
    for kk in range(k_+1):
        sum=0
        l=[]
        for i in range(kk):
            sum+=V[i]
            l.append(V[i])
        for i in range(k-kk):
            sum+=V_[i]
            l.append(V_[i])
        l.sort()
        for j in range(min(k_,K-k_)):
            if l[j]>=0:
                break
            sum-=l[j]
        ans=max(ans,sum)
print(ans)


Fixed submission:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0

for i in range(1,N+1):#何個取るか
    if i>K:
        break
    for r in range(i+1):

        l=i-r
        a=V[:r]
        b=V[N-l:]
        f=max(0,K-i)
        c=a+b
        c.sort()
        k=0
        t=0
        for j in c:
            if t<f and j<0:
                t+=1
                continue
            k+=j
        ans=max(ans,k)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
KK=K
if K>N:
    KK=N
for i in range(KK+1):
    j=KK-i
    a=V[:i]
    b=V[N-j:]
    ans=max(ans,sum(a)+sum(b))
    k=sum(a)+sum(b)
    c=a+b
    c.sort(reverse=True)
    W=K+max(0,K-N)
    W-=2
    while(W>0):
        if c==[]:
            break
        t=c.pop()
        if t in a:
            a.remove(t)
        else:
            b.remove(t)
        if a!=[] and b!=[] :
            if a[-1]<=b[0]:
                h=a.pop(-1)
            else:
                h=b.pop(0)
        elif a!=[] and b==[]:
            h=a.pop(-1)
        elif b!=[] and a==[]:
            h=b.pop(0)
        else:
            break
        k=k-t-h
        ans=max(k,ans)
        W-=2
print(ans)


Fixed submission:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0

for i in range(1,N+1):#何個取るか
    if i>K:
        break
    for r in range(i+1):

        l=i-r
        a=V[:r]
        b=V[N-l:]
        f=max(0,K-i)
        c=a+b
        c.sort()
        k=0
        t=0
        for j in c:
            if t<f and j<0:
                t+=1
                continue
            k+=j
        ans=max(ans,k)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
for i in range(K+1):
    j=K-i
    a=V[:i]
    b=V[N-j:]
    ans=max(ans,sum(a)+sum(b))
    k=sum(a)+sum(b)
    c=a+b
    c.sort(reverse=True)
    W=K
    W-=2
    while(W>0):
        if c==[]:
            break
        t=c.pop()
        if t in a:
            a.remove(t)
        else:
            b.remove(t)
        if a!=[] and b!=[] :
            if a[-1]<=b[0]:
                h=a.pop(-1)
            else:
                h=b.pop(0)
        elif a!=[] and b==[]:
            h=a.pop(-1)
        elif b!=[] and a==[]:
            h=b.pop(0)
        else:
            break
        k=k-t-h
        ans=max(k,ans)
        W-=2
print(ans)


Fixed submission:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0

for i in range(1,N+1):#何個取るか
    if i>K:
        break
    for r in range(i+1):

        l=i-r
        a=V[:r]
        b=V[N-l:]
        f=max(0,K-i)
        c=a+b
        c.sort()
        k=0
        t=0
        for j in c:
            if t<f and j<0:
                t+=1
                continue
            k+=j
        ans=max(ans,k)
print(ans)