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 = [int(i) for i in input().split()]

def rec(left, right, k, minus) :
    if k == 0 or left >= right :
        return 0

    if V[left] < 0 :
        a = rec(left+1, right, k-1, minus+[V[left]]) + V[left]
    else :
        a = rec(left+1, right, k-1, minus+[V[left]]) + V[left]

    if V[right] < 0 :
        b = rec(left, right-1, k-1, minus+[V[right]]) + V[right]
    else :
        b = rec(left, right-1, k-1, minus+[V[right]]) + V[right]

    c = 0
    if k <= len(minus) :
        minus.sort()
        c = rec(left, right, k-1, minus[1:]) - minus[0]

    return max(a, b, c, 0)

print(rec(0, N-1, K, []))

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
A=list(map(int,input().split()))

output=0
for tr in range(b//2+1):
    for g in range(b-tr+1):
        #print(g)
        tmparr1=[]
        tmparr1.extend(A[:g])
        if g-b+tr<0:
            tmparr1.extend(A[g-b+tr:])
        #print(tmparr1)
        tmparr1.sort()
        #print(tmparr1)
        #print(tmparr1[tr:])

        #trまで捨てれる
        if output<sum(tmparr1[tr:]):
            output=sum(tmparr1[tr:])

        for t in range(tr):
            if output<sum(tmparr1[t:]) :
                output=sum(tmparr1[t:])
            #print(output)
print(output)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque

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

ans = 0

for i in range(min(K+1,N+1)):
    for l in range(i+1):
        for r in range(i+1-l):
            j = min(i-r-l,r+l)
            hq = []
            dq = deque(V)
            for _ in range(l):
                item = dq.popleft()
                heappush(hq, item)
            for _ in range(r):
                item = dq.pop()
                heappush(hq, item)
            for _ in range(j):
                heappop(hq)
            res = sum(hq)
            ans = max(ans,res)
        
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heappop, heappush

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    ans = 0
    L = [0] * (K+1)
    R = [0] * (K+1)
    for i in range(N):
        left = V[:i]
        right = V[::-1][:N-i]

        left_heap = []
        for v in left:
            heappush(left_heap, v)

        cnt = i
        while cnt < K and len(left_heap) > 0:
            v = heappop(left_heap)
            if v > 0:
                heappush(left_heap, v)
                break

            cnt += 1

        if cnt <= K:
            L[cnt] = max(sum(left_heap), L[cnt])


        right_heap = []
        for v in right:
            heappush(right_heap, v)

        cnt = N - i
        while cnt < K and len(right_heap) > 0:
            v = heappop(right_heap)
            if v > 0:
                heappush(right_heap, v)
                break

            cnt += 1

        if cnt <= K:
            R[cnt] = max(sum(right_heap), R[cnt])

    
    R = R[::-1]
    for left, right in zip(L, R):
        ans = max(ans, left + right)

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
a = 0 #左からaことる
b = 0 #右からbことる

ans = -10**10
while a <= min(k,n):
    while b <= min(k-a,n-a):
        plus = []
        minus = []
        for i in range(a):
            if v[i] >= 0:
                plus.append(v[i])
            else:
                minus.append(v[i])
        for i in range(b):
            if v[-1-i] >= 0:
                plus.append(v[-1-i])
            else:
                minus.append(v[-1-i])
        plus.sort()
        minus.sort()
        temp = sum(plus) + sum(minus)
        m = k-a-b #取り出した後に残った操作回数
        for i in range(min(m,len(minus))):
            temp += -minus[i]
        m = m - min(m,len(minus)) #負の数を戻した後に残った操作回数
        if m != 0:
            if m%2 != 0:
                if len(minus) == 0 and len(plus) == 0:
                    temp = -10**10
                elif len(minus) == 0:
                    temp -= plus[0]
                elif len(plus) == 0:
                    temp += minus[-1]
                else:
                    if abs(minus[-1]) <= plus[0]:
                        temp += minus[-1]
                    else:
                        temp -= plus[0]
        if temp > ans:
            ans = temp
        b += 1
    a += 1
    b = 0
print(ans)

Submission status: Wrong Answer

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

max_score = 0
max_l = min(k, n)
for l in range(max_l + 1):
    l_score = 0
    cand = []
    for i in range(l):
        cand.append(v_list[i])
    for i in range(l // 2 + 1):
        eraberu = l - i * 2
        cand2 = cand[:l-i]
        cand2.sort(reverse=True)
        s = sum(cand2[:eraberu])
        if s > l_score:
            l_score = s

    r = max_l - l
    r_score = 0
    cand = []
    for i in range(r):
        cand.append(v_list[len(v_list) - 1 - i])
    for i in range(r // 2 + 1):
        eraberu = r - i * 2
        cand2 = cand[i:]
        cand2.sort(reverse=True)
        s = sum(cand2[len(cand2)-eraberu:])
        if s > r_score:
            r_score = s

    score = l_score + r_score
    if score > max_score:
        max_score = score
print(max_score)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = [0]+list(map(int,input().split()))+[0]
S = [0]
for v in V[1:-1]:
    S.append(S[-1]+v)

ans = 0
leftminus = []
leftm = 0
for left in range(min(n+1,k)):
    rightminus = []
    if V[left] < 0:
        leftminus.append(V[left])
        leftm += 1
    for right in range(n+1,left,-1):
        tempans = S[left]
        times = left + n - right + 1
        rm = leftm
        if times > k:
            break
        for v in V[right:]:
            tempans += v
            if v < 0:
                rightminus.append(v)
                rm += 1
        minus = rightminus+leftminus
        minus.sort()
        for j in range(min(k-times,rm)):
            tempans -= minus[j]
        if ans < tempans:
            ans = tempans
print(ans)

Submission status: Wrong Answer

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

    
maxv=0
for i in range(1,min(K+1,len(v))):
    for j in range(0,i+1):
        l=v[0:j]
        r=v[N-(i-j):]
        my=l
        my.extend(r)
        my=sorted(my)
        for k in range(0,min(K-i,len(my))):
            if my[k]<0:
                my[k]=0
            else:
                break
        if maxv<sum(my):
            maxv=sum(my)
print(maxv)

Submission status: Wrong Answer

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

ans = 0

for m1 in range(N):
    for m2 in range(N-m1):
        for l in range(m1+m2):
            if m1 + m2 + l > K: continue
            tmp = V[0:m1] + V[N-m2:N]
            tmp.sort(reverse = True)
            ans = max(ans,sum(tmp[0:m1+m2-l]))
            
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
a = 0 #左からaことる
b = 0 #右からbことる

ans = -10**10
while a <= min(k,n):
    while b <= min(k-a,n-a):
        plus = []
        minus = []
        for i in range(a):
            if v[i] >= 0:
                plus.append(v[i])
            else:
                minus.append(v[i])
        for i in range(b):
            if v[-1-i] >= 0:
                plus.append(v[-1-i])
            else:
                minus.append(v[-1-i])
        plus.sort()
        minus.sort()
        temp = sum(plus) + sum(minus)
        m = k-a-b #取り出した後に残った操作回数
        for i in range(min(m,len(minus))):
            temp += -minus[i]
        m = m - min(m,len(minus)) #負の数を戻した後に残った操作回数
        if m != 0:
            if m%2 != 0:
                if len(minus) == 0 and len(plus) == 0:
                    temp = -10**10
                elif len(minus) == 0:
                    temp -= plus[0]
                elif len(plus) == 0:
                    temp += minus[-1]
                else:
                    temp += min(minus[-1],-plus[0])
        if temp > ans:
            ans = temp
        b += 1
    a += 1
    b = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().strip().split(' '))
V = list(map(int, input().strip().split(' ')))
V += V
# 宝石をi個取る、j個返す
# i個の取り方はi+1通り
# 返す時は負のもののみ、絶対値が大きいtopj個を返すので、取り方が決まるとそのときの最大点はすぐ決まる
# 0 <= i <= K, 0 <= j <= K-i
now_max = 0
for i in range(1,K+1):
  for offset in range(i+1):
    jewel = sorted(V[-offset+N: -offset+i+N])
    remain_del = K-i
    while True:
      if len(jewel) == 0:
        break
      if jewel[0] < 0 and remain_del > 0:
        jewel.pop(0)
        remain_del -= 1
      else:
        break
    now_max = max(now_max, sum(jewel))
print(now_max)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque

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

ans = 0

for i in range(min(K+1,N+1)):
    for l in range(i+1):
        for r in range(i+1-l):
            j = min(i-r-l,r+l)
            hq = []
            dq = deque(V)
            for _ in range(l):
                item = dq.popleft()
                heappush(hq, item)
            for _ in range(r):
                item = dq.pop()
                heappush(hq, item)
            for _ in range(j):
                heappop(hq)
            res = sum(hq)
            if res == 16:
                print(hq)
            ans = max(ans,res)
        
print(ans)

Submission status: Wrong Answer

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

out = 0
for i in range(min(K, N)+1):
    res = min(K, N) - i
    for j in range(i+1):
        k = i - j
        # print(V[:j], V[N-k:])

        Q = sorted(V[:j] + V[N-k:])

        minus = sum([q < 0 for q in Q])
        # print("  ", Q[min(minus, res):])

        score = sum(Q[min(minus, res):])
        s = max(s, score)

print(s)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
m = []
print(0)

Submission status: Wrong Answer

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

def rec(left, right, k, minus) :
    if k == 0 or left >= right :
        return 0

    if V[left] < 0 :
        a = rec(left+1, right, k-1, minus+[V[left]]) + V[left]
    else :
        a = rec(left+1, right, k-1, minus+[V[left]]) + V[left]

    if V[right] < 0 :
        b = rec(left, right-1, k-1, minus+[V[right]]) + V[right]
    else :
        b = rec(left, right-1, k-1, minus+[V[right]]) + V[right]

    c = 0
    if len(minus) > 0 :
        minus.sort()
        c = rec(left, right, k-1, minus[1:]) - minus[0]

    return max(a, b, c)

print(rec(0, N-1, K, []))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v = list(map(int, input().split()))
cum = [0] * (n+1)

for i in range(n):
    cum[i+1] = cum[i] + v[i]

ans = 0
cnt = 0
for i in range(n):
    for j in range(n):
        if i > j:
            continue
        l = (j - i + 1)
        if n - l > k:
            continue
        a = v[:i] + v[j+1:]
        a.sort()
        tmp1 = sum(a)
        tmp = cum[n] - (cum[j+1] - cum[i])
        # tmp = tmp1
        if tmp != tmp1:
            raise Exception()
        for x in range(k - (n-l)):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])
        cnt += 1
# if k == 1:
#     ans = max([ans, v[0], v[-1]])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	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 = m()

a = l()
if all(a[i] <= 0 for i in range(n)):
	print(0)
	exit()

su = 0
for i in range(n):
	ll = max(i,n-(m - i))
	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:
				q.append(p[k])
		for k in range(len(pp)):
			if pp[k] < 0:
				q.append(pp[k])
		co += sum(p)
		co += sum(pp)
		q.sort()
		for k in range(min(len(q),j-ll)):
			co += abs(q[k])
		su = max(su,co)
print(su)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r >= N:
            ans = max(ans, R[-1] - sum(R_m[-1]))
            if l + r > N:
                break
        if r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
            a = L[l] + R[r] - m
            ans = max(ans, a)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

dpl, dpr = [-10 ** 9] * (k + 1), [-10 ** 9] * (k + 1)
dpl[0] = dpr[0] = 0

for i in range(min(n, k)):
    i += 1
    vv = vvv[:i]
    vv.sort(reverse=True)
    sm = sum(vv)
    dpl[i] = max(dpl[i], sm)
    # print(i, vv, dpl)
    while i < k and vv:
        sm -= vv.pop()
        i += 1
        dpl[i] = max(dpl[i], sm)
    # print(i, vv, dpl)

vvv.reverse()

for i in range(min(n, k)):
    i += 1
    vv = vvv[:i]
    vv.sort(reverse=True)
    sm = sum(vv)
    dpr[i] = max(dpr[i], sm)
    # print(i, vv, dpr)
    while i < k and vv:
        sm -= vv.pop()
        i += 1
        dpr[i] = max(dpr[i], sm)
    # print(i, vv, dpr)

# print(dpl)
# print(dpr)
acl = list(accumulate(dpl, func=max))
acr = list(accumulate(dpr, func=max))
# print(acl)
# print(acr)
ans = 0
for i in range(k + 1):
    ans = max(ans, acl[i] + acr[k - i])
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):#左から何個とるか
    a = V[:i]
    for j in range(min(K - i, N - i) + 1):#右から何個とるか
        a = a + V[N - j:]
        a.sort()
        s = 0
        for l in range(i + j):
            if l >= K - i - j or a[l] >= 0:
                s += a[l]
        ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
n, k = map(int, input().split())
s_list = list(map(int, input().split()))
h_list = []
res_list = []

def rec_ope(s_list, h_list, k, minus_len):
  if k == 0:
    res_list.append(sum(h_list))
    return 0
  if len(h_list) > 0:
    if minus_len <= k:
      tmp_h = h_list.copy()
      min_h = min(h_list)
      tmp_h.remove(min_h)
      tmp_s = s_list.copy()
      tmp_s.insert(0, min_h)
      rec_ope(tmp_s, tmp_h, k-1, minus_len-1)
      tmp_s = s_list.copy()
      tmp_s.append(min_h)
      rec_ope(tmp_s, tmp_h, k-1, minus_len-1)
  if len(s_list) > 0:
    tmp_h = h_list.copy()
    tmp_s = s_list.copy()
    add_s = tmp_s.pop(0)
    tmp_h.append(add_s)
    if add_s < 0:
      minus_len += 1
    rec_ope(tmp_s, tmp_h, k-1, minus_len)
    tmp_h = h_list.copy()
    tmp_s = s_list.copy()
    add_s = tmp_s.pop(-1)
    tmp_h.append(add_s)
    if add_s < 0:
      minus_len += 1
    rec_ope(tmp_s, tmp_h, k-1, minus_len)

rec_ope(s_list, h_list, k, 0)
print(max(res_list))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2000000)
def do_action(q, temoto, value, k_remain):
    if k_remain == 0:
        return value
    a = b = c = d  = -100000000

    # どっちを掘るか決める
    l_sum = r_sum = 0
    for i in range(min(k_remain, len(q))):
        if q[i] > 0:
            l_sum += q[i]
        if q[- (i + 1)] > 0:
            r_sum += q[- (i + 1)]

    # a
    if l_sum >= r_sum and l_sum > 0:
        if len(q) != 0:
            val = q[0]
            a = do_action(q[1:], temoto + [val], value + val, k_remain - 1)
    # b
    if r_sum <= l_sum and r_sum > 0:
        if len(q) != 0:
            val = q[-1]
            b = do_action(q[:-1], temoto + [val], value + val, k_remain - 1)

    temoto.sort()
    # 戻す可能性があるのは負のがあるときのみ
    if len(temoto) > 0 and temoto[0] < 0:
        if r_sum >= l_sum and r_sum > temoto[0]:
            # c
            if len(temoto) != 0:
                temoto.sort()
                val = temoto[0]
                c = do_action([val] + q , temoto[1:], value - val, k_remain - 1)
        if l_sum >= r_sum and l_sum > temoto[0]:
            # d
            if len(temoto) != 0:
                temoto.sort()
                val = temoto[0]
                d = do_action(q + [val], temoto[1:], value - val, k_remain - 1)

    # e
    e = value

    return max(a,b,c,d,e)

n, k = map(int, input().split())
dat_v = map(int, input().split())
dat_v = list(dat_v)
val = do_action(dat_v, [], 0, k)
print(val)

Submission status: Wrong Answer

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

    
maxv=0
for i in range(1,K+1):
    for j in range(0,i+1):
        l=v[0:j]
        r=v[N-(i-j):]
        my=l
        my.extend(r)
        my=sorted(my)
        for k in range(0,min(K-i,len(my))):
            if my[k]<0:
                my[k]=0
            else:
                break
        if maxv<sum(my):
            maxv=sum(my)
print(maxv)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc128/tasks/abc128_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect

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

    ans = 0
    # print(D)
    for i in range(N+1):
        front = D[:i]
        back = D[i:]
        back.reverse()
        for k in range(K+1):
            k_front = front[:k]
            # print('k_front = {}'.format(k_front))
            front_max = 0

            for fk in range(k):
                c_k = k_front[:len(k_front)-fk]
                c_k.sort()
                cc_k = c_k[fk:]
                front_max = max(front_max, max(sum(c_k), sum(cc_k)))

            back_k = K - k
            k_back = back[:back_k]
            # print('k_back = {}'.format(k_back))
            back_max = 0

            for fk in range(back_k):
                c_k = k_back[:len(k_back)-fk]
                c_k.sort()
                cc_k = c_k[fk:]
                back_max = max(back_max, max(sum(c_k), sum(cc_k)))

            # print('front = {}, back = {}'.format(front_max, back_max))

            ans = max(ans, front_max + back_max)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy
import heapq

n, k = map(int, input().split())
v = list(map(int, input().split()))
v_sorted = sorted(v)

ans = 0
for i in range(k+1):
    if i < n:
        for nl in range(i+1):
            nr = k - nl
            ansl, ansr = 0, 0
            for n_trash in range(nl):
                ansll, ansrr = 0, 0
                hands = sorted(v[:nl-n_trash])
                for vv in hands:
                    if vv < 0 and n_trash > 0:
                        n_trash -= 1
                    else:
                        ansll += vv
                ansl = max(ansl, ansll)

            for n_trash in range(nr):
                ansrr = 0
                hands = sorted(v[-(nr-n_trash):])
                # print(hands, nr, n_trash)
                for vv in hands:
                    if vv < 0 and n_trash > 0:
                        n_trash -= 1
                    else:
                        ansrr += vv
                ansr = max(ansr, ansrr)
            # print(ansl, ansr)
            ans = max(ans, ansl + ansr)
    else:
        n_trash = n-i
        ans_cur = 0
        for vv in v_sorted:
            if vv < 0 and n_trash > 0:
                n_trash -= 1
            else:
                ans_cur += vv
        ans = max(ans, ans_cur)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,raw_input().split())
v = map(int,raw_input().split())
best = 0
for i in xrange(k+1):
	for j in xrange(min(n-i,k+1-i)):
		take = v[0:i]+v[n-j:]
		rid = k-i-j
		take = sorted(take)
		temp = sum(take)
		for x in xrange(min(rid,len(take))):
			if take[x] < 0:
				temp -= take[x]
		if temp > best:
			best = temp
print best

Submission status: Wrong Answer

Solution code:
import sys
# gcd
from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
from collections import deque
# import math
# from functools import reduce
#
# input関係の定義
# fin = open('in_1.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    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]"

    # write code

    left = []
    right = []
    left.append(0)
    right.append(0)
    left.append(max(V[0], 0))
    right.append(max(V[-1], 0))
    for i in range(2, 2*N+1):
        tmp_max = 0
        for j in range(-(-i // 2), i+1):
            tmp_V = V[:j]
            tmp_V.sort()
            # print(i, j, tmp_V[i - 2 * j:])
            if tmp_max < sum(tmp_V[i - 2 * j:]):
                tmp_max = sum(tmp_V[i - 2 * j:])
        # print(tmp_max)
        left.append(tmp_max)

    for i in range(2, 2*N+1):
        tmp_max = 0
        for j in range(i // 2, i+1):
            tmp_V = V[-j:]
            tmp_V.sort()
            if tmp_max < sum(tmp_V[i - 2 * j:]):
                tmp_max = sum(tmp_V[i - 2 * j:])
        # print(tmp_max)
        right.append(tmp_max)
    ans = []
    # print(left)
    # print(right)
    for j in range(0, min(N, K) + 1):
        tmp_ans = -10**9
        for i in range(j+1):
            if tmp_ans < left[i] + right[j - i]:
                tmp_ans = left[i] + right[j - i]
        ans.append(tmp_ans)
    print(max(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):#左から何個とるか
    a = V[:i]
    for j in range(min(K - i, N - i)):#右から何個とるか
        a = a + V[N - j:]
        a.sort()
        s = 0
        for l in range(i + j):
            if l >= K - i - j or a[l] >= 0:
                s += a[l]
        ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

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

r_part_sum = [0 for i in range(N)]
l_part_sum = [0 for i in range(N)]
# tmp = 0
# for i in range(N):
#     tmp += V[i]
#     l_part_sum[i] = tmp
#
# tmp = 0
# V.reverse()
# for i in range(N):
#     tmp += V[i]
#     r_part_sum[i] = tmp

ans = -mod
for i in range(K): # 戻す数
    for j in range(K-i+1):
        ans_list = V[:j]+V[N-(K-i-j):]
        # print(ans_list)
        ans_list.sort()
        tmp = sum(ans_list)
        for k in range(min(len(ans_list),i)):
            if ans_list[k] < 0:
                tmp -= ans_list[k]
            else:
                break
        # print(tmp)
        ans = max(ans,tmp)
        # print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
# gcd
from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
from collections import deque
# import math
# from functools import reduce
#
# input関係の定義
# fin = open('in_1.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    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]"

    # write code

    left = []
    right = []
    left.append(0)
    right.append(0)
    left.append(max(V[0], 0))
    right.append(max(V[-1], 0))
    for i in range(2, 2*N+1):
        tmp_max = 0
        for j in range(-(-i // 2), i+1):
            tmp_V = V[:j]
            tmp_V.sort()
            # print(i, j, tmp_V[i - 2 * j:])
            if tmp_max < sum(tmp_V[i - 2 * j:]):
                tmp_max = sum(tmp_V[i - 2 * j:])
        # print(tmp_max)
        left.append(tmp_max)

    for i in range(2, 2*N+1):
        tmp_max = 0
        for j in range(i // 2, i+1):
            tmp_V = V[-j:]
            tmp_V.sort()
            if tmp_max < sum(tmp_V[i - 2 * j:]):
                tmp_max = sum(tmp_V[i - 2 * j:])
        # print(tmp_max)
        right.append(tmp_max)
    ans = []
    print(left)
    print(right)
    for j in range(0, min(N, K) + 1):
        tmp_ans = -10**9
        for i in range(j+1):
            if tmp_ans < left[i] + right[j - i]:
                tmp_ans = left[i] + right[j - i]
        ans.append(tmp_ans)
    print(max(ans))

Submission status: Wrong Answer

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

ans = 0
for i in range(1, K+1):
    enq = K - i
    for j in range(i+1):
        combi = V[:i-j] + V[N-j:]
        ans = max(ans, sum(combi))
        for _ in range(enq):
            combi.remove(min(combi))
            if sum(combi) <= ans:
                break
            else:
                ans = sum(combi)
        if i >= N:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import*
from copy import*
n,k=map(int,input().split())
*v,=map(int,input().split())
v2=deepcopy(v)
motimono=[]
ans=[]
if k>=n:
    v*=2
    for j in range(1,k+1):
        for i in range(n-j,n+1):
            motimono=v[i:i+j]
            ans.append(sum(motimono))
            heapify(motimono)
            for i in range(k-j):
                if len(motimono)>0:
                    heappop(motimono)
                ans.append(sum(motimono))
    print(max(ans))
else:
    v*=2
    for j in range(1,k+1):
        for i in range(n-j,n+1):
            motimono=v[i:i+j]
            ans.append(sum(motimono))
            heapify(motimono)
            for i in range(k-j):
                if len(motimono)>0:
                    heappop(motimono)
                ans.append(sum(motimono))
    print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r >= N:
            ans = max(ans, R[-1] - sum(R_m[-1]))
            break
        if r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
            a = L[l] + R[r] - m
            ans = max(ans, a)

print(ans)