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 heapq import heappop, heappush
from collections import deque
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
Q = deque()
ans = 0
R = min(N,K)
for l in range(R):
  H = []
  if l >= 1:
    for i in range(l):
      heappush(H,V[i])
  for r in range(R-l):
    if r >= 1:
      for i in range(r):
        heappush(H,V[N-1-i])
    ans = max(ans,sum(H))
    for i in range(min(len(H),K-l-r)):
      Q.append(heappop(H))
      ans = max(ans,sum(H))
print(ans)

Submission status: Wrong Answer

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

m=[]
for i in range(k+1):
 for j in range(k-i+1):
  # print(i,j)
  t=sorted(v[:i]+v[:~j:-1])

  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m.append(sum(t))
print(max(m))

Submission status: Wrong Answer

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

m=[]
for i in range(k):
 for j in range(min(k,n)-i+1):
  # print(i,j)
  t=sorted(v[:i]+v[:~j:-1])

  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m.append(sum(t))
print(max(m))

Submission status: Wrong Answer

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

m=[]
for i in range(k):
 for j in range(min(k,n)-i+1):
  # print(i,j)
  t=sorted(v[:i]+v[:~j:-1])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m+=sum(t),
print(max(m))

Submission status: Wrong Answer

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

ans = 0
k = min(n,k)
for i in range(k+1): 
    for j in range(k-i+1):

        left_gain = jewel_list[:j]
        if k-i-j != 0:
            right_gain = jewel_list[-k+i+j:]
        else:
            right_gain = []
        
        gabage = sorted(left_gain+right_gain)[:i]

        total = sum(left_gain+right_gain)
        for p in gabage:
            if p < 0:
                total -= p
            else:
                break
        ans = max(ans,total)

print(ans)

Submission status: Wrong Answer

Solution code:

n, k = [ int(v) for v in input().split() ]
jewel_list = [ int(v) for v in input().split() ]

ans = 0
for i in range(k+1): 
    for j in range(k-i+1):

        left_gain = jewel_list[:j]
        if k-i-j != 0:
            right_gain = jewel_list[-k+i+j:]
        else:
            right_gain = []
        
        gabage = sorted(left_gain+right_gain)[:i]

        total = sum(left_gain+right_gain)
        for p in gabage:
            if p < 0:
                total -= p
            else:
                break
        ans = max(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(min(N, K)):
    for j in range(i + 1):
        for k in range(K - i + 1):
            ans = max(ans, sum(sorted(V[:i - j] 
                                      + list(reversed(V))[:j])[k:]))
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n, k = list(map(int, input().split()))
que = list(map(int, input().split()))
ans = 0
pop_push = [(i, k-i) for i in range(k, math.ceil(k/2)-1, -1)]
for pop, push in pop_push:
    left_right = [(j, pop-j) for j in range(pop+1)]
    for left, right in left_right:
        ans = max(ans, sum(sorted(que[:left] + list(reversed(que))[:right])[push:]))
print(ans)

Submission status: Wrong Answer

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

m=min(n,k)
max_=0

for i in range(m+1):
    for j in range(m+1-i):
        score=0
        hold=[]
        trash=[]
        if i!=0:
            for a in range(i):
                score+=v[a]
                hold.append(v[a])
        if j!=0:
            for b in range(j):
                score+=v[-b-1]
                hold.append(v[-b-1])
        trash=sorted(hold)
            
        for _ in range(min(m-i-j,i+j)):
            if len(trash)>0:
                if trash[0]<0:
                    score-=trash[0]
                    del trash[0]
            
        if max_<score:
            max_=score
            
print(max_)

Submission status: Wrong Answer

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

m=min(n,k)
max_=0

for i in range(m+1):
    for j in range(m+1-i):
        score=0
        hold=[]
        trash=[]
        if i!=0:
            for a in range(i):
                score+=v[a]
                hold.append(v[a])
        if j!=0:
            for b in range(j):
                score+=v[-b-1]
                hold.append(v[-b-1])
        trash=sorted(hold)
            
        for _ in range(min(m-i-j,i+j)):
            if trash[0]<0:
                score-=trash[0]
                del trash[0]
            
        if max_<score:
            max_=score
            
print(max_)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = list(map(int, input().split()))
global ans
#m残り捜査回数, hand 手に持っている宝石 board :残っている宝石
ans = 0
def get(n, plus_hand, mainus_hand, board, s):
    if s>0:
        plus_hand.append
    else:
        mainus_hand.append
    global ans
    mainus_hand.sort()
    ans = max(ans, sum(plus_hand) + sum(mainus_hand[n:]))
    if n > 0 and len(board) > 0:
        get(n-1, plus_hand,  mainus_hand, board[1:], board[0])
        get(n-1, plus_hand,  mainus_hand, board[:-1], board[-1])
        
get(k, [], [], li, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import collections

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

ans = 0
for a in range(min(N, K)):
    for b in range(min(N, K)-a):
        score = 0
        D = sorted(V[:a]+V[N-b:])
        score = sum(D[min(K-(a+b), bisect.bisect_left(D, 0)):])
        # print(score)
        ans = max(ans, score)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
U = V[::-1]
ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        hold = V[:l] + U[:r]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
U = V[::-1]
ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        hold = V[:l] + U[r:]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

Submission status: Wrong Answer

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

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        hold = V[:l] + V[N-r:]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = V[:l] + V[N-r:]
    hold.sort()
    return sum(hold[d:]) 

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        for d in range(K+1-l-r):
            ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = V[:l] + V[r:]
    hold.sort()
    return sum(hold[d:]) 

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        for d in range(K+1-l-r):
            ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-r, N):
        hold.append(V[i])
    hold.sort()
    return sum(hold[d:]) 

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        for d in range(K+1-l-r):
            ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-r, N):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        for d in range(K+1-l-r):
            ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-r, N):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
K = min(N, K)
for l in range(K+1):
    for r in range(K+1-l):
        d = K - l - r
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = []
    for i in range(min(N,l)):
        hold.append(V[i])
    for i in range(N-1, max(N-1-r,-1), -1):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = K - l - r
        if d < 0:
            assert()
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

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

def calc(l, r, d):
    hold = []
    for i in range(min(N,l)):
        hold.append(V[i])
    for i in range(N-1, max(N-1-r,-1), -1):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = max(K - l - r, 0)
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
inputs = [int(i) for i in input().split()]

ans = 0
for left in range(K+1):
    #print('left', left)
    for right in range(K+1-left):
        if left + right > K:
            continue
        stay = K - left - right
        # print(right, left, stay)
        tmp = inputs[:left] + inputs[::-1][:right]
        tmp.sort()
        total = sum(tmp)
        if stay <= len(tmp):
            tmp = tmp[:stay]
        for item in tmp:
            if item < 0:
                total -= item
            else:
                break
        ans = max(ans, total)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
inputs = [int(i) for i in input().split()]

ans = 0
for left in range(K+1):
    #print('left', left)
    for right in range(K+1-left):
        if left + right > K:
            continue
        # print(right, left)
        stay = K - left - right
        tmp = inputs[:left] + inputs[::-1][:right]
        tmp.sort()
        total = sum(tmp)
        for i in range(stay):
            if i >= len(tmp):
                break 
            if tmp[i] < 0:
                total -= tmp[i]
            else:
                break
        ans = max(ans, total)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
inputs = [int(i) for i in input().split()]

ans = 0
for left in range(K+1):
    for right in range(K-left):
        if left + right > K:
            continue
        stay = K - left - right
        end = N if -(right+1) == -1 else -(right+1)
        tmp = inputs[:left] + inputs[::-1][:right]
        tmp.sort()
        total = sum(tmp)
        for i in range(stay):
            if i >= len(tmp):
                break 
            if tmp[i] < 0:
                total -= tmp[i]
            else:
                break
        ans = max(ans, total)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().strip().split())
V = list(map(int, input().strip().split()))
H = []
maxi = 0
if N < K:
    for i in range(N):
        for o in range(i):
            ts = 0
            hand = []
            for p in range(o):
                hand.append(V[p])
            for p in range(N-(i-o), N):
                hand.append(V[p])
            for values in hand:
                ts += values
            if ts > maxi:
                maxi = ts
            if len(hand) > 0:
                hand.sort()
                for j in range(K-i):
                    if len(hand) > 0:
                        if hand[0] < 0:
                            del hand[0]
                        else:
                            break
            ts = 0
            for k in hand:
                ts += k
            if ts > maxi:
                maxi = ts
else:
    for i in range(K):
        for o in range(i):
            ts = 0
            hand = []
            for p in range(o):
                hand.append(V[p])
            for p in range(N-(i-o), N):
                hand.append(V[p])
            for values in hand:
                ts += values
            if ts > maxi:
                maxi = ts
            if len(hand) > 0:
                hand.sort()
                for j in range(K-i):
                    if len(hand) > 0:
                        if hand[0] < 0:
                            del hand[0]
                        else:
                            break
            ts = 0
            for k in hand:
                ts += k
            if ts > maxi:
                maxi = ts
print(maxi)

Submission status: Wrong Answer

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

N = N + 1 # 選択しなかった場合を追加
res = 0
AB = []
if N < K:
	AB = V
	k = min(K-len(AB), len(AB))
	for idx in range(k):
		if AB[idx] < 0:
			AB[idx] = 0
	res = max(res, sum(AB))
else:
	for a in range(N):
		for b in range(N):
			if a + b <= K:
				AB = V[:a] + revV[:b]
				AB.sort()
				k = min(K-len(AB), len(AB))
				for idx in range(k):
					if AB[idx] < 0:
						AB[idx] = 0
				res = max(res, sum(AB))
				print(a,b,AB)
print(res)

Submission status: Wrong Answer

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

N = N + 1 # 選択しなかった場合を追加
res = 0
AB = []
if N < K:
	AB = V
	k = min(K-len(AB), len(AB))
	for idx in range(k):
		if AB[idx] < 0:
			AB[idx] = 0
	res = max(res, sum(AB))
else:
	for a in range(N):
		for b in range(N):
			if a + b < K:
				AB = V[:a] + revV[:b]
				AB.sort()
				k = min(K-len(AB), len(AB))
				for idx in range(k):
					if AB[idx] < 0:
						AB[idx] = 0
				res = max(res, sum(AB))
print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
total = 0
for a in range(K+1):
	A = V[:a]
	
	for b in range(K+1):
		B = list(reversed(V))[:b]
		lis = A + B
		if len(lis) == 0:
			continue
		if len(lis) <= K:
			cd = min(K - len(lis), len(lis))
			lis.sort()
			for i in range(cd):
				if lis[i] < 0:
					lis[i] = 0
			total = max(sum(lis), total)
print(total)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
for i in range(min(N,K)+1): # Dequeueからとる宝石の数
    for j in range(i+1):  # 左からとる宝石の数
        Juel = V[:j]+V[N-i+j:]
        Juel.sort()
        sum_v = 0
        for k in range(min(K-i,j)):
            Juel[k] = max(Juel[k],0)
        ans = max(ans,sum(Juel))
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(min(N,K)+1):
    for j in range(i+1):
        Juel = V[:j]+V[N-i+j:]
        Juel.sort()
        sum_v = 0
        for k in range(min(K-i,j)):
            if Juel[k] < 0:
                Juel[k] = 0
        ans = max(ans,sum(Juel))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > K: break
            if l+r == 0: continue
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort(reverse=True)

            restK = K - (l+r)

            for _ in range(restK):
                if len(having)!=0 and having[-1] < 0:
                    having.pop()
                    continue
                break

            ans = max(ans, sum(having))
    
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > K: break
            if l+r == 0: continue
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort()

            restK = K - (l+r)
            j = 0
            for i in range(restK):
                if having[i] < 0:
                    j += 1
                break

            ans = max(ans, sum(having[j:]))
    
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > K: break
            if l+r == 0: break
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort()

            restK = K - (l+r)
            j = 0
            for i in range(restK):
                if having[i] < 0:
                    j += 1
                break

            ans = max(ans, sum(having[j:]))
    
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
	n, k = map(int, input().strip().split())
	v = list(map(int, input().strip().split()))
	ans = 0
	for a in range(k+1):
		for b in range(k-a+1):
			t = sorted(v[:a] + v[n-b:])
			for i in range(k-a-b):
				if i < len(t) and t[i] < 0:
					t[i] = 0
			ans = max(ans, sum(t))
	print(ans)
if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
	n, k = map(int, input().strip().split())
	v = list(map(int, input().strip().split()))
	ans = 0
	for a in range(k):
		for b in range(k-a):
			t = sorted(v[:a] + v[n-b:])
			for i in range(k-a-b):
				if i < len(t) and t[i] < 0:
					t[i] = 0
			ans = max(ans, sum(t))
	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
sz = min(N,K)
for a in range(sz+1):
	for b in range(sz+1-a):
		#print('{} {}'.format(V[:a],V[N-b:]))
		taken = V[:a]+V[N-b:]
		taken.sort()
		for c in range(min(sz+1-a-b,a+b)):
			score = sum(taken[c:])
			#print('   ({}) {}'.format(score,taken[c:]))
			ans = max(ans,score)
print(ans)

Submission status: Wrong Answer

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

ans = 0
sz = min(N,K)
for a in range(sz+1):
	for b in range(sz+1-a):
		#print('{} {}'.format(V[:a],V[N:N-b-1:-1]))
		taken = V[:a]+V[N:N-b-1:-1]
		taken.sort()
		for c in range(sz+1-a-b):
			score = sum(taken[c:])
			ans = max(ans,score)
print(ans)

Submission status: Wrong Answer

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

def maximize(p):
	#print('  maximize: {}'.format(p))
	sz = len(p)
	best = 0
	for m in range((sz+1)//2):
		temp = sorted(p[:sz-m])
		score = sum(temp[m:])
		best = max(best,score)
		#print('    score {} = sum of {}'.format(score,temp[m:]))
	return best

ans = 0
for b in range(min(N,K)+1):
	p = V[:min(N,K)-b].copy()
	q = V[N:N-b-1:-1].copy()
	#print('{} {}'.format(p,q))
	score1 = maximize(p)
	score2 = maximize(q)
	#print('  {} + {} = {}'.format(score1,score2,score1+score2))
	ans = max(ans,score1+score2)
print(ans)

Submission status: Wrong Answer

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

if N < K:
	print('N < K')
	exit()

def maximize(p):
	#print('  maximize: {}'.format(p))
	sz = len(p)
	best = 0
	for m in range((sz+1)//2):
		temp = sorted(p[:sz-m])
		score = sum(temp[m:])
		best = max(best,score)
		#print('    score {} = sum of {}'.format(score,temp[m:]))
	return best

ans = 0
for b in range(K+1):
	p = V[:K-b].copy()
	q = V[N:N-b-1:-1].copy()
	#print('{} {}'.format(p,q))
	score1 = maximize(p)
	score2 = maximize(q)
	#print('  {} + {} = {}'.format(score1,score2,score1+score2))
	ans = max(ans,score1+score2)
print(ans)

Submission status: Wrong Answer

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

ans = 0
max_ope = min(N, K)
for put in range(K+1):
    if put > K//2:
        break
    get = min(K-put, N)
    for right in range(get+1):
        left = get - right
        jewels = A[:right] + A[N-left:]
        jewels.sort()
        value = sum(jewels)
        cnt = 0
        for jewel in jewels:
          if cnt > put:
            break
          if jewel < 0:
            value += abs(jewel)
            cnt += 1
        ans = max(ans, value)
print(ans)

Submission status: Wrong Answer

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

ans = 0
max_ope = min(N, K)
for put in range(K+1):
    if put > K//2:
        break
    get = min(K-put, N)
    for right in range(get+1):
        left = get - right
        jewels = A[:right] + A[N-left:]
        jewels.sort()
        value = sum(jewels)
        cnt = 0
        for jewel in jewels:
          if cnt > put:
            break
          if jewel < 0:
            value += abs(jewel)
        ans = max(ans, value)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for put in range(K):
    if put > K//2:
        break
    get = K - put
    for right in range(get+1):
        left = get - right
        jewels = A[:right] + A[N-left:]
        jewels.sort()
        value = sum(jewels)
        for i in range(put):
            if i >= len(jewels):
                break
            if jewels[i] < 0:
                value += abs(jewels[i])
            else:
                break
        ans = max(ans, value)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                store.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(min(K+1,N+1)):
    for l in range(k+1):
        for r in range(k+1-l):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                store.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(min(K+1,N+1)):
    for l in range(min(K+1,N+1)):
        for r in range(min(K+1,N+1)-l):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                store.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(min(K+1,N+1)):
    for l in range(min(K+1,N+1)):
        for r in range(min(K+1,N+1)):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)

Submission status: Wrong Answer

Solution code:
import bisect


def do_action(Vs, Vs_in_D, rest):
    if rest == 0:  # もう操作できないとき
        return 0

    do_AB = True
    for values in Vs_in_D:  # 筒の中に価値があるものがなければ操作A,Bをしないのが最善
        if values > 0:
            break
    else:
        do_AB = False

    if Vs_in_D and do_AB:  # Dが空ではないとき
        Vs_A = Vs[:]
        bisect.insort_left(Vs_A, Vs_in_D[0])
        patternA = do_action(Vs_A, Vs_in_D[1:], rest-1) + Vs_in_D[0]  # 操作A
        Vs_B = Vs[:]
        bisect.insort_left(Vs_B, Vs_in_D[-1])
        patternB = do_action(Vs_B, Vs_in_D[:-1], rest-1) + Vs_in_D[-1]  # 操作B
    else:
        patternA = 0  # 操作A
        patternB = 0  # 操作B

    do_CD = False
    for values in Vs_in_D:  # 手持ちにマイナスのものがなければ操作C,Dをしないのが最善
        if values < 0:
            break
    else:
        do_CD = True

    if Vs and do_CD:  # 宝石を持っていたら
        patternC = do_action(Vs[1:], [Vs[0]]+Vs_in_D, rest-1) - Vs[0]   # 操作C
        patternD = do_action(Vs[1:], Vs_in_D+[Vs[0]], rest-1) - Vs[0]  # 操作D
    else:
        patternC = 0  # 操作C
        patternD = 0  # 操作D
    return max(max(patternA, patternB), max(patternC, patternD))


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

print(do_action([], V_list, K))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
#C,D(宝石を手放す)は最後にまとめてよい
#A,Bを行う回数a,b
#取り出せるもののうちできるだけ負かつ
#その絶対値が大きいものを取り出したい
nk=min(n,k)
discard=0
ans=0
for a in range(nk+1):
    for b in range(nk+1):
        if a+b>nk:
            break
        discard=k-(a+b)     #捨てる回数
        dis=v[:a]+v[n-b:]   #両端から取り出すリスト
        minus=[i for i in dis]  #取り出すもののうち負のもの
        minus.sort()
        ans=max(sum(dis)-sum(minus[:min(discard,len(minus))]),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

lesser = min(N, K)
ans = 0
for A in range(lesser + 1):
    for B in range(lesser + 1):
        if A + B > lesser:
            continue
        holding = V[:A] + V[N - B:]
        heapq.heapify(holding)
        S = sum(holding)
        CD = lesser - (A + B)
        for i in range(min(A + B, CD)):
            p = heapq.heappop(holding)
            if p >= 0:
                break
            S -= p
        ans = max(ans, S)
print(ans)

Submission status: Wrong Answer

Solution code:

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)


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

max_num = 0
V_left = [0,V[0]]
V_right = [0,V[N - 1]]
V_left_m = [[] for i in range(N + 1)]
V_right_m = [[] for i in range(N + 1)]
for i in range(N - 1):
    V_left.append(V_left[-1] + V[i + 1])
    V_right.append(V_right[-1] + V[N - 2 - i])

for i in range(1, N+1):
    if i == 0:
        minus_l = []
        minus_r = []
    else:
        minus_l = copy.deepcopy(V_left_m[i - 1])
        minus_r = copy.deepcopy(V_right_m[i - 1])
    if V[i - 1] < 0:
        minus_l.append(V[i - 1])
    V_left_m[i] = minus_l
    if V[N  - i] < 0:
        minus_r.append(V[N - i])
    V_right_m[i] = minus_r

for k in range(1, min(N, K) + 1):
    for i in range(0, k + 1):
        point = V_left[i] + V_right[k - i]
        minus = set(V_left_m[i]) | set(V_right_m[k - 1])
        minus = list(minus)
        minus = sorted(minus)
        point -= sum(minus[0:K - k])
        #print(point, i, k - i, minus, minus[0:K - k], K - k)
        if point > max_num:
            max_num = point
           # print(point, i, k-i,minus, minus[0:K-k], K-k)

print(max_num)