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()))
# N = 40
# K = 100
# V = [i for i in range(N)]

# V = [0 for i in range(N)] + V + [0 for i in range(N)] 

sumval = 0
for l in range(min(K+1,N)):
    for r in range(min(K+1-l,N-l)):
        sV = sorted(V[:l]+ V[N-r:])
        i_div = 0
        for i_div in range(len(sV)):
            if sV[i_div] > 0:
                break
        # for sute in range(min(K-r-l+1,len(sV),i_div+1)):
        for sute in range(min(K-r-l+1,len(sV))):
            c = sum(sV[sute:])
            sumval = max(c,sumval)

print(sumval)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
# import statistics
import bisect
import copy
import fractions
import math
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))
def unite(x, y):
    # それぞれのノードの根を求める
    x = root(x)
    y = root(y)
    if x == y:
        return
    # node[x]の根をyに変更する
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  # xが根の場合
        return x
    else:
        node[x] = root(node[x])  # 経路圧縮
        return node[x]
def dfs(v):
    # ノードに訪れた
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  # そもそもvとv2が辺でつながっていない
            continue
        if visited[v2] == True:  # もう見た
            continue
        dfs(v2)


n, k = MI()
v = LI()
m = min(n, k)
ans = -INF
for a in range(m+1):
    for b in range(0, m+1-a):
        tmp_l = []
        print(a, b)
        for i in range(a):
            tmp_l.append(v[i])
        for j in range(b):
            tmp_l.append(v[-(j+1)])
        index = 0
        negative_sum = 0
        tmp_l.sort()
        print(tmp_l)
        print(a, b)
        while index < len(tmp_l) and index < k-(a+b):
            if tmp_l[index] < 0:
                negative_sum -= tmp_l[index]
            index += 1
        ans = max(ans, negative_sum + sum(tmp_l))
print(ans)

Submission status: Wrong Answer

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

combs = []
lim = min([N, K])
for a in range(0, lim + 1):
    for b in range(0, lim - a + 1):
          combs.append((a, b))

maxres = 0
for a,b in combs:
    elems = sorted(D[:a] + D[-b:])
    negative = len([e for e in elems if e < 0])
    drop = min(negative, max(0, K - a - b))
    res = sum(elems[drop:])
    maxres = max(maxres, res)
print(maxres)

Submission status: Wrong Answer

Solution code:
import copy

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

def calcMax(arr,K):
	maxValue = 0
	# maxValue = max(maxValue,sum(arr))
	import heapq
	heap=[]
	for k in range(K):
		heapq.heappush(heap, arr[k])
		maxValue=max(maxValue,sum(heap))

		if 1 <= k:
			tmpHeap = copy.copy(heap)
			for _ in range(K-(k+1)):
				if 0 < len(tmpHeap):
					# print(k,K,tmpHeap)
					heapq.heappop(tmpHeap)
					maxValue=max(maxValue,sum(tmpHeap))

	# print(arr,K,maxValue)
	return maxValue



maxValue=0

for k in range(K+1):
	left=min(k,N)
	right=min(K-k,N)

	if min(K,N) < left + right:
		continue

	leftValue=0
	rightValue=0

	if 0 < left:
		leftValue=calcMax(V[:left],left)

	if 0 < right:
		tmp=V[N-right:]
		tmp=tmp[::-1]
		rightValue=calcMax(tmp,right)

	# print("---")
	maxValue = max(maxValue,leftValue+rightValue)



print(maxValue)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for i in range(1, min(N,K)+1):
    for j in range(i+1):
        W = sorted(V[:j]+V[N-i+j:])
        for k in range(min(i, min(N,K)-i)+1):
            x = sum(W[k:])
            if x > ans:
                ans = x
print(ans)

Submission status: Wrong Answer

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

# V = [0 for i in range(N)] + V + [0 for i in range(N)] 

sumval = 0
for l in range(min(K+1,N)):
    for r in range(min(K+1-l,N-l)):
        sV = sorted(V[:l]+ V[N-r:])
        i_div = 0
        for i_div in range(len(sV)):
            if sV[i_div] > 0:
                break
        # print(K-r-l+1, r+l, i_div)
        for sute in range(min(K-r-l+1,r+l,i_div+1)):
            c = sum(sV[sute:])
            # print(sV)
            # print("     " ,r,l,sute, c)
            sumval = max(c,sumval)

print(sumval)

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):
        W = sorted(V[:j]+V[N-i+j:])
        for k in range(min(i, min(N,K)-i)+1):
            x = sum(W[k:])
            if x > ans:
                ans = x
print(ans)

Submission status: Wrong Answer

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

maxv = 0
for il in range(K + 1):
    for ir in range(K + 1):
        if il + ir > K:
            break
        d = K - il - ir
        if il + ir >= N:
            gets = V
        else:
            gets = V[:il + 1] + V[-1 - ir:]
        gets.sort()
        #print("il={},ir={},d={},gets={}".format(il,ir,d,gets))
        for v in gets:
            if v >= 0:
                break
            if d == 0:
                break
            #print("remove={}".format(v))
            gets.remove(v)
            d -= 1
        if sum(gets) > maxv:
            maxv = sum(gets)
        #print("sum(gets)={}".format(sum(gets)))

print(maxv)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = -10**7
for i in range(min(N,K)+1):
    for j in range(i+1):
        W = sorted(V[:j]+V[N-i+j:])
        for k in range(min(i, min(N,K)-i)+1):
            x = sum(W[k:])
            if x > ans:
                ans = x
print(ans)

Submission status: Wrong Answer

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

combs = []
lim = min([N, K])
for a in range(0, lim + 1):
    for b in range(0, lim - a + 1):
          combs.append((a, b))

maxres = 0
for a,b in combs:
    elems = D[:a] + D[len(D) - b:]
    elems.sort()
    negative = len(list(filter(lambda x: x < 0, elems)))
    drop = min(negative, lim - a - b)
    res = sum(elems[drop:])
    maxres = max(maxres, res)
print(maxres)

Submission status: Wrong Answer

Solution code:
# 0~kまでx個左から取り、0~k-xまで右からy個取り、最後に0~k-x-yまで取ったうちで小さいやつを捨てる
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for x in range(k + 1):
    for y in range(k - x + 1):
        for z in range(k - x - y + 1):
            if x + y < z or x + y + z > k:
                continue
            arr_tmpx = []
            arr_tmpy = []
            if x > 0:
                arr_tmpx = arr[:x]
            if y > 0:
                arr_tmpy = arr[-y:]
            arr_tmpx.extend(arr_tmpy)
            arr_tmpx = sorted(arr_tmpx)
            m = 0
            sorted_arr_tmpx = sorted(arr_tmpx)
            for zi in range(z):
                if sorted_arr_tmpx[zi] >= 0:
                    break
                m += sorted_arr_tmpx[zi]
            ans = max(ans, sum(arr_tmpx)-m)

print(ans)

Submission status: Wrong Answer

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

value = max_value = 0
for j in range(K//2+1):
    for i in range(K-j+1):
        if i == K-j:
            value = V[:K-j]
            
        else:
            value = V[:i]+V[-(K-j-i):]
        value.sort()
        # print(i,j,value)
        
        
        jj = min(len(value)-1,j)
        if value[jj] > 0:
            s = sum([i for i in value if i>0])
        else:
            s = sum(value[jj:])

        if  s > max_value:
            max_value = s

print(max_value)

Submission status: Wrong Answer

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

maxcost = min(N,K)
o = []

A = 0
while A <= maxcost:
    B = 0
    while A + B <= maxcost:
        a = V[:A]+V[N-B:]
        b = sorted(list(filter(lambda x:x<0,a)))[:maxcost-A-B]
        #print(a,b)
        o.append(sum(a)-sum(b))
        B += 1
    A += 1

print(max(o))

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(min(K, N) + 1):
    to_remove = K - pick
    if pick < to_remove: continue
    for left in range(pick + 1):
        right = pick - left

        t = sum_L[left] + sum_R[right]

        q = vl[:left] + vr[:right]
        heapify(q)

        cnt = 0
        while cnt < to_remove and q:
            t -= heappop(q)
            cnt += 1
        ans = max(ans, t)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

def calcMax(arr,K):
	maxValue = 0
	maxValue = max(maxValue,sum(arr))
	import heapq
	heap=[]
	for k in range(K):
		heapq.heappush(heap, arr[k])
		maxValue=max(maxValue,sum(heap))

		if 1 <= k:
			tmpHeap = copy.copy(heap)
			for _ in range(K-(k+1)):
				if 0 < len(tmpHeap):
					# print(k,K,tmpHeap)
					heapq.heappop(tmpHeap)
					maxValue=max(maxValue,sum(tmpHeap))

	# print(arr,K,maxValue)
	return maxValue



maxValue=0

for k in range(K+1):
	left=min(k,N)
	right=min(K-k,N)

	if N < left + right:
		continue

	leftValue=0
	rightValue=0

	if 0 < left:
		leftValue=calcMax(V[:left],left)

	if 0 < right:
		tmp=V[N-right:]
		tmp=tmp[::-1]
		rightValue=calcMax(tmp,right)

	# print("---")
	maxValue = max(maxValue,leftValue+rightValue)



print(maxValue)

Submission status: Wrong Answer

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

V_minus = len([v for v in V if v < 0])
K = min(K, N + V_minus)

vc_left = [0] * (K + 1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_left[i + 1] = max(vc_left[i + 1], sum(V[:i + 1]), vc_left[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_left[i+1+j] = max(vc_left[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_left[i+j])

V.reverse()
vc_right = [0] * (K+1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_right[i + 1] = max(vc_right[i + 1], sum(V[:i + 1]), vc_right[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_right[i+1+j] = max(vc_right[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_right[i+j])

ans_cand = [0] * (K+1)
for i in range(K+1):
    ans_cand[i] = vc_left[i] + vc_right[K-i]
print(max(ans_cand))

Submission status: Wrong Answer

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

V_minus = len([v for v in V if v < 0])
K = min(K, N + V_minus)

vc_left = [0] * (K + 1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_left[i + 1] = max(vc_left[i + 1], sum(V[:i + 1]), vc_left[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_left[i+1+j] = max(vc_left[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_left[i+j])

V.reverse()
vc_right = [0] * (K+1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_right[i + 1] = max(vc_right[i + 1], sum(V[:i + 1]), vc_right[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_right[i+1+j] = max(vc_right[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_right[i+j])

print(vc_left)
print(vc_right)

ans_cand = [0] * (K+1)
for i in range(K+1):
    ans_cand[i] = vc_left[i] + vc_right[K-i]
print(max(ans_cand))

Submission status: Wrong Answer

Solution code:
import itertools as it
import collections as col
import heapq
import sys
sys.setrecursionlimit(10**9)
"""
N=int(input())
D=[]
for i in range(N):
  s,p=input().split()
  D.append((s, int(p), i+1))
D.sort(key=lambda d: (d[0], -d[1], d[2]))

for d in D:
  print(d[2])
"""
"""
N,M=map(int, input().split())
K=[]
S=[]
for i in range(M):
  k,*si=map(int, input().split())
  K.append(k)
  S.append(list(sorted(si)))
P=list(map(int, input().split()))

L=it.product([0,1], repeat=N)
cnt=0
#print(S)
for l in L:
  ok=True
  for i,si in enumerate(S):
    pi=P[i]
    sumS=len(list(filter(lambda it: l[it-1]==1, si)))
    #print(l, sumS)
    if sumS%2 != pi: ok=False
  if ok: cnt += 1
print(cnt)
"""
N,K=map(int,input().split())
V=list(map(int, input().split()))
M=min(N,K)

ans=0

for l in range(M):
  for r in range(M):
    if l+r<=min(N,K):
      H=[]
      U=list(V)
      H+=U[:l] + U[N-r:]
      H.sort(reverse=True)
      #print((l,r),U, H)
      for i in range(K-l-r):
        if H and H[-1] < 0: H.pop()
        else: break
      #print(sum(H), H)
      ans=max(ans, sum(H))
print(ans)

Submission status: Wrong Answer

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

all=[]

for i in range(k+1):
    for j in range(k-i+1):
        if j<=n:
            if k-i-j>0:
                left=line[:j]
                right=line[-k+i+j:]
                sep=left+right
            else:
                sep=line[:j]
            
            sep_neg=[item for item in sep if item < 0]
            if len(sep_neg) < i:
                get=[item for item in sep if item >= 0]
            else:
                sep.sort()
                get=sep[i:]
            all.append(sum(get))
        #     print('sep')            
        #     print(sep)
        #     print('sep_neg')            
        #     print(sep_neg)
        #     print('get')
        #     print(get)

print(max(all))

Submission status: Wrong Answer

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


def first(condition, iterable, default=None):
    try:
        return next(i for i, x in enumerate(iterable) if condition(x))
    except StopIteration:
        return default


cap = min(n, k)
max_val = 0
for left in range(k):
    for right in range(cap - left):
        rest = k - (left + right)  # 0 <= rest <= k
        l_popped = v[:left]
        r_popped = v[-right:] if right > 0 else []
        popped = l_popped + r_popped

        popped.sort()

        first_positive = first(lambda x: x > 0, popped, default = len(popped))

        sum_val = sum(popped[min(rest, first_positive):])
        if sum_val > max_val:
            max_val = sum_val

print(max_val)

Submission status: Wrong Answer

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

maxcost = min(N,K)
o = []

A = 0
while A <= maxcost:
    B = 0
    while A + B <= maxcost:
        a = V[:A]+V[N-B:]
        o.append(sum(a)-sum(sorted(list(filter(lambda x:x<0,a)))[:maxcost-A-B]))
        B += 1
    A += 1

print(max(o))

Submission status: Wrong Answer

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

maxv = 0
for il in range(K):
    for ir in range(K):
        if il + ir > K:
            break
        d = K - il - ir
        gets = V[:il + 1] + V[-1 - ir:]
        gets.sort()
        #print("il={},ir={},d={},gets={}".format(il,ir,d,gets))
        for v in gets:
            if v >= 0:
                break
            if d == 0:
                break
            #print("remove={}".format(v))
            gets.remove(v)
            d -= 1
        if sum(gets) > maxv:
            maxv = sum(gets)
        #print("sum(gets)={}".format(sum(gets)))

print(maxv)

Submission status: Wrong Answer

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

all=[]

for i in range(k):
    for j in range(k-i+1):
        if j<=n:
            if k-i-j>0:
                left=line[:j]
                right=line[-k+i+j:]
                sep=left+right
            else:
                sep=line[:j]
            
            sep_neg=[item for item in sep if item < 0]
            if len(sep_neg) < i:
                get=[item for item in sep if item >= 0]
            else:
                sep.sort()
                get=sep[i:]
            all.append(sum(get))
        #     print('sep')            
        #     print(sep)
        #     print('sep_neg')            
        #     print(sep_neg)
        #     print('get')
        #     print(get)

print(max(all))

Submission status: Wrong Answer

Solution code:
N,KK = map(int,input().split())
V = list(map(int,input().split()))
# N = 40
# K = 100
# V = [i for i in range(N)]

# V = [0 for i in range(N)] + V + [0 for i in range(N)] 

sumval = 0
for K in range(KK+1):
    for r in range(K):
        for sute in range(K):
            l = K-r
            # print(V[:K-sute-i], V[N-i:])
            sV = sorted(V[:max(l-sute,0)]+ V[N-r:])
            # print(sV[sute:])
            for i_sute in range(min(sute+1, len(sV))):
                if sV[i_sute] > 0:
                    break
            c = sum(sV[i_sute:])
            sumval = max(c,sumval)

print(sumval)

Submission status: Wrong Answer

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

ans = 0

for a in range(K + 1):
    for b in range(K - a + 1):
        c = K - a - b
        tmp = v_list[:a:] + v_list[N-b:]
        tmp.sort()
        trash = 0
        for i in range(min(c, len(tmp))):
            t = tmp[i]
            if t < 0:
                trash += t
            else:
                break
        ans = max(sum(tmp) - trash, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
O = min(N, K)
for i in range(O+1):
    for j in range(O+1):
        if i + j > O:
            break
        DV = deque(V)
        S = 0
        nums = []
        for _ in range(i):
            nums.append(DV.popleft())
        for _ in range(j):
            nums.append(DV.pop())
        nums.sort()
        d = 0
        for k, n in enumerate(nums):
            if d == K - i - j:
                break
            if n < 0:
                del nums[k]
                d += 1
            else:
                break
        ans = max(ans, sum(nums))
print(ans)

Submission status: Wrong Answer

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

combs = []
lim = min([N, K])
for a in range(0, lim + 1):
    for b in range(0, lim - a + 1):
          combs.append((a, b, lim - a - b))
# print(combs)
maxres = None
for a,b,c in combs:
    elems = D[:a] + D[len(D) - b:]
    elems.sort()
    negative = len(list(filter(lambda x: x < 0, elems)))
    drop = min(negative, c)
    # print("Negative", negative, "drop", c, drop)
    res = sum(elems[drop:])
    if maxres == None:
        maxres = res
    elif maxres < res:
        # print(a, b, drop, res, elems)
        maxres = res
print(maxres)

Submission status: Wrong Answer

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

s = 0
for i in range(K): # 左から取り出す回数
    for j in range(K - i): # 右から取り出す回数
        if i + j > N:
            continue
        
        a = V[:i] + V[N-j:]
        r = K - i - j # 戻すことのできる回数

        a.sort()
        for k in range(min(len(a)+1, r+1)):
            s = max(s, sum(a[k:]))

print(s)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from copy import copy
import numpy as np
from numpy.fft import fft, ifft
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


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


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

n, k = MAP()

v = LIST()

m = int(n) + 1


lis = []
for h in range(k+1):
    for i in range(h+1):
        for j in range(h-i):
            #print(h, i, j)
            j*=-1
            if j==0:
                #print(v[:i])
                lis.append(v[:i])
            else:
                #print(v[:i] + v[j:])
                lis.append(v[:i]+v[j:])

ans=0

for x in lis:
    a = x.copy()
    a.sort()
    for i in range(1, k - len(x) + 1):
        if a == []:
            break
        else:
            a.pop(0)
            #print(a)
            ans = max(ans, sum(a))
            #print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

def calcMax(arr,K):
	maxValue = 0
	maxValue = max(maxValue,sum(arr))
	import heapq
	heap=[]
	for k in range(K):
		heapq.heappush(heap, arr[k])
		maxValue=max(maxValue,sum(heap))

		if 1 <= k:
			tmpHeap = copy.deepcopy(heap)
			for _ in range(K-(k+1)):
				if 0 < len(tmpHeap):
					# print(k,K,tmpHeap)
					heapq.heappop(tmpHeap)
					maxValue=max(maxValue,sum(tmpHeap))

	# print(arr,K,maxValue)
	return maxValue



maxValue=0

for k in range(K+1):
	left=min(k,N)
	right=min(K-k,N)

	leftValue=0
	rightValue=0

	if 0 < left:
		leftValue=calcMax(V[:left],left)

	if 0 < right:
		tmp=V[N-right:]
		tmp=tmp[::-1]
		rightValue=calcMax(tmp,right)

	# print("---")
	maxValue = max(maxValue,leftValue+rightValue)



print(maxValue)

Submission status: Wrong Answer

Solution code:
# 0~kまでx個左から取り、0~k-xまで右からy個取り、最後に0~k-x-yまで取ったうちで小さいやつを捨てる
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for x in range(k + 1):
    for y in range(k - x + 1):
        for z in range(k - y + 1):
            if x + y < z or x + y + z > k:
                continue
            arr_tmpx = []
            arr_tmpy = []
            if x > 0:
                arr_tmpx = arr[:x]
            if y > 0:
                arr_tmpy = arr[-y:]
            arr_tmpx.extend(arr_tmpy)
            arr_tmpx = sorted(arr_tmpx)
            m = 0
            sorted_arr_tmpx = sorted(arr_tmpx)
            for zi in range(z):
                if sorted_arr_tmpx[zi] >= 0:
                    break
                m += sorted_arr_tmpx[zi]
            ans = max(ans, sum(arr_tmpx)-m)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=[]
for a in range(min(n,k)+1):
    for b in range(min(n,k)-a+1):
        if b>0:
            tmp=sum(v[:a])+sum(v[-b:])
            l=v[:a]+v[-b:]
        elif b==0:
            tmp=sum(v[:a])
            l=v[:a]
        l.sort()
        for j in range(min(n,k)-a-b):
            if len(l)>=1 and l[0]<0:
                l.remove(l[0])
        ans.append(sum(l))
print(max(max(ans),0))

Submission status: Wrong Answer

Solution code:
import itertools as it
import collections as col
import heapq
import sys
sys.setrecursionlimit(10**9)
"""
N=int(input())
D=[]
for i in range(N):
  s,p=input().split()
  D.append((s, int(p), i+1))
D.sort(key=lambda d: (d[0], -d[1], d[2]))

for d in D:
  print(d[2])
"""
"""
N,M=map(int, input().split())
K=[]
S=[]
for i in range(M):
  k,*si=map(int, input().split())
  K.append(k)
  S.append(list(sorted(si)))
P=list(map(int, input().split()))

L=it.product([0,1], repeat=N)
cnt=0
#print(S)
for l in L:
  ok=True
  for i,si in enumerate(S):
    pi=P[i]
    sumS=len(list(filter(lambda it: l[it-1]==1, si)))
    #print(l, sumS)
    if sumS%2 != pi: ok=False
  if ok: cnt += 1
print(cnt)
"""
N,K=map(int,input().split())
V=col.deque(map(int, input().split()))
M=min(N,K)

ans=0

for l in range(M):
  for r in range(M):
    if l+r<=min(N,K):
      H=[]
      heapq.heapify(H)
      U=col.deque(V)
      for i in range(l):
        heapq.heappush(H, U.popleft())
      for i in range(r):
        heapq.heappush(H, U.pop())
      #print((l,r),U, H)
      i=0
      while len(H) > 0 and i<K-l-r:
        i += 1
        rm=heapq.heappop(H)
        if rm>=0:
          heapq.heappush(H, rm)
          i=K
      #print(sum(H), H)
      ans=max(ans, sum(H))
print(ans)

Submission status: Wrong Answer

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))

def gsum(l,r):
    return sum(V[l:(r)])
N,K=spaceinput()
V=spaceinput()

v_total=sum(V)
res=0
for nl in range(N):
    for nr in range(N-nl):
        if nl+nr>K:
            break
        l=nl
        r=N-nr-1
        if nl>0:
            temoto=V[:l]
        else:
            temoto=[]
        #print("temoto_l",temoto)
        if nr>0:
            temoto.extend(V[(r+1):])            
        temoto=sorted(temoto)
        #tmp=v_total-gsum(l,r)
        tmp=sum(temoto)
        #print(temoto,tmp,v_total,gsum(l,r),r)
        for ss in range(K-len(temoto)):
            #print(ss)
            if ss>len(temoto)-1:
                break
            if temoto[ss]<0:
                tmp-=temoto[ss]
            else:
                break

        if tmp>res:
            res=tmp
            #print("koksin",res)
            #print(nl,nr)
            #print(l,r)
        

print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def uy():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,m = uy()

a = l()

su = 0
for i in range(n+1):
	ll = max(i,n-(m - i))
	if ll == i:
		lll = n-(m - i) - i
	else:
		lll = 0

	for j in range(ll,n+1):
		q = []
		co = 0
		p = a[:i]
		pp = a[j:]
		for k in range(len(p)):
			if p[k] < 0:
				hq.heappush(q,p[k])
		for k in range(len(pp)):
			if pp[k] < 0:
				hq.heappush(q,pp[k])
		co += sum(p)
		co += sum(pp)
		q.sort()
		for k in range(min(len(q),j-ll+lll)):
			co += abs(hq.heappop(q))
		su = max(su,co)
print(su)

Submission status: Wrong Answer

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

s = 0
for i in range(K): # 左から取り出す回数
    for j in range(K - i): # 右から取り出す回数
        if i + j > N:
            continue
        
        a = V[:i] + V[N-j:]
        r = K - i - j # 戻すことのできる回数

        a.sort()
        for k in range(min(len(a), r+1)):
            s = max(s, sum(a[k:]))

print(s)

Submission status: Wrong Answer

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))

def gsum(l,r):
    return sum(V[l:(r)])
N,K=spaceinput()
V=spaceinput()

v_total=sum(V)
res=0
for nl in range(N):
    for nr in range(N-nl):
        if nl+nr>=K:
            break
        l=nl
        r=N-nr-1
        if nl>0:
            temoto=V[:l]
        else:
            temoto=[]
        #print("temoto_l",temoto)
        if nr>0:
            temoto.extend(V[(r+1):])            
        temoto=sorted(temoto)
        #tmp=v_total-gsum(l,r)
        tmp=sum(temoto)
        #print(temoto,tmp,v_total,gsum(l,r),r)
        for ss in range(K-len(temoto)):
            #print(ss)
            if ss>len(temoto)-1:
                break
            if temoto[ss]<0:
                tmp-=temoto[ss]
            else:
                break

        if tmp>res:
            res=tmp
            #print("koksin",res)
            #print(nl,nr)
            #print(l,r)
        

print(res)