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:
global k
global maximam
n, k = map(int, input().split())

deq_li = list(map(int, input().split()))
hand_li =[]

maximam = 0
def a_b(deq, hand, n, a_b_checker):
    
    hand.append(deq.pop(a_b_checker))
    global maximam, k
    hand_li.sort()
    
    if n > 0 and len(deq) != 0:
        a_b(deq.copy(), hand.copy(), n-1, 0)
        a_b(deq.copy(), hand.copy(), n-1, -1)
    sums = sum(hand[n:])
    
    if maximam < sums:
        maximam = sums
    
    

a_b(deq_li.copy(), hand_li.copy(), k-1, 0)
a_b(deq_li.copy(), hand_li.copy(), k-1, -1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
from collections import deque
v = deque()
for i in V:
    v.append(i)
res=0
for i in range(min(N,K)):
    for j in range(min(N,K)-i):
        cur = list()
        for f in V:
          v.append(f)
        ans=0
        for p in range(i):
            cur.append(v.popleft())
        for q in range(j):
            cur.append(v.pop())
        sorted(cur,reverse= True)
        for n in range(min(K-i-j,len(cur))):
            if cur[n]>0:
                ans +=cur[n]
            else:
                break
        res = max(ans,res)
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import bisect

inf = float('inf')

def solve(N: int, K: int, V: "List[int]"):
    res = -inf
    for k in range(K+1):
        for left in range(k+1):
            right = k - left
            L = V[:left] + V[N-right:]
            L.sort()
            i = min(bisect.bisect(L, 0), K - k)
            res = max(res, sum(L[i:]))
    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

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(max_ope+1):
    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:
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = 0
for put in range(K+1):
    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:
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = 0
for get in range(K+1):
    put = K - get
    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:
n,k = map(int,input().split())
v = list(map(int,input().split()))

ans = 0
for i in range(k+1):
    for j in range(k+1-i):
        for l in range(k+1-i-j):
            can = v[0:i]+v[n-j:n]
            can = sorted(can,reverse=True)
            ans = max(ans,sum(can[0:len(can)-k]))
print(ans)

Submission status: Wrong Answer

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

ans = 10**(-15)
for i in range(k+1):
    for j in range(k+1-i):
        for l in range(k+1-i-j):
            can = v[0:i]+v[n-j:n]
            can = sorted(can,reverse=True)
            ans = max(ans,sum(can[0:len(can)-k]))
print(ans)

Submission status: Wrong Answer

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

ans = -1e9
for leftops in range(min(N,K)):
  for rightops in range(min(N,K)-leftops):
    X = V[:leftops] + V[-rightops:] if rightops > 0 else V[:leftops]
    work = sum(X)
    #print(leftops,rightops,work)
    #print(X)
    X.sort()
    
    if len(X) == 0:
      ans = max(ans,0)
      continue
      
    for k in range(min(K-leftops-rightops,len(X))):
      if X[k] < 0:
        work -= X[k]
      else:
        break
    ans = max(ans,work)
    
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 >= 2 * N:
    ans = sum([v for v in V if v >= 0])
print(ans)

Submission status: Wrong Answer

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

def larger(x, y):
	if x > y:
		return x
	return y

N, K = map(int, input().split())
V = list(map(int, input().split()))
sumlefttoright = [[0, []]]
sumrighttoleft = [[0, []]]
for i in range(0, N):
	sum1 = sumlefttoright[-1][0] + V[i]
	sum2 = sumrighttoleft[-1][0] + V[-1-i]
	neg1 = []
	neg2 = []
	if V[i] < 0:
		neg1.append(V[i])
	if V[-1-i] < 0:
		neg2.append(V[-1-i])
	sumlefttoright.append([sum1, sumlefttoright[-1][1]+neg1])
	sumrighttoleft.append([sum2, sumrighttoleft[-1][1]+neg2])
max = 0
for i in range(0, smaller(N, K)+1):
	for j in range(0, smaller(N, K)+1-i):
		sum = sumlefttoright[i][0] + sumrighttoleft[j][0]
		negs = sumlefttoright[i][1] + sumrighttoleft[j][1]
		negs.sort()
		for k in range(0, smaller(smaller(N, K)-i-j, len(negs))):
			sum -= negs[k]
		max = larger(max, sum)
print(max)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0

ans = 0

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,upper+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        up = min(k-i,j)
        for l in range(up):
            if a[l] < 0:
                v -= a[l]
            else:
                break
        if v > ans:
            ans = v

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0

ans = 0

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,upper+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        if k-i > 0:
            up = min(k-i,i)
            for l in range(up):
                if a[l] < 0:
                    v -= a[l]
                else:
                    break
        if v > ans:
            ans = v

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def main():
	n, k = map(int, input().strip().split())
	v = deque(map(int, input().strip().split()))
	ans = 0
	for i in range(k):
		for j in range(k - i + 1):
			now = 0
			s = []
			if i + j > n:
				continue
			md = k - i - j
			for t in range(i):
				s.append(v[t])
				now += v[t]
			for t in range(j):
				s.append(v[n-1-t])
				now += v[n-1-t]
			s.sort()
			# for t in range(min(md, i+j)):
			# 	# print("t:{}, len:{}".format(t, len(s)))
			# 	if s[t] >= 0:
			# 		break
			# 	else:
			# 		s[t] = 0
			for t in range(md):
				if t >= len(s):
					break
				if s[t] >= 0:
					break
				now -= s[t]
			ans = max(now, ans)

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def main():
	n, k = map(int, input().strip().split())
	v = deque(map(int, input().strip().split()))
	ans = 0
	for i in range(k):
		for j in range(k - i):
			now = 0
			s = []
			if i + j > n:
				continue
			md = k - i - j
			for t in range(i):
				s.append(v[t])
				now += v[t]
			for t in range(j):
				s.append(v[n-1-t])
				now += v[n-1-t]
			s.sort()
			# for t in range(min(md, i+j)):
			# 	# print("t:{}, len:{}".format(t, len(s)))
			# 	if s[t] >= 0:
			# 		break
			# 	else:
			# 		s[t] = 0
			for t in range(md):
				if t >= len(s):
					break
				if s[t] >= 0:
					break
				now -= s[t]
			ans = max(now, ans)

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def main():
	n, k = map(int, input().strip().split())
	v = deque(map(int, input().strip().split()))
	ans = 0
	for i in range(k):
		for j in range(k - i):
			s = []
			if i + j > n:
				continue
			md = k - i - j
			for t in range(i):
				s.append(v[t])
			for t in range(j):
				s.append(v[n-1-t])
			s.sort()
			for t in range(min(md, i+j)):
				# print("t:{}, len:{}".format(t, len(s)))
				if s[t] >= 0:
					break
				s[t] = 0
			ans = max(sum(s), ans)

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    if i<=n:
        for j in range(i+1):
            tmp=[]
            # print("{0} {1}".format(i,j))
            for l in range(j):
                if l<n:
                    tmp.append(a[l])
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
            print(tmp)
            tmp.sort(reverse=True)
            # print(tmp)
            for m in range(k-i):
                if len(tmp)>0:
                    if tmp[-1]<0:
                        tmp.pop()
            ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    if i<=n:
        for j in range(i+1):
            tmp=[]
            # print("{0} {1}".format(i,j))
            for l in range(j):
                if l<n:
                    tmp.append(a[l])
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
            # print(tmp)
            tmp.sort(reverse=True)
            # print(tmp)
            for m in range(k-i):
                if len(tmp)>0:
                    tmp.pop()
            ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    for j in range(i+1):
        tmp=[]
        # print("{0} {1}".format(i,j))
        for l in range(j):
            if l<n:
                tmp.append(a[l])
        if i+1<n:
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
        # print(tmp)
        tmp.sort(reverse=True)
        # print(tmp)
        for m in range(k-i):
            if len(tmp)>0:
                tmp.pop()
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    for j in range(i+1):
        tmp=[]
        # print("{0} {1}".format(i,j))
        for l in range(j):
            if l<n:
                tmp.append(a[l])
        if i<n:
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
        # print(tmp)
        tmp.sort(reverse=True)
        # print(tmp)
        for m in range(k-i):
            if len(tmp)>0:
                tmp.pop()
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    for j in range(i+1):
        tmp=[]
        # print("{0} {1}".format(i,j))
        for l in range(j):
            if l<n:
                tmp.append(a[l])
        for r in range(i-j):
            if n-1-r>=0:
                tmp.append(a[-1-r])
        # print(tmp)
        tmp.sort(reverse=True)
        # print(tmp)
        # tmp.pop()
        # print(tmp)
        for m in range(k-i):
            if len(tmp)>0:
                tmp.pop()
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    N, K = i2nn()
    V = i2nn()
    im = min(N+1, K+1)
    n = 0
    for i in range(im):
        jm = min(N+1, K+1-i)
        for j in range(jm):
            k = K - i - j
            w = V[0:i] + V[N-j:N]
            w.sort()
            t = 0
            for u in w:
                if u < 0 and k > 0:
                    k -= 1
                    continue
                t += u
            if n < t:
                n = t
    print(n)

main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    N, K = i2nn()
    V = i2nn()
    im = min(N+1, K)
    n = 0
    for i in range(im):
        jm = min(N+1, K-i)
        for j in range(jm):
            k = K - i - j
            w = V[0:i] + V[N-j:N]
            w.sort()
            t = 0
            for u in w:
                if u < 0 and k > 0:
                    k -= 1
                    continue
                t += u
            if n < t:
                n = t
    print(n)

main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    N, K = i2nn()
    V = i2nn()
    im = min(N+1, K)
    n = 0
    for i in range(im):
        jm = min(N+1, K-i)
        for j in range(jm):
            k = K - i - j
            w = V[0:i] + V[N-j:N]
            w.sort()
            t = 0
            if k == 0:
                t = sum(w)
            elif k > len(w) or w[k-1] >= 0:
                for u in w:
                    if u > 0:
                        t += u
            else:
                t = sum(w[k:])
            if n < t:
                n = t
    print(n)

main()

Submission status: Wrong Answer

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

def f(a):
    res = [0] * (k + 1)
    for i in range(1, k + 1):
        tmp = 0
        for j in range(i):
            hand = a[:i - j]
            negative = [h for h in sorted(hand) if h < 0]
            tmp = max(tmp, sum(hand) - sum(negative[:j]))
        res[i] = tmp

    return res


left = f(v)
right = f(list(reversed(v)))

ans = 0
for i in range(k + 1):
    ans = max(ans, left[k - i] + right[i])

print(ans)

Submission status: Wrong Answer

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

def f(a):
    res = [0] * (k + 1)
    for i in range(1, k + 1):
        tmp = 0
        for j in range(i):
            hand = a[:i - j]
            tmp = max(tmp, sum(hand) - sum(sorted(hand)[:j]))
        res[i] = max(tmp, res[i - 1])

    return res


left = f(v)
right = f(list(reversed(v)))

ans = 0
for i in range(k + 1):
    ans = max(ans, left[k - i] + right[i])

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
#C,Dは最後でよし
r=min(n,k)
ans_list=[]
for i in range(-1,len(v)):
    #print("----"+str(i)+"-------")
    for j in reversed(range(len(v)+1)):
        if (i+1)+(len(v)-1-j)>r:
            #print("break",i+(len(v)-j),r)
            break
        get=[]
        right = len(v)-1-j
        if i!=-1:
            get+=v[:i+1]
        if j!=len(v):
            get+=v[j+1:]
        else:
            right=0
        get=sorted(get)[::-1]
        #print(get)
        res = k-((i+1)+right)
        if res<0:
            res=0
        #print(res,i+1,right,j)
        for l in range(res):
            if len(get)==0:
                break
            if get[len(get)-1]<0:
                #print(get[len(get)-1])
                get.pop()
            else:
                #print(get[len(get)-1])
                break
        ans_list.append(sum(get))
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
#C,Dは最後でよし
r=min(n,k)
ans_list=[]
for i in range(-1,len(v)):
    #print("----"+str(i)+"-------")
    for j in reversed(range(len(v)+1)):
        if (i+1)+(len(v)-1-j)+1>r:
            #print("break",i+(len(v)-j),r)
            break
        get=[]
        if i!=-1:
            get+=v[:i+1]
        if j!=len(v)+1:
            get+=v[j+1:]
        get=sorted(get)[::-1]
        #print(get)
        res = k-(i+(len(v)-j))
        if res<0:
            res=0
        #print(res)
        for l in range(res):
            if len(get)==0:
                break
            if get[len(get)-1]<0:
                #print(get[len(get)-1])
                get.pop()
            else:
                #print(get[len(get)-1])
                break
        ans_list.append(sum(get))
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
#C,Dは最後でよし
r=min(n,k)
ans_list=[]
for i in range(-1,len(v)):
    print("----"+str(i)+"-------")
    for j in reversed(range(len(v)+1)):
        if (i+1)+(len(v)-1-j)+1>r:
            #print("break",i+(len(v)-j),r)
            break
        get=[]
        if i!=-1:
            get+=v[:i+1]
        if j!=len(v)+1:
            get+=v[j+1:]
        get=sorted(get)[::-1]
        print(get)
        res = k-(i+(len(v)-j))
        if res<0:
            res=0
        print(res)
        for l in range(res):
            if len(get)==0:
                break
            if get[len(get)-1]<0:
                #print(get[len(get)-1])
                get.pop()
            else:
                #print(get[len(get)-1])
                break
        ans_list.append(sum(get))
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
#C,Dは最後でよし
r=min(n,k)
ans_list=[]
for i in range(-1,len(v)):
    #print("----"+str(i)+"-------")
    for j in reversed(range(len(v)+1)):
        if (i+1)+(len(v)-1-j)+1>r:
            #print("break",i+(len(v)-j),r)
            break
        get=[]
        if i!=-1:
            get+=v[:i+1]
        if j!=len(v)+1:
            get+=v[j+1:]
        get=sorted(get)[::-1]
        #print(get)
        res = k-(i+(len(v)-j)+1)
        if res<0:
            res=0
        #print(res)
        for l in range(res):
            if len(get)==0:
                break
            if get[len(get)-1]<0:
                #print(get[len(get)-1])
                get.pop()
            else:
                #print(get[len(get)-1])
                break
        ans_list.append(sum(get))
print(max(ans_list))

Submission status: Wrong Answer

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

ans = 0
if N > K:
    handl = []
    for a in range(K+1):
        if a > 0:
            handl.append(V[a-1])
        handr = []
        for b in range(K-a+1):
            if b > 0:
                handr.append(V[-b])
            
            hand = [0] + handl + handr
            hand.sort(reverse=True)
            
            if K > a + b:
                c = 0
                for h in hand[::-1]:
                    if h < 0:
                        hand.pop()
                        c += 1
                        if c >= K - (a + b):
                            break
                ans = max(ans, sum(hand))
else:
    handl = []
    for a in range(N+1):
        if a > 0:
            handl.append(V[a-1])
        handr = []
        for b in range(N-a+1):
            if b > 0:
                handr.append(V[-b])
            
            hand = [0] + handl + handr
            hand.sort(reverse=True)
            
            if K > a + b:
                c = 0
                for h in hand[::-1]:
                    if h < 0:
                        hand.pop()
                        c += 1
                        if c >= K - (a + b):
                            break
                ans = max(ans, sum(hand))
    
print(ans)

Submission status: Wrong Answer

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

ans = 0
if N > K:
    handl = []
    for a in range(K+1):
        if a > 0:
            handl.append(V[a-1])
        handr = []
        for b in range(K-a+1):
            if b > 0:
                handr.append(V[-b])
            
            hand = [0] + handl + handr
            hand.sort(reverse=True)
            
            if K > a + b:
                c = 0
                for h in hand[::-1]:
                    if h < 0:
                        hand.pop()
                        c += 1
                        if c >= K - (a + b):
                            break
                ans = max(ans, sum(hand))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
R = min(N,K)

ans = 0
for a in range(R):
    for b in range(R):
        if a+b > R: break
        tmpV = sorted(V[:a]+rV[:b])
        if N == 1: tmpV = [V[0]]
        t = sum(tmpV)
        for i in range(min(len(tmpV),K-(a+b))):
            if tmpV[i] >= 0: break
            else: t -= tmpV[i]
        if ans < t: ans = t
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
R = min(N,K)

ans = 0
for a in range(R):
    for b in range(R):
        if a+b > R: break
        tmpV = sorted(V[:a]+rV[:b])
        t = sum(tmpV)
        for i in range(min(len(tmpV),K-(a+b))):
            if tmpV[i] >= 0: break
            else: t -= tmpV[i]
        if ans < t: ans = t
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
v=[int(i) for i in input().split()]
res=0
for right in range(k+1):
    for left in range(k-right+1):
        ans=[]
        ans+=v[:right]
        ans+=v[n-left:]
        ans.sort()
        for i in range(min(k-right-left,len(ans))+1):
            res=max(res,sum(ans[i:]))
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
v=[int(i) for i in input().split()]
res=0
for right in range(k+1):
    for left in range(k-right+1):
        ans=[]
        ans+=v[:right]
        ans+=v[n-left:]
        ans.sort()
        for i in range(min(k-right-left,len(ans))+1):
            print((right,left,ans[i:]))
            res=max(res,sum(ans[i:]))
print(res)

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
v=[int(i) for i in input().split()]
res=0
for right in range(k+1):
    for left in range(k-right+1):
        if right==0 and left==0:
            continue
        ans=[]
        ans+=v[:right]
        ans+=v[n-left:]
        ans.sort()
        for i in range(k-right-left+1):
            res=max(res,sum(ans[i:]))
print(res)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque
N, K = map(int, input().split())
D = list(map(int, input().split()))

ans = 0
for pull_num in range(K+1):
    push_num = K - pull_num
    #print('pull_num:{}'.format(pull_num))
    for left_num in range(pull_num+1):
        right_num = pull_num - left_num
        #print('left_num:{} right_num:{}'.format(left_num, right_num))
        left_D = D[:left_num];  right_D = D[N-right_num:]
        #print('left_D:{} right_D:{}'.format(left_D, right_D))
        temochi = left_D + right_D
        temochi.sort()
        #print('temochi:{}'.format(temochi))
        index = bisect.bisect_left(temochi, 0)
        #print('index:{}'.format(index))
        #print('push_num:{}'.format(push_num))
        if push_num >= index:
            ans = max(ans, sum(temochi[index:]))
        else:
            ans = max(ans, sum(temochi[push_num:]))
print(ans)

Submission status: Wrong Answer

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

ans=0
for nab in range(1,min(N+1,K+1)):
    nr=K-nab
    for na in range(1,nab+1):
        nb=nab-na
        vt=sorted(V[:na]+V[N-nb:])
        for ncd in range(1,min(nr+1,nab+1)):
            np=sum(vt[ncd:])
            ans=max(ans,np)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=[]
for i in range(k):
  x=v[0:i]
  for j in range(k-i+1):
    if i<=n-j:
      y=v[n-j:n]
    else:
      y=v[i:n]
    L=x+y
    L.sort()
    ct=0
    ct2=0
    for m in range(len(L)):
      if L[m]>=0:
        ct+=L[m]
      else:
        if ct2<k-i-j:
          ct2+=1
        else:
          ct+=L[m]
    ans.append(ct)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=[]
for i in range(k):
  x=v[0:i]
  for j in range(k-i+1):
    if k<n:
      y=v[n-j:n]
    else:
      y=v[i:n]
    L=x+y
    L.sort()
    ct=0
    ct2=0
    for m in range(len(L)):
      if L[m]>=0:
        ct+=L[m]
      else:
        if ct2<k-i-j:
          ct2+=1
        else:
          ct+=L[m]
    ans.append(ct)
print(max(ans))