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:
N,K = map(int,input().split())
V = list(map(int,input().split()))
KK = K
K = min(N,K)

ans = 0
for k in range(1,K):#K+1だっけ？
    for i in range(k+1):
        R = V[-(k-i):]
        if len(R) == N:
            R = []
        print(R)
        L = V[:i]+R+[0]
        L.sort()
        zero_index = min(L.index(0),KK-k)
        res = sum(L[zero_index:])
        ans = max(ans,res)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
vv = list(map(int,input().split()))
get_max = min(n,k)
ans = 0
for li in range(get_max + 1):
    takes_left = vv[:li]
    for ri in range(get_max - li + 1):
        takes = takes_left + vv[::-1][:ri]
        takes = sorted(takes)
        for x in range(min(get_max - li - ri,len(takes))):
            if takes[x] < 0:
                takes[x] = 0
            else:
                break
        ans = max(ans,sum(takes))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for l in range(K):
        for r in range(K - l):
            if l + r > N:
                continue
            d = K - l - r
            s = V[:l] + V[N - r:N]
            now = 0
            now += sum(s)
            s.sort()
            for i in range(d):
                if i >= len(s):
                    break
                if s[i] > 0:
                    break
                now -= s[i]
            ans = max(ans, now)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for l in range(K):
        for r in range(K - l, -1, -1):
            LR = V[:l] + V[N - r:N]
            for _ in range(K - r - l):
                if 0 >= len(LR):
                    break
                LR.pop(LR.index(min(LR)))
            if len(LR) > 0:
                ans = max(ans, sum(LR))
    print(ans)


main()

Submission status: Wrong Answer

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

maxsum = -10**13
for i in range(min(n+1,k+1)):
    for j in range(min(n+1-i,k+1-i)):
        su = (V[:i] + V[:n-j-1:-1])
        su.sort()
        sumsu = sum(su)
        #print("i,j,",i,j,su)
        if k-i-j <= 0: continue
        for l in range(k-i-j):
            if len(su) > l and su[l] < 0: sumsu -= su[l]
        maxsum = sumsu if maxsum < sumsu else maxsum
print(maxsum)

Submission status: Wrong Answer

Solution code:
import sys

def intinput(): return map(int,sys.stdin.readline().split())

n,k=intinput()
l = list(intinput())
f=0
for i in range(k+1):
    for j in range(k-i+1):
        li = l[:i] + l[n-j:]
        li.sort()
        cnt = k-i-j
        for h in range(len(li)):
            if cnt <= 0 or li[h]>=0:
                break
            elif li[h]<0:
                li[h]=0
                cnt -= 1
        f = max(f,sum(li))
print(f)

Submission status: Wrong Answer

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

maxsum = -10**10
for i in range(min(n+1,k+1)):
    for j in range(min(n+1-i,k+1-i)):
        su = (V[:i] + V[:n-j-1:-1])
        su.sort()
        sumsu = sum(su)
        #print("i,j,",i,j,su)
        if k-i-j <= 0: continue
        for l in range(k-i-j):
            if len(su) > l and su[l] < 0: sumsu -= su[l]
        maxsum = sumsu if maxsum < sumsu else maxsum
print(maxsum)

Submission status: Wrong Answer

Solution code:
def take(n, lis):
    if n >= len(lis):
        return lis
    if n <= 0:
        return []
    else:
        return lis[:n]


n, k = list(map(int, input().split(" ")))
vs = list(map(int, input().split(" ")))
if n == 1:
    print(max(0, vs[0]))
    exit()
if k == 1:
    print(max(0, vs[0], vs[-1]))
    exit()
scores = []
score = 0
length = min(n, k)
for gets in range(length + 1):
    for left in range(gets + 1):
        right = gets - left
        retnum = length - gets
        get_list = take(left, vs) + take(right, vs[::-1])
        minus = sorted([get for get in get_list if get <= 0])
        score = sum(get_list) - sum(take(retnum, minus))
        scores.append(score)
print(max(0, *scores))

Submission status: Wrong Answer

Solution code:
def take(n, lis):
    if n >= len(lis):
        return lis
    if n <= 0:
        return []
    else:
        return lis[:n]


n, k = list(map(int, input().split(" ")))
vs = list(map(int, input().split(" ")))
if n == 1:
    print(max(0, vs[0]))
    exit()
if k == 1:
    print(max(0, vs[0], vs[-1]))
    exit()
scores = []
score = 0
for gets in range(k + 1):
    for left in range(gets + 1):
        right = gets - left
        retnum = k - gets
        get_list = take(left, vs) + take(right, vs[::-1])
        minus = sorted([get for get in get_list if get <= 0])
        score = sum(get_list) - sum(take(retnum, minus))
        scores.append(score)
print(max(0, *scores))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ans = 0 # ans >= 0
for l in range(K+1):
    for r in range(K+1-l):
        ll_max = K+1-l-r
        if ll_max < 0:
            break
        for ll in range(ll_max):
            rr = K-l-r-ll
            # if rr < 0:
            #     break
            # if ll > l or rr > r:
            #     break
            # if not all([0<=val<N for val in [l,r,ll,rr]]):
            #     break
            # print("l,r,ll,rr:", l,r,ll,rr)
            l_vs = sorted(vs[:l])
            r_vs = sorted(vs[N-r:])

            l_remain = l_vs[ll:]
            r_remain = r_vs[rr:]
            # print("l_remain:", l_remain)
            # print("r_remain:", r_remain)
            ans = max(ans, sum(l_remain) + sum(r_remain))

print(ans)

Submission status: Wrong Answer

Solution code:
def take(n, lis):
    if n >= len(lis):
        return lis
    if n <= 0:
        return []
    else:
        return lis[:n]


n, k = list(map(int, input().split(" ")))
vs = list(map(int, input().split(" ")))
scores = []
score = 0
for gets in range(k + 1):
    for left in range(gets + 1):
        right = gets - left
        retnum = k - gets
        get_list = take(left, vs) + take(right, vs[::-1])
        minus = sorted([get for get in get_list if get <= 0])
        score = sum(get_list) - sum(take(retnum, minus))
        scores.append(score)
print(max(0, *scores))

Submission status: Wrong Answer

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

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
ans=[]
k=min(n,k)
for i in range(k+1):
    for j in range(k-i+1):
        d=k-i-j
        l=v[0:i]
        r=v[n-j:n]
        p=r+l
        q=[]
        s=sum(p)
        for x in p:
            if x<0:
                q.append(x)
        q.sort()
        for x in range(min(d,len(q))):
            s-=q[x]
        ans.append(s)
        
print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
V=list(map(int,input().split()))
max_score=0
for k in range(K+1):#合計取る個数
    for k_l in range(k+1):# 左から取る個数
        left=V[:k_l]
        k_r = k-k_l
        right=V[-k_r:]if k_r>0 else []
        sterable=sorted(left+right)[:K-k]
        stesum=0
        for ss in sterable:
            if ss<0:
                stesum+=ss
            else:break
        score = sum(left)+sum(right) - stesum
        max_score=max(score,max_score)
print(max_score)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
V=list(map(int,input().split()))
max_score=0
for k in range(K+1):#合計取る個数
    for k_l in range(k+1):# 左から取る個数
        left=V[:k_l]
        k_r = k-k_l
        right=V[-k_r:]if k_r>0 else []
        k_ste=K-k
        stesum=sum(sorted(left+right)[:k_ste])
        score = sum(left)+sum(right) - stesum
        max_score=max(score,max_score)
print(max_score)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
ans=[]

for i in range(k+1):
    for j in range(k-i+1):
        d=k-i-j
        l=v[0:i]
        r=v[n-j:n]
        p=r+l
        q=[]
        s=sum(p)
        for x in p:
            if x<0:
                q.append(x)
        q.sort()
        for x in range(min(d,len(q))):
            s-=q[x]
        ans.append(s)
        
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
gems = [int(x) for x in input().split()]

max_price = 0
for pick in range(1, K + 1):
    for left_pick_num in range(pick + 1):
        right_pick_num = max(pick - left_pick_num, 0)
#        if not left_pick_num and not right_pick_num: continue
#        print("Pick {} from left, {} from right".format(left_pick_num, right_pick_num))
        picked = gems[:left_pick_num]
        if right_pick_num: picked += gems[-right_pick_num:]
#        print("Pick {} gems".format(pick))
#        print("The gems picked: {}".format(picked))
        picked.sort()
        num_throw = min(pick, K - pick)
        j = 0
        while j < num_throw and picked[j] < 0:
            j += 1
#        print("The gems picked after throwing: {}".format(picked[j:]))
        max_price = max(max_price, sum(picked[j:]))
print(max_price)

Submission status: Wrong Answer

Solution code:
[N,K]=list(map(int,input().split()))
V=list(map(int,input().split()))
have=[]
for n,k in enumerate(reversed(range(0,K+1))):
  tmp=[V[:ki]+V[-(k-ki):] for ki in range(k)
      ]+[V[:k],]
  tmp=[sorted(item) for item in tmp if any([g>0 for g in item])]
  #print(n,tmp)
  if n!=0 and tmp!=[]:
    for num_remove in range(n):
      tmp2=[item[n:] for item in tmp 
            if all([g<0 for g in item[:n]])
           ]
      
      tmp.extend(tmp2)
      #print("tmp",tmp)
      #print("tmp2",tmp2)
      
  have.extend(tmp)
  #print(n,have[-1])

if have==[]:
  print(0)
else:
  print(max(max(list(map(sum,have))),
            0
           )
       )

Submission status: Wrong Answer

Solution code:
[N,K]=list(map(int,input().split()))
V=list(map(int,input().split()))
have=[]
for n,k in enumerate(reversed(range(0,K+1))):
  tmp=[V[:ki]+V[-(k-ki):] for ki in range(k)
      ]+[V[:k],]
  tmp=[sorted(item) for item in tmp if sum(item)>0]
  #print(n,tmp)
  if n!=0:
    for num_remove in range(n):
      tmp2=[item[n:] for item in tmp 
            if all([g<0 for g in item[:n]])
           ]
      
      tmp.extend(tmp2)
      #print("tmp",tmp)
      #print("tmp2",tmp2)
      
  have.extend(tmp)
  #print(n,have[-1])

if have==[]:
  print(0)
else:
  print(max(list(map(sum,have))))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
lim = min(n, k)
ans = [0]

for a in range(lim+1):
    for b in range(lim-a+1):
        hand = sorted(v[:a]+v[n-b:])
        hs = sum(hand)
        for i in range(min(lim-a-b+1, a+b)):
            test = hand[i]
            if test < 0:
                hs -= test
        ans.append(hs)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
lim = min(n, k)
ans = [0]

for a in range(lim+1):
    for b in range(lim-a+1):
        hand = sorted(v[:a]+v[n-b:])
        hs = sum(hand)
        for i in range(min(lim-a-b, a+b)):
            test = hand[i]
            if test < 0:
                hs -= test
        ans.append(hs)

print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
lim = min(n, k)
ans = [0]

for a in range(lim+1):
    for b in range(lim-a+1):
        hand = sorted(v[:a]+v[n-b:])
        hs = sum(hand)
        for i in range(min(lim-a-b, a+b)):
            test = hand[i]
            if test < 0:
                hs -= test
        ans.append(hs)
        print(a, b, hand, hs)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
lim = min(n, k)
ans = []

for a in range(lim+1):
    for b in range(lim-a+1):
        hand = sorted(v[:a]+v[n-b:])
        hs = sum(hand)
        for i in range(min(lim-a-b, a+b)):
            test = hand[i]
            if test < 0:
                hs -= test
        ans.append(hs)

print(max(ans))

Submission status: Wrong Answer

Solution code:
import math
import numpy as np


def inputs(): return list(map(int, input().split()))


def main():
    N, K = inputs()
    D = inputs()
    ans = []
    cnt = 0
    for i in range(K):
        if not D:
            break
        else:
            index = np.argmax(D)
            MAX = D[index]
            if MAX < 0:
                break
            else:
                if N / 2 > index:
                    v = D.pop(0)
                else:
                    v = D.pop()
                if v < 0:
                    cnt += 1
            ans.append(v)
            if cnt + i + 1 >= K:
                break
        # print(ans, cnt + i + 1)
    ans.sort()
    # print(ans)
    for i in range(cnt):
        ans.pop(0)

    print(max(sum(ans), 0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = -10**15
for i in range(N+1):
    for j in range(N+1):
        if(i+j>K): continue
        li = []
        if(not j==0):
            li = V[:i]+V[-j:]
        else:
            li = V[:i]
        cnt = 0
        li.sort()
        for x in li:
            if(x<0):
                cnt+=1
            else:
                break
        ans = max(ans,sum(li[min(cnt,K-(i+j)):]))

print(ans)

Submission status: Wrong Answer

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

ans = -pow(10, 9)
for left in range(0, K + 1):
  for right in range(0, K - left + 1):
    all_get = V[:left] + V[::-1][:right]
    all_get.sort(reverse=True)
    num = 0
    while True:
      if num >= K - (left + right) or len(all_get) == 0:
        break
      if all_get[-1] >= 0:
        break
      all_get.pop(-1)
      num += 1
    now = sum(all_get)
    ans = max(ans, now)
print(ans)

Submission status: Wrong Answer

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

ans = -pow(10, 9)
for left in range(0, K + 1):
  for right in range(0, K - left + 1):
    all_get = V[:left] + V[::-1][:right]
    all_get.sort(reverse=True)
    num = 0
    while True:
      if num >= K - (left + right) or len(all_get) == 0:
        break
      if all_get[-1] >= 0:
        break
      all_get.pop(-1)
    now = sum(all_get)
    ans = max(ans, now)
print(ans)

Submission status: Wrong Answer

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

left = K
right = K - left

ans = -pow(10, 9)
while left < 0:
  all_get = V[:left] + V[::-1][:right]
  all_get.sort(reverse=True)
  num = 0
  while True:
    if num >= K - (left + right) or all_get[-1] >= 0:
      break
    all_get.pop(-1)
  now = sum(all_get)
  ans = max(ans, now)
  left -= 1
  right += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
DP = [[[-10**10,0] for _ in range(N+1)]for _ in range(N+1)]
ans = -10**10
for i in range(N+1):
    for j in range(N+1):
        if(i+j>K): continue
        if(not j==0):
            li = V[:i]+V[-j:]
        else:
            li = V[:i]
        cnt = 0
        li.sort()
        for x in li:
            if(x<0):
                cnt+=1
            else:
                break
        ans = max(ans,sum(li[min(cnt,K-(i+j)):]))

print(ans)

Submission status: Wrong Answer

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

INT_MIN = -1 * (10**10)

ans = INT_MIN

for left in range(0,K+1):
    for right in range(0, K+1-left):
        #print("========")
        #print(left, right)
        #print(V[:left], V[left:N-right], V[N-right:])
        
        rest_ope = K-left-right
        #print(rest_ope)
        my_v = V[:left]+V[N-right:]
        sum_v = 0
        for i, x in enumerate(sorted(my_v)):
            if x < 0 and i < rest_ope:
                continue
            else:
                sum_v += x
        #print(sum_v)
        ans = max(ans, sum_v)
        
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
m=K//2 #mは捨てる回数の最大
lis=list(map(int,input().split())) #宝石リスト

ann=0 #annはi変化での最大
for i in range(0,m+1): #iは捨てる回数で場合分け
  n=K-i #nはとりあえず取る個数
  if n>=N: #とりあえず全部とれるとき
    lsum=sorted(lis)
    an=sum(lsum[i:]) #anはiの時の最大

    if an>ann: #i0~m+1での最大かの判定
      ann=an

    
  else:
    for j in range(0,n+1): #jは切り取る位置
      if j==0:
        lsum=sorted(lis[N-n:])
        an=sum(lsum[i:])

        
      elif j==n:
        lsum=sorted(lis[:n])
        an=sum(lsum[i:])

      
      else:
        l1=lis[:j]
        l2=lis[N-n+j:]
        l3=l1+l2
        lsum=sorted(l3)
        an=sum(lsum[i:])

      if an>ann: #i0~m+1での最大かの判定
        ann=an

      

    
print(ann)

Submission status: Wrong Answer

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

import sys
INT_MIN = -1 * sys.maxsize

ans = INT_MIN

for left in range(0,K+1):
    for right in range(0, K+1-left):
        #print("========")
        #print(left, right)
        #print(V[:left], V[left:N-right], V[N-right:])
        
        rest_ope = K-left-right
        my_v = V[:left]+V[N-right:]
        sum_v = 0
        for i, x in enumerate(sorted(my_v)):
            if x < 0 and i < rest_ope:
                continue
            else:
                sum_v += x
        #print(sum_v)
        ans = max(ans, sum_v)
        
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
m=K//2 #mは捨てる回数の最大
lis=list(map(int,input().split())) #宝石リスト

ann=0 #annはi変化での最大
for i in range(0,m+1): #iは捨てる回数で場合分け
  n=K-i #nはとりあえず取る個数
  if n>=N: #とりあえず全部とれるとき
    lsum=sorted(lis)
    an=sum(lsum[i:]) #anはiの時の最大

    if an>ann: #i0~m+1での最大かの判定
      ann=an

    
  else:
    for j in range(0,n+1): #jは切り取る位置
      if j==0:
        lsum=sorted(lis[N-n:])
        an=sum(lsum[i:])

        
      elif j==n:
        lsum=sorted(lis[:n])
        an=sum(lsum[i:])

      
      else:
        l1=lis[:j]
        l2=lis[N-n+j:]
        l3=l1+l2
        lsum=sorted(l3)
        an=sum(lsum[i:])

      if an>ann: #i0~m+1での最大かの判定
        ann=an

      

    
print(ann)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = map(int, raw_input().split())
V = map(int, raw_input().split())
q = deque([])
nl = []
for i in range(min(K+1,N)):
	for j in range(i+1):
		q.append(V[:j]+V[N-i+j:])
	while q:
		q2 = q.popleft()
		q2.sort()
		qlen = len(q2)
		np = sum(q2)
		for j in range(min(qlen,K-i)):
			if(q2[j] < 0):
				np -=q2[j]
		nl.append(np)
print(max(nl))

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = map(int, raw_input().split())
V = map(int, raw_input().split())
q = deque([])
nl = []
for i in range(min(K+1,N)):
	for j in range(i+1):
		q.append(V[:j]+V[N-i+j:])
	while q:
		q2 = q.popleft()
		q2.sort()
		qlen = len(q2)
		np = i
		for j in range(qlen):
			if(j == K-i):
				np = j
				break
			elif(q2[j] >= 0):
				np = j
				break
		nl.append(sum(q2[np:]))
print(max(nl))

Submission status: Wrong Answer

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

vmax = 0
for l in range(0, K):
	for r in range(0, K - l + 1):
		if N < l + r:
			break
		tv = V[:l] + V[N - r:]
		#print(tv)
		tv.sort()
		for n in range(min(len(tv), K - (l + r))):
			if tv[n] < 0:
				tv[n] = 0
		vmax = max(vmax, sum(tv))

print(vmax)

Submission status: Wrong Answer

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

maxValue = 0

for i in range(min(N,K)+1):
    for j in range(min(N,K)-i+1):
        left = D[:i]
        right = D[N-j:]
        merge = (left + right + [0])
        merge.sort()

        index = 0

        for k in range(K-i-j):
            if (merge[0] < 0):
                index += 1

        val = sum(merge[index:])
        if(maxValue < val):
            maxValue = val

print(maxValue)

Submission status: Wrong Answer

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

maxValue = 0

for i in range(min(N,K)):
    for j in range(min(N,K)-i):
        if i == 0 and j == 0:
            continue
        left = D[:i]
        right = D[N-j:]
        merge = (left + right)
        merge.sort()

        for k in range(K-i-j):
            if(len(merge) == 0):
                break
            if (merge[0] < 0):
                merge.pop(0)

        val = sum(merge)
        if(maxValue < val):
            maxValue = val

print(maxValue)

Submission status: Wrong Answer

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

ans = - 10**7
for i in range(min(N,K)+1):
  for j in range(min(N,K)+1-i):
    if j == 0:
      st = A[:i]
    else:
      st = A[:i]+ A[-j:]
    st.sort(reverse = 1)
    for suteru in range(K - i - j):
      ans = max(ans, sum(st[:i+j-suteru]))
    
print(ans)

Submission status: Wrong Answer

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

ans = - 10**7
for i in range(min(N,K)+1):
  for j in range(min(N,K)+1-i):
    suteru = K - i - j
    if suteru < 0 or suteru > i +j :
      continue
    if j == 0:
      st = A[:i]
    else:
      st = A[:i]+ A[-j:]
    st.sort(reverse = 1)
    ans = max(ans, sum(st[:i+j-suteru]))
    
print(ans)

Submission status: Wrong Answer

Solution code:
from numpy import*
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n):
 for j in range(min(k,n)-i+1):m=max(m,sum(sort(v[:i]+v[:~j:-1])[k-i-j:]))
print(m)

Submission status: Wrong Answer

Solution code:
from numpy import*
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n):
 for j in range(k-i+1):t=sort(v[:i]+v[:~j:-1]);m=max(m,sum(t)-sum(t[t<0][:k-i-j]))
print(m)

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(K + 1):
    for r in range(K + 1 - l):
        if l + r <= N and 2 * l + 2 * r > K:
            jotai.append([l, r, K - l - r])

# print(jotai)

for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    # print(tmp)
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    # print(tmp)
    for k in range(jotai[i][2]):
        tmp.remove(min(tmp))
    # print(tmp)
    ans = max(sum(tmp), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(K + 1):
    for r in range(K + 1 - l):
        if l + r <= N and 2 * l + 2 * r > K:
            jotai.append([l, r, K - l - r])

print(jotai)

for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    # print(tmp)
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    # print(tmp)
    for k in range(jotai[i][2]):
        tmp.remove(min(tmp))
    # print(tmp)
    ans = max(sum(tmp), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())
v = np.array([int(i) for i in input().split()])
max_val = 0
for x in range(k+1):
    for y in range(k+1):
        if x+y >= k or x+y > len(v):
            break
        elif y == 0:
            stack = np.sort(v[:x])
        else:
            stack = np.sort(np.concatenate([v[:x], v[-y:]]))
        for z in range(max(0, k-(x+y))):
            if (len(stack) <= z) or (stack[z] >= 0):
                break
            else:
                stack[z] = 0
        max_val = max(max_val, sum(stack))
print(max_val)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())
v = np.array([int(i) for i in input().split()])
max_val = 0
for x in range(k+1):
    for y in range(k+1):
        if x+y >= k or x+y >= len(v):
            break
        elif y == 0:
            stack = np.sort(v[:x])
        else:
            stack = np.sort(np.concatenate([v[:x], v[-y:]]))
        for z in range(max(0, k-(x+y))):
            if (len(stack) <= z) or (stack[z] >= 0):
                break
            else:
                stack[z] = 0
        max_val = max(max_val, sum(stack))
print(max_val)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())
v = np.array([int(i) for i in input().split()])
max_val = 0
for x in range(k+1):
    for y in range(k+1):
        if x+y >= k:
            break
        elif y == 0:
            stack = np.sort(v[:x])
        else:
            stack = np.sort(np.concatenate([v[:x], v[-y:]]))
        for z in range(max(0, k-(x+y))):
            if (len(stack) <= z) or (stack[z] >= 0):
                break
            else:
                stack[z] = 0
        max_val = max(max_val, sum(stack))
print(max_val)

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(0, K):
    for r in range(0, K - l):
        if l + r <= N and 2 * l + 2 * r > K:
            jotai.append([l, r, K - l - r])

# print(jotai)

for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    # print(tmp)
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    # print(tmp)
    for k in range(jotai[i][2]):
        tmp.remove(min(tmp))
    # print(tmp)
    ans = max(sum(tmp), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(0, K):
    for r in range(0, K - l):
        if l + r <= N and 2 * l + 2 * r >= K:
            jotai.append([l, r, K - l - r])
for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    for k in range(jotai[i][2]):
        tmp.remove(min(tmp))
    ans = max(sum(tmp), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
v_list = list(map(int, input().split()))
ans = 0
for i in range(1, min(n,k)+1):
    for j in range(i+1):
        cnt = min(n,k) - i
        tmp_list = []
        if j == 0:
            tmp_list += v_list[-i:]
        elif j == i:
            tmp_list += v_list[:i]
        else:
            tmp_list += v_list[:j]
            tmp_list += v_list[-(i-j):]
        tmp_list.sort()
        tmp_sum = sum(tmp_list)
        for tmp in tmp_list:
            if cnt <= 0:
                break
            if tmp < 0:
                tmp_sum -= tmp
                cnt -= 1
            else:
                break
        ans = max(ans, tmp_sum)
print(ans)

Submission status: Wrong Answer

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

ans = []
for k_l in range(K+1):
    rem = K - k_l
    for k_r in range(rem+1):
        rem_ = rem-k_r
        v = V[:k_l]+V[N-k_r:]
        #print(k_l,k_r,rem_,v)
        v.sort()
        for i in range(rem_):
            if i < len(v) and v[i] < 0:
                v[i] = 0
        ans.append(sum(v))

print(max(ans))

Submission status: Wrong Answer

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

ans = []
for k_l in range(K+1):
    rem = K - k_l
    for k_r in range(rem+1):
        rem_ = rem-k_r
        v = V[:k_l]+V[N-k_r:]
        #print(k_l,k_r,rem_,v)
        v.sort()
        for i in range(min(rem_,len(v))):
            if v[i] < 0:
                v[i] = 0
        ans.append(sum(v))

print(max(ans))