Problem:


Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.



Sample input:
6 4
-10 8 2 1 2 6


Sample output:
14

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m):
        for r in range(m):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort(reverse=True)
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m):
        for r in range(m):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
for l in range(N+1):
    for r in range(N+1):
        if l+r > K: continue
        t = K-(l+r)
        if r == 0:
            U = sorted(V[:l])
        else:
            U = sorted(V[:l]+V[-r:])
        for i in range(len(U)):
            if t > 0 and U[i] < 0:
                U[i] = 0
                t -= 1
        ans = max(ans,sum(U))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))

tab_sum = [0]
sum_tmp = 0
for i, Vi in enumerate(V):
    sum_tmp += Vi
    tab_sum.append(sum_tmp)

ans = 0
for N_pop in range(min(N, K) + 1):
    for N_left in range(N_pop + 1):
        N_right = N_pop - N_left
        #
        popped = []
        for i, Vi in enumerate(V):
            if i < N_left:
                popped.append(Vi)
            elif i < N - N_right:
                continue
            else:
                popped.append(Vi)
        popped.sort()
        #
        tab_sum = [0]
        sum_tmp = 0
        for item in popped:
            sum_tmp += item
            tab_sum.append(sum_tmp)
        #
        N_push = (K - N_pop) % 2
        while N_push <= min(N_pop, K - N_pop):
            ans_tmp = tab_sum[len(popped)] - tab_sum[N_push]
            ans = max(ans, ans_tmp)
            N_push += 1
            
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
for k in range(K+1):
    for l in range(k+1):
        t = sorted(V[:l])
        temp = sum(t)
        for m in range(min(len(t),K-len(t))):
            if t[m] < 0:
                temp -= t[m]
        ans = max(temp,ans)
        for r in range(K-l):
            t = sorted(V[:l]+V[-r-1:])
            temp = sum(t)
            for m in range(min(len(t),K-len(t))):
                if t[m] < 0:
                    temp -= t[m]
            ans = max(temp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N, K = map(int, input().split())
V = list(map(int, input().split()))

ans = 0

for l in range(min(N, K) + 1): #左側から取る個数
    left = N - l #筒に残っている数
    for r in range(min(left, K - l) + 1): #右側から取る個数
        k = K - l - r #捨てる個数
        if (l + r) < k: #取る数より捨てる数が多い時-->全部捨てるのと同義
            continue
        tmp = V[:l] + V[N - r:]
        tmp.sort(reverse = False)
        t = sum(tmp)
        for i in range(k): #最大捨てることができる個数
            if tmp[i] < 0:
                t -= tmp[i]
            else:
                break
        
        ans = max(ans, t)

print (ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))

tab_sum = [0]
sum_tmp = 0
for i, Vi in enumerate(V):
    sum_tmp += Vi
    tab_sum.append(sum_tmp)

ans = 0
for N_pop in range(min(N, K) + 1):
    N_push = K - N_pop
    if N_pop < N_push: continue
    #
    for N_left in range(N_pop + 1):
        N_right = N_pop - N_left
        #
        popped = []
        for i, Vi in enumerate(V):
            if i < N_left:
                popped.append(Vi)
            elif i < N - N_right:
                continue
            else:
                popped.append(Vi)
        popped.sort()
        #
        ans_tmp = sum(popped[N_push:])
        ans = max(ans, ans_tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N, K = map(int, input().split())
V = list(map(int, input().split()))

ans = 0

for l in range(min(N, K) + 1):
    left = N - l #筒に残っている数
    for r in range(min(left, K - l) + 1):
        k = K - l - r #捨てる個数
        if (l + r) < k: #取る数より捨てる数が多い時
            continue
        tmp = V[:l] + V[N - r:]
        tmp.sort(reverse = True)
        ans = max(ans, sum(tmp[:l + r - k]))

print (ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
min_nk=min(n,k)
for i in range(min_nk+1):#左
    for j in range(n-min_nk+i,n+1):#右
        count=a[0:i]+a[j:n]
        count.sort()
       # print(count)
        ans=max(ans,sum(count))
        for x in range(1,min_nk-len(count)+1):#消せれば消す
           # print(sum(count[x:]))
            ans=max(ans,sum(count[x:]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
min_nk=min(n,k)
for i in range(min_nk+1):#左
    for j in range(n-min_nk+i,n+1):#右
        count=a[0:i]+a[j:n]
        count.sort()
       # print(count)
        for x in range(1,min_nk-len(count)+1):#消せれば消す
           # print(sum(count[x:]))
            ans=max(ans,sum(count[x:]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)+1
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = k-l-r-1
        data = have[0]+have[1]
        data.sort()
        for i in range(len(data)):
            if i < a and data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)+1
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = m-l-r-1
        data = have[0]+have[1]
        data.sort()
        for i in range(len(data)):
            if i < a and data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = m-l-r
        data = have[0]+have[1]
        data.sort()
        for i in range(len(data)):
            if i < a and data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = m-l-r
        data = have[0]+have[1]
        data.sort()
        for i in range(min(len(data), a)):
            if data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(k+1):
    ans = max(ans,sum(v[:i])+sum(v[n-k+i:]))
    for j in range(1,k//2+1):#何個諦めるか
        for x in range(j+1):#右から何個諦めるか
            l = v[:i]
            r = list(reversed(v[n-k+i:]))
            rc = x
            lc = j-x
            if len(l)<lc or len(r)<rc:continue
            a = sorted(l[:len(l)-lc]+r[:len(r)-rc])
            ans = max(ans,sum(a[j:]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(k+1):
    ans = max(ans,sum(v[:i])+sum(v[n-k+i:]))
    for j in range(1,(k+1)//2):#何個諦めるか
        for x in range(j+1):#右から何個諦めるか
            l = v[:i]
            r = list(reversed(v[n-k+i:]))
            rc = x
            lc = j-x
            if len(l)<lc or len(r)<rc:continue
            for i in range(rc):
                r.pop()
            for i in range(lc):
                l.pop()
            a = sorted(l+r)
            ans = max(ans,sum(a[j:]))
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(k+1):
    for j in range(k//2):
        o = product(range(2),repeat=j)
        ans = max(ans,sum(v[:i])+sum(v[n-k+i:]))
        for x in o:
            l = v[:i]
            r = list(reversed(v[n-k+i:]))
            f = True
            for a in x:
                if a==0:
                    if l:l.pop()
                    else: f=False
                else:
                    if r:r.pop()
                    else:f=False
            if f:
                a = sorted(l+r)
                ans = max(ans,sum(a[j:]))
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for left in range(n+1):
    for right in range(left+1, n+1):
        have = []
        heapq.heapify(have)
        i = 0
        while i < left:
            heapq.heappush(have, v[i])
            i += 1
        cost = left + (n - right)
        if cost > k:
            continue
        i = n-1
        while i >= right:
            heapq.heappush(have, v[i])
            i -= 1
        while k - cost > 0 and len(have) > 0:
            value = heapq.heappop(have)
            if  value >= 0:
                heapq.heappush(have,value)
                break
            cost += 1
        #print("left:{} right:{} ans:{}".format(left, right, sum(have)))
        ans = max(ans, sum(have))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,k=NM()
l=L()
ans=0
for a in range(n+1):
    for b in range(1,n-a+1):
        for c in range(a+1):
            for d in range(b+1):
                if a+b+c+d>k:
                    continue
                x=l[:a]
                y=l[-1:-b-1:-1]
                x.sort()
                y.sort()
                ans=max(ans,sum(x[c:])+sum(y[d:]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

ll=[-10**9]*k
rr=[-10**9]*k

import heapq  # heapqライブラリのimport
import copy

def cnt2(ary):
    global n,k
    tmp=[-10**9]*k
    a=[]
    for i in range(min(n,k)):
        cnt=i
        a.append(ary[i])
        heapMm = copy.deepcopy(a)
        heapq.heapify(heapMm)
        sumA=sum(list(heapMm))
        if cnt<k:
            tmp[cnt]=max(tmp[cnt],sumA)
        else:
            break
        while 1:
            if len(list(heapMm))>0:
                t=heapq.heappop(heapMm)
                cnt+=1
                if t<0 and cnt<k:
                    sumA-=t
                    tmp[cnt]=max(tmp[cnt],sumA)
                else:
                    break
            else:
                break

    tmp[0]=max(tmp[0],0)
    for i in range(1,len(tmp)):
        if tmp[i-1]>tmp[i]:
            tmp[i]=tmp[i-1]
    
    return tmp



ll=[0]+cnt2(v)
rr=[0]+cnt2(v[::-1])

result=0
#print(ll)
#print(rr)

for i in range(k+1):
    result=max(result,ll[i]+rr[k-i])

print(result)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

ll=[-10**9]*k
rr=[-10**9]*k

import heapq  # heapqライブラリのimport
import copy

def cnt(ary):
    global n,k
    tmp=[-10**9]*k
    a=[]
    for i in range(min(n,k)):
        cnt=i
        a.append(ary[i])
        heapMm = copy.deepcopy(a)
        heapq.heapify(heapMm)
        sumA=sum(list(heapMm))
        if cnt<k:
            tmp[cnt]=max(tmp[cnt],sumA)
        else:
            break
        while 1:
            if len(list(heapMm))>0:
                t=heapq.heappop(heapMm)
                cnt+=1
                if t<0 and cnt<k:
                    sumA-=t
                    tmp[cnt]=max(tmp[cnt],sumA)
                else:
                    break
            else:
                break

    tmp[0]=max(tmp[0],0)
    for i in range(1,len(tmp)):
        if tmp[i-1]>tmp[i]:
            tmp[i]=tmp[i-1]
    
    return tmp



ll=[0]+cnt(v)
rr=[0]+cnt(v[::-1])

result=0
#print(ll)
#print(rr)

for i in range(k+1):
    result=max(result,ll[i]+rr[k-i])

print(result)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

ll=[-10**9]*k
rr=[-10**9]*k

import heapq  # heapqライブラリのimport
import copy

def cnt(ary):
    global n,k
    tmp=[-10**9]*k
    a=[]
    for i in range(min(n,k)):
        cnt=i
        a.append(ary[i])
        heapMm = copy.deepcopy(a)
        heapq.heapify(heapMm)
        sumA=sum(list(heapMm))
        if cnt<k:
            tmp[cnt]=max(tmp[cnt],sumA)
        while 1:
            if len(list(heapMm))>0:
                t=heapq.heappop(heapMm)
                cnt+=1
                if t<0 and cnt<k:
                    sumA-=t
                    tmp[cnt]=max(tmp[cnt],sumA)
                else:
                    break
            else:
                break

    tmp[0]=max(tmp[0],0)
    for i in range(1,len(tmp)):
        if tmp[i-1]>tmp[i]:
            tmp[i]=tmp[i-1]
    
    return tmp



ll=[0]+cnt(v)
rr=[0]+cnt(v[::-1])

result=0
#print(ll)
#print(rr)

for i in range(k+1):
    result=max(result,ll[i]+rr[k-i])

print(result)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

ll=[-10**9]*k
rr=[-10**9]*k

import heapq  # heapqライブラリのimport
import copy

def cnt(ary):
    global n,k
    tmp=[-10**9]*k
    a=[]
    for i in range(min(n,k)):
        cnt=i
        a.append(ary[i])
        heapMm = copy.deepcopy(a)
        heapq.heapify(heapMm)
        sumA=sum(list(heapMm))
        if cnt<k:
            tmp[cnt]=max(tmp[cnt],sumA)
        while 1:
            if len(list(heapMm))>0:
                t=heapq.heappop(heapMm)
                cnt+=1
                if t<0 and cnt<k:
                    sumA-=t
                    tmp[cnt]=max(tmp[cnt],sumA)
                else:
                    break
            else:
                break
    for i in range(1,len(tmp)):
        if tmp[i-1]>tmp[i]:
            tmp[i]=tmp[i-1]
    
    return tmp



ll=[0]+cnt(v)
rr=[0]+cnt(v[::-1])

result=0

for i in range(k+1):
    result=max(result,ll[i]+rr[k-i])

print(result)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

#k回行える

nk=min(n,k)

ll=[-10**9]*max(n,k)
rr=[-10**9]*max(n,k)
#左側から確認

mm=[]
pp=[]


import heapq  # heapqライブラリのimport
#heapq.heapify(mm)  # リストを優先度付きキューへ
#print(heapq.heappop(a))  # 最小値の取り出し
#heapq.heappush(a, -2)
import copy
for i in range(nk):
    #print("i#########",i)
    cnt=i
    if v[i]>=0:
        pp.append(v[i])
    else:
        mm.append(v[i])

    heapMm = copy.deepcopy(mm)
    heapq.heapify(heapMm)
    hoge=sum(pp)+sum(mm)
    #一発入れてみる
    ll[cnt]=max(ll[cnt],hoge)
    #print(ll)
    #print(heapMm)
    while len(list(heapMm))>0:
        tmp=heapq.heappop(heapMm)
        cnt+=1
        if cnt>=k:
            break
        hoge-=tmp
        ll[cnt]=max(ll[cnt],hoge)
        #print(ll)

for i in range(1,len(ll)):
    if ll[i-1]>ll[i]:
        ll[i]=ll[i-1]
#print(ll)

##右側から

pp=[]
mm=[]

v=v[::-1]

for i in range(nk):
    #print("i#########",i)
    cnt=i
    if v[i]>=0:
        pp.append(v[i])
    else:
        mm.append(v[i])

    heapMm = copy.deepcopy(mm)
    heapq.heapify(heapMm)
    hoge=sum(pp)+sum(mm)
    #一発入れてみる
    rr[cnt]=max(rr[cnt],hoge)
    #print(rr)
    #print(heapMm)
    while len(list(heapMm))>0:
        tmp=heapq.heappop(heapMm)
        cnt+=1
        if cnt>=k:
            break
        hoge-=tmp
        rr[cnt]=max(rr[cnt],hoge)
        #print(rr)

for i in range(1,len(rr)):
    if rr[i-1]>rr[i]:
        rr[i]=rr[i-1]


ll=[0]+ll
rr=[0]+rr

result=-10**9
for i in range(nk+1):
    result=max(result,ll[i]+rr[nk-i])
print(max(result,0))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))

#k回行える

nk=min(n,k)

ll=[-10**9]*max(n,k)
rr=[-10**9]*max(n,k)
#左側から確認

mm=[]
pp=[]


import heapq  # heapqライブラリのimport
#heapq.heapify(mm)  # リストを優先度付きキューへ
#print(heapq.heappop(a))  # 最小値の取り出し
#heapq.heappush(a, -2)
import copy
for i in range(nk):
    #print("i#########",i)
    cnt=i
    if v[i]>=0:
        pp.append(v[i])
    else:
        mm.append(v[i])

    heapMm = copy.deepcopy(mm)
    heapq.heapify(heapMm)
    hoge=sum(pp)+sum(mm)
    #一発入れてみる
    ll[cnt]=max(ll[cnt],hoge)
    #print(ll)
    #print(heapMm)
    while len(list(heapMm))>0:
        tmp=heapq.heappop(heapMm)
        cnt+=1
        if cnt>=k:
            break
        hoge-=tmp
        ll[cnt]=max(ll[cnt],hoge)
        #print(ll)

for i in range(1,len(ll)):
    if ll[i-1]>ll[i]:
        ll[i]=ll[i-1]
#print(ll)

##右側から

pp=[]
mm=[]

v=v[::-1]

for i in range(nk):
    #print("i#########",i)
    cnt=i
    if v[i]>=0:
        pp.append(v[i])
    else:
        mm.append(v[i])

    heapMm = copy.deepcopy(mm)
    heapq.heapify(heapMm)
    hoge=sum(pp)+sum(mm)
    #一発入れてみる
    rr[cnt]=max(rr[cnt],hoge)
    #print(rr)
    #print(heapMm)
    while len(list(heapMm))>0:
        tmp=heapq.heappop(heapMm)
        cnt+=1
        if cnt>=k:
            break
        hoge-=tmp
        rr[cnt]=max(rr[cnt],hoge)
        #print(rr)

for i in range(1,len(rr)):
    if rr[i-1]>rr[i]:
        rr[i]=rr[i-1]


ll=[0]+ll
rr=[0]+rr

result=-10**9
for i in range(nk+1):
    result=max(result,ll[i]+rr[nk-i])
print(result)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
from collections import deque
au = list(map(int,input().split()))
left = [0 for i in range(n)]
right = [0 for i in range(n)]
left[0] = au[0]
right[0] = au[-1]
ans = 0
for i in range(n-1):
    left[i+1] = left[i] + au[i+1]
    right[i+1] = right[i] + au[n-i-2]
p = min(n,k)
for a in range(p+1):
    for b in range(p+1-a):
        d = deque(au)
        x = []
        y = 0
        for i in range(a):
            u = d.popleft()
            x.append(u)
            y += u
        for j in range(b):
            v = d.pop()
            x.append(v)
            y += v
        x.sort(reverse = True)
        for ue in range(k-a-b):
            if len(x) > 0:
                y -= x.pop()
            else:
                break
        ans = max(ans,y)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))  # 筒

c = min(n, k)
ans = 0
for a in range(c):
    for b in range(c - a):
        w = v[:a] + v[n-b:]  # 手持ち
        w.sort()
        for i in range(k - (a + b)):
            if len(w) > 0 and w[0] < 0:
                del w[0]
            else:
                break
        ans = max(ans, sum(w))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import heapq
import copy
sys.setrecursionlimit(10 ** 6)

def foo(v, w, k, i):
    D = -10 ** 10
    if k == 0:
        return sum(w)
    else:
        ans1, ans2, ans3, ans4 = D, D, D, D
        default = sum(w)
        if len(v) > 1:
            if i != 3:
                v1, w1 = copy.copy(v), copy.copy(w)
                heapq.heappush(w1, v1.popleft())
                ans1 = foo(v1, w1, k-1, 1)
            if i != 4:
                v2, w2 = copy.copy(v), copy.copy(w)
                heapq.heappush(w2, v2.pop())
                ans2 = foo(v2, w2, k-1, 2)
        if len(w) > 1 and w[0] < 0:
            if i != 1:
                v1, w1 = copy.copy(v), copy.copy(w)
                v1.appendleft(heapq.heappop(w1))
                ans3 = foo(v1, w1, k-1, 3)
            if i != 2:
                v2, w2 = copy.copy(v), copy.copy(w)
                v2.appendleft(heapq.heappop(w2))
                ans4 = foo(v2, w2, k-1, 4)
        return max(ans1, ans2, ans3, ans4, default)
    

n, k = map(int, input().split())
v = deque(map(int, input().split()))  # 筒
w = []
heapq.heapify(w)  # 手持ち
print(foo(v, w, k, 0))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import heapq
import copy
sys.setrecursionlimit(10 ** 6)

def foo(v, w, k):
    D = -10 ** 10
    if k == 0:
        return sum(w)
    else:
        ans1, ans2, ans3, ans4 = D, D, D, D
        default = sum(w)
        if len(v) > 1:
            v1, w1 = copy.copy(v), copy.copy(w)
            heapq.heappush(w1, v1.popleft())
            ans1 = foo(v1, w1, k-1)
            v2, w2 = copy.copy(v), copy.copy(w)
            heapq.heappush(w2, v2.pop())
            ans2 = foo(v2, w2, k-1)
        if len(w) > 1:
            v1, w1 = copy.copy(v), copy.copy(w)
            v1.appendleft(heapq.heappop(w1))
            ans3 = foo(v1, w1, k-1)
            v2, w2 = copy.copy(v), copy.copy(w)
            v2.appendleft(heapq.heappop(w2))
            ans4 = foo(v2, w2, k-1)
        return max(ans1, ans2, ans3, ans4, default)
    

n, k = map(int, input().split())
v = deque(map(int, input().split()))  # 筒
w = []
heapq.heapify(w)  # 手持ち
print(foo(v, w, k))

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    v = list(map(int, input().split()))
    ans = -float('inf')
    for i in range(1, min(N,K)+1):
        for a in range(i+1):
            b = i-a
            A = v[:a]
            B = v[-b:]
            get = A+B
            get = sorted(get, reverse=True)
            for j in range(min(K-i, len(get))):
                p = get.pop()
                if p >= 0:
                    get.append(p)
                    break
            if sum(get) > ans:
                ans = sum(get)
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    for r in range(min_nk-l+1):
        hand = []
        for i in range(l):
            hand.append(v[i])
        for i in range(r):
            hand.append(v[n-1-i])
        hand.sort()

        back_count = 0
        while min_nk-l-r-back_count > 0:
            if len(hand) != 0:
                if min(hand) < 0:
                    hand[hand.index(min(hand))] = 0
                    back_count += 1
                else:
                    break
            else:
                break
        ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    for r in range(min_nk-l+1):
        hand = []
        for i in range(1, l+1):
            hand.append(v[i-1])
        for i in range(1, r+1):
            hand.append(v[n-i])
        hand.sort()

        back_count = 0
        while min_nk-l-r-back_count > 0:
            if len(hand) != 0:
                if min(hand) < 0:
                    hand[hand.index(min(hand))] = 0
                    back_count += 1
                else:
                    break
            else:
                break
        ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    for r in range(min_nk-l+1):
        hand = []
        for i in range(1, l+1):
            hand.append(v[i-1])
        for i in range(1, r+1):
            hand.append(v[n-i])
        hand.sort()

        back_count = 0
        while min_nk-l-r-back_count > 0:
            if len(hand) != 0:
                if min(hand) < 0:
                    hand[hand.index(min(hand))] = 0
                    back_count += 1
                else:
                    break
            else:
                break
        print(hand)
        ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    for r in range(min_nk-l+1):
        hand = []
        for i in range(1, l+1):
            hand.append(v[i-1])
        for i in range(1, r+1):
            hand.append(v[n-i])
        hand.sort()

        for back in range(min_nk-l-r):
            if len(hand) != 0:
                if min(hand) < 0:
                    x = hand.index(min(hand))
                    hand[x] = 0
        ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    if min_nk-l > 0:
        for r in range(min_nk-l+1):
            hand = []
            for i in range(1, l+1):
                hand.append(v[i-1])
            for i in range(1, r+1):
                hand.append(v[n-i])
            hand.sort()
            if min_nk-l-r > 0:
                for back in range(min_nk-l-r):
                    if len(hand) != 0:
                        if min(hand) < 0:
                            x = hand.index(min(hand))
                            hand[x] = 0
                ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1, N-i+1)):
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1, N-i)):
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans)
if __name__ == '__main__':
    main()