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:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,right,rK,N,score):
    dbg(V[left:right+1],rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil((right - left +1)/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,right,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(right ,max(right - ceil((right-left+1)/2)  ,right-rK)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (right - i)
        #next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
        next_tmp_v = tansaku(   left,   i-1  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V)-1,K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,right,rK,N,score):
    dbg(V[left:right+1],rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil((right - left +1)/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,right,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(right ,max(right - ceil((right-left+1)/2)  ,right-rK)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (i-right)
        #next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
        next_tmp_v = tansaku(   left,   i-1  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V)-1,K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,right,rK,N,score):
    dbg(V,rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil((right - left)/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,right,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(right-1 ,max(floor((right-left)/2) + left ,right-1-rK)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (i-(right-1))
        #next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
        next_tmp_v = tansaku(   left,   i -1  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V),K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,lenV,rK,N,score):
    dbg(V,rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil(lenV/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,lenV - (i - left )-1,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(left + lenV -1    ,max(floor(lenV/2) + left ,lenV-1-rK+left)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (lenV-1-i)
        next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V),K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,lenV,rK,N,score):
    dbg(V,rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil(lenV/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,lenV - (i - left )-1,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(left + lenV -1    ,max(floor(lenV/2) + left ,lenV-1-rK+left)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (len(V)-1-i)
        next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V),K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,lenV,rK,N,score):
    dbg(V,rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left, ceil(lenV/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,lenV - (i - left )-1,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(left + lenV -1    ,max(floor(lenV/2) + left ,lenV-1-rK+left)  ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (len(V)-1-i)
        next_tmp_v = tansaku(   left,  lenV - (left+lenV-1-i+1)  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V),K,[],0))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(left,lenV,rK,N,score):
    dbg(V,rK,N,score)


    max_v = score + sum(sorted(N)[rK:])


    n = []
    for i in range(left,min(rK+left,ceil(lenV/2)+left)):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[i+1:left+lenV]
        next_rK = rK - 1 - i - left
        next_tmp_v = tansaku(i+1,lenV - (i - left )-1,  next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    for i in range(    left + lenV -1    ,   max(floor(lenV/2) + left ,len(V)-1-rK) + left ,-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        #next_V = V[:i]
        next_rK = rK - 1 - (len(V)-1-i)
        next_tmp_v = tansaku(   left,  lenV - (left+lenV-1-i+1)  ,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(0,len(V),K,[],0))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
ans=0
chk=[0,0]
c=[]
for x in range(min(n,k)+1):
    if x>0:
        chk[0]+=v[x-1]
        if v[x-1]<0:
            c.append(v[x-1])
    ans=max(ans,chk[0])
    d=[]
    chk[1]=0
    for y in range(min(n,k)+1):
        if x+y>=k:
            break
        if y>0:
            chk[1]+=v[-y]
            if v[-y]<0:
                d.append(v[-y])
        ans=max(ans,chk[0]+chk[1])
        e=c+d
        e.sort(reverse=True)
        t=0
        for z in range(k):
            #if x+y+z>=k or len(e)==0:
            if x+y+z>k or len(e)==0:
                break
            if z>0:
                t+=-(e[-1])
                e.pop()
                ans=max(ans,chk[0]+chk[1]+t)
print(ans)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(V,rK):
    if rK == 0:
        return 0


    max_v = 0
    LV = V[:ceil(len(V)/2)]
    RV = V[ceil(len(V)/2):][::-1]


    for i in range(min(rK,len(LV))):
        if LV[i] <= 0:
            continue
        tmpL = LV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + LV[i]

        if v_tmp > 0:
            next_V = V[i+1:]
            next_rK = rK - 1 - i - j
            # next_v_tmp = tansaku(next_V,next_rK)
            # if next_v_tmp + v_tmp >= max_v:
            #     max_v = next_v_tmp + v_tmp
            # break
            max_v = max(tansaku(next_V,next_rK) + v_tmp, max_v)
            break


    for i in range(min(rK,len(RV))):
        if RV[i] <= 0:
            continue
        tmpL = RV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + RV[i]

        if v_tmp > 0:
            next_V = V[::-1][i+1:]
            next_rK = rK - 1 - i - j
            # next_v_tmp = tansaku(next_V,next_rK)
            # if next_v_tmp + v_tmp >= max_v:
            #     max_v = next_v_tmp + v_tmp
            max_v = max(tansaku(next_V,next_rK) + v_tmp,max_v)
            break

    return max_v


print(tansaku(V,K))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(V,rK):
    if rK == 0:
        return 0


    max_v = 0
    LV = V[:ceil(len(V)/2)]
    RV = V[floor(len(V)/2):][::-1]


    for i in range(min(rK,len(LV))):
        if LV[i] <= 0:
            continue
        tmpL = LV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + LV[i]

        if v_tmp > 0:
            next_V = V[i+1:]
            next_rK = rK - 1 - i - j
            # next_v_tmp = tansaku(next_V,next_rK)
            # if next_v_tmp + v_tmp >= max_v:
            #     max_v = next_v_tmp + v_tmp
            # break
            max_v = max(tansaku(next_V,next_rK) + v_tmp,max_v)
            break


    for i in range(min(rK,len(RV))):
        if RV[i] <= 0:
            continue
        tmpL = RV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + RV[i]

        if v_tmp > 0:
            next_V = V[::-1][i+1:][::-1]
            next_rK = rK - 1 - i - j
            # next_v_tmp = tansaku(next_V,next_rK)
            # if next_v_tmp + v_tmp >= max_v:
            #     max_v = next_v_tmp + v_tmp
            max_v = max(tansaku(next_V,next_rK) + v_tmp,max_v)
            break

    return max_v


print(tansaku(V,K))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(V,rK):
    if rK == 0:
        return 0


    max_v = 0
    LV = V[:ceil(len(V)/2)]
    RV = V[floor(len(V)/2):][::-1]


    for i in range(min(rK,len(LV))):
        if LV[i] <= 0:
            continue
        tmpL = LV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + LV[i]

        if v_tmp > 0:
            next_V = V[i+1:]
            next_rK = rK - 1 - i - j
            next_v_tmp = tansaku(next_V,next_rK)
            if next_v_tmp + v_tmp >= max_v:
                max_v = next_v_tmp + v_tmp
        break

    for i in range(min(rK,len(RV))):
        if RV[i] <= 0:
            continue
        tmpL = RV[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + RV[i]

        if v_tmp > 0:
            next_V = V[::-1][i+1:][::-1]
            next_rK = rK - 1 - i - j
            next_v_tmp = tansaku(next_V,next_rK)
            if next_v_tmp + v_tmp >= max_v:
                max_v = next_v_tmp + v_tmp
        break

    return max_v


print(tansaku(V,K))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(V,rK):
    if rK == 0:
        return 0


    max_v = 0


    LV = V[:ceil(len(V)/2)]
    RV = V[floor(len(V)/2):]


    V = LV
    for i in range(min(rK,len(V))):
        tmpL = V[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + V[i]

        if v_tmp > 0:
            next_V = V[i+1:] + RV
            next_rK = rK - 1 - i - j
            next_v_tmp = tansaku(next_V,next_rK)
            if next_v_tmp + v_tmp >= max_v:
                max_v = next_v_tmp + v_tmp
    V = RV[::-1]
    for i in range(min(rK,len(V))):
        tmpL = V[:i]
        tmpL.sort()
        j = 0 #捨てる石の数
        for _ in range(min(rK - 1 - i,len(tmpL)) ):
            if tmpL[j] >= 0:
                break
            j += 1
        dbg("dbg= ",tmpL)
        v_tmp = sum(tmpL[j:]) + V[i]

        if v_tmp > 0:
            next_V = LV + V[i+1:][::-1]
            next_rK = rK - 1 - i - j
            next_v_tmp = tansaku(next_V,next_rK)
            if next_v_tmp + v_tmp >= max_v:
                max_v = next_v_tmp + v_tmp

    return max_v


print(tansaku(V,K))

Submission status: Wrong Answer

Solution code:
INF = float("inf")
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N, K = nl()
Vs = nl()

import bisect

res = -INF
for l in range(K+1):
    for r in range(K+1-l):
        v = sorted(Vs[:l] + Vs[max(0, N-r):])
        i = 0
        for k in range(len(v)):
            i = k
            if v[i] >= 0:
                break
            #i = bisect.bisect_left(v, 0)
        value = sum(v[min(i, K-l-r):])
        if value > res:
            res = value
print(res)

Submission status: Wrong Answer

Solution code:
INF = float("inf")
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N, K = nl()
Vs = nl()

import bisect

res = -INF
for l in range(K+1):
    for r in range(K+1-l):
        v = sorted(Vs[:l] + Vs[max(0, N-r):])
        i = bisect.bisect_left(v, 0)
        value = sum(v[min(i, K-l-r):])
        if value > res:
            res = value
print(res)

Submission status: Wrong Answer

Solution code:
INF = float("inf")
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N, K = nl()
Vs = nl()

import bisect

res = -INF
for l in range(K+1):
    for r in range(K+1-l):
        v = sorted(Vs[:l] + Vs[N-r:])
        i = bisect.bisect_left(v, 0)
        value = sum(v[min(i, K-l-r):])
        if value > res:
            res = value
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
rev = v[::-1]
leftsum = [0]
rightsum = [0]
s = 0
for i in v:
    s = s+i
    leftsum.append(s)
s = 0
for i in v[::-1]:
    s = s+i
    rightsum.append(s)

m = 0
kn = min(k,n)

for x in range(1,kn+1):
    for a in range(x+1):
        for b in range(x-a+1):
            minus= [i for i in v[:a] if i<0]
            minus.extend([i for i in rev[:b] if i<0])
            minus.sort()
            cd = x-a-b
            tmp = leftsum[a]+rightsum[b]-sum(minus[:cd])
            #print('{0} {1} {2} {3} {4}'.format(a,b,cd,tmp,sum(minus[:cd])))
            m = max(m,tmp)

print(m)

Submission status: Wrong Answer

Solution code:
def v_and_num(queue):
  output = [(0,0)]
  for i in range(len(queue)+1):
    hand = queue[:i]
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

"""
left = D[:n//2+1]
right = list(reversed(D[n//2+1:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
      """

r = min(k,n)
M = 0
for a in range(r+1):
  for b in range(r+1-a):
    hand = D[:a] + D[-b:] if b!=0 else D[:a]
    print(hand)
    hand = list(sorted(hand))
    i = 0
    for i in range(min(k-a-b+1,a+b)):
      if hand[i] > 0:
        break
    buf = sum(hand[i:])
    if buf > M:
      M = buf
print(M)

Submission status: Wrong Answer

Solution code:
def v_and_num(queue):
  output = [(0,0)]
  for i in range(len(queue)+1):
    hand = queue[:i]
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

left = D[:n//2+1]
right = list(reversed(D[n//2+1:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
print(M)

Submission status: Wrong Answer

Solution code:
def v_and_num(queue):
  output = [(0,0)]
  for i in range(len(queue)+1):
    hand = queue[:i]
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

left = D[:n//2]
right = list(reversed(D[n//2:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
print(M)

Submission status: Wrong Answer

Solution code:
def v_and_num(queue):
  output = []
  for i in range(len(queue)):
    hand = queue[:i]
    process_num = i
    hand = sorted(hand)
    for j in range(len(hand)):
      output.append((i+j,sum(hand[j:])))
      if hand[j] > 0:
        break
  return output


n,k = list(map(int,input().split(" ")))
D =   list(map(int,input().split(" ")))

left = D[:n//2]
right = list(reversed(D[n//2:]))

l = v_and_num(left)
r = v_and_num(right)
M = 0
for k1, v1 in l:
  for k2, v2 in r:
    if k1+k2 > k:
      continue
    if v1+v2 > M:
      M = v1+v2
print(M)

Submission status: Wrong Answer

Solution code:
def solve(n, k, v):
    best = 0
    r = min(n, k)
    for a in range(r):
        for b in range(r-a):
            w = v[:a]
            if b > 0:
                w += v[-b:]
            w = sorted(w)
            m = min(k-a-b, len(w))
            for i in range(m):
                if w[i] < 0:
                    w[i] = 0
            best = max(best, sum(w))
    return best

n, k = map(int, input().split())
v = list(map(int, input().split()))
print(solve(n, k, v))

Submission status: Wrong Answer

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

if N < K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while len(V) > i:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        print(left,right)
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        t = K - a
        lr = left+right
        lr = sorted(lr)
        tans = sum(lr)
        for j in range(min(len(lr),t)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        t = K - a
        lr = left+right
        lr = sorted(lr)
        tans = sum(lr)
        for j in range(t):
            if len(lr) <= j:
                break
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().strip().split())
V = [int(i) for i in input().strip().split()]

R = min(N, K)

MAX_VALUE = 0

for a in range(R):
  for b in range(R - a):
    tmp = V[0:a]
    if b > 0:
      tmp.extend(V[-b:])

    tmp.sort()
    start = 0
    for i in range(min(len(tmp), K - a - b)):
      if tmp[i] >= 0: break
      start = i + 1
    MAX_VALUE = max(MAX_VALUE, sum(tmp[start:]))

print(MAX_VALUE)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for lk in range(min(K, N)+1):
    for rk in range(min(K, N)+1-lk):
        k = K - lk - rk
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]), ans)
if K >= 2 * N:
    ans = sum([v for v in V if v > 0])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for lk in range(min(K, N)+1):
    for rk in range(min(K, N)+1-lk):
        k = K - lk - rk
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]), ans)
if K >= N:
    ans = sum([v for v in V if v > 0])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for lk in range(min(K, N)):
    for rk in range(min(K, N)-lk):
        k = K - lk - rk
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K):
    for lk in range(K-k+1):
        rk = K - lk - k
        if lk + rk > N:
            continue
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]), ans)
if K >= N:
    ans = sum([v for v in V if v > 0])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K):
    for lk in range(K-k+1):
        rk = K - lk - k
        if lk + rk > N:
            continue
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    N, K = map(int, input().split())
    *D, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for i in range(m+1):
        for a in range(i+1):
            items = D[:a] + D[N-(i-a):]
            ans = max(ans, sum(items))
            heapq.heapify(items)
            for _ in range(min(i, m-i)):
                head = heapq.heappop(items)
                if head > 0:
                    break
                ans = max(ans, sum(items))
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
T = []
ans = 0
for i in range(k+1):
    for a in range(i+1):
        L = V[0:a]+V[n-i+a:n]
        L.sort()
        cur = sum(L)
        for b in range(k-i):
            if b < len(L):
                if L[b] < 0:
                    cur -= L[b]
        if cur > ans:
            ans = cur
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
p = 1
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i):
            if len(v1) != 0 and v[0] < 0:
                del v1[0]
        if len(v1) != 0:
            ansl.append(sum(v1))
        else:
            ansl.append(0)
ans = max(ansl)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i):
            if len(v1) != 0:
                del v1[0]
        ansl.append(sum(v1))
    ans = max(ansl)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,max_k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for k in range(1,max_k+1):
    for a in range(k+1):
        for b in range(k-a+1):
            if a+b>=n or a+b<=k-a-b:
                continue
            in_hand=d[:a]
            in_hand+=d[n-b:]
            in_hand=sorted(in_hand)
            answer=max(answer,sum(in_hand[k-a-b:]))
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(min(k,n)+1):
    for b in range(min(k,n)-a+1):
        if a+b>=n:
            continue
        in_hand=d[:a]
        in_hand+=d[n-b:]
        in_hand=sorted(in_hand)
        for i in range(k-a-b+1):
            answer=max(sum(in_hand[i:]),answer)
print(answer)

Submission status: Wrong Answer

Solution code:
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(base))
    return str(ans)
 
 
if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))