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:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        if a+b<=k-a-b:
            continue
        if n<=a+b:
            continue
        in_hand=d[:a]
        in_hand+=in_hand[n-b:]
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        if a+b<=k-a-b:
            continue
        if n<=a+b:
            continue
        in_hand=d[:a]
        in_hand+=list(reversed(d[a:]))[:b]
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        if a+b<=k-a-b:
            continue
        if n<=a+b:
            continue
        in_hand=d[:a]
        in_hand+=list(reversed(d))[:b]
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        if a+b<=k-a-b:
            continue
        if n<=a+b:
            continue
        in_hand=d[:a]
        in_hand.extend(list(reversed(d))[:b])
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        in_hand=d[:a]
        in_hand.extend(list(reversed(d[a:]))[:b])
        in_hand=sorted(in_hand)
        if k-a-b<0:
            continue
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

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

n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        print(a,b,k-a-b)
        in_hand=d[:a]
        in_hand.extend(list(reversed(d[a:]))[:b])
        in_hand=sorted(in_hand)
        if k-a-b<0:
            continue
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

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

n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k+1):
    for b in range(k-a+1):
        in_hand=d[:a]
        in_hand.extend(list(reversed(d))[:b])
        in_hand=sorted(in_hand)
        if k-a-b<0:
            continue
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

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

n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k):
    for b in range(k-a):
        in_hand=d[:a]
        in_hand.extend(list(reversed(d))[:b])
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

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

n,k=list(map(int, input().split()))
d=list(map(int, input().split()))
answer=0
for a in  range(k):
    for b in range(k-a):
        how_many_in_hand=a+b-(k-a-b)
        if how_many_in_hand <=0:
            continue
        in_hand=d[:a]
        in_hand.extend(list(reversed(d))[:b])
        in_hand=sorted(in_hand)
        in_hand=in_hand[k-a-b:]
        if sum(in_hand)>answer:
            answer=sum(in_hand)
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
V = IL()

ans = 0
for i in range(N+1):
  for j in range(N+1):
    if i+j > K:
      break
    data = V[:j]+V[N-i:]
    k = K-(j+i)
    data.sort()
    for t in range(min(len(data),k)):
      if data[0] <= 0:
        del data[0]
    else:
      ans = max(ans,sum(data))
      
print(ans)

Submission status: Wrong Answer

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M+1):
    for j in range(M+1-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        print(dump_num)
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 0:
                break;
            num -= dump_num[k]
        
        if num >= ans:
            ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j > N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 0:
                break;
            num -= dump_num[k]
        
        if num >= ans:
            ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 0:
                break;
            num -=dump_num[k]
        
        if num > ans:
            ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 1:
                break;
            num -=dump_num[k]
        if num > ans:
            ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,K = na()
que = na()
revque = que[::-1]

ans = 0
for a in range(min(N,K)+1):
    for b in range(min(K-a,N-a)+1):
        sum_q = que[:a] + revque[:b] if b>0 else que[:a]
        r = sum(sum_q)
        sum_q.sort()
        l = 0
        #print(sum_q)
        if sum_q:
            for i in range(min(K-(a+b),len(sum_q))):
                #sprint(i)
                if sum_q[i]>0:
                    l = i
                    break
        r = sum(sum_q[l:])
        if ans < r:
            ans = r
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,K = na()
que = na()
revque = que[::-1]

Kl,Kr = K,0
sum_s = 0
ans = 0
for a in range(min(N,K)+1):
    for b in range(0,K-a+1):
        sum_s = que[:a] + revque[:b]
        su = sum(sum_s)
        j = K-(a+b)
        while j>0 and len(sum_s)>0 and min(sum_s)<0:
            t = min(sum_s)
            su-=t
            sum_s.remove(t)
            j-=1
        ans = max(ans,su)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,K = na()
que = na()
revque = que[::-1]

Kl,Kr = K,0
sum_s = 0
ans = 0
for a in range(K+1):
    for b in range(0,K-a+1):
        sum_s = que[:a] + revque[:b]
        su = sum(sum_s)
        j = K-(a+b)
        while j>0 and len(sum_s)>0 and min(sum_s)<0:
            t = min(sum_s)
            su-=t
            sum_s.remove(t)
            j-=1
        ans = max(ans,su)
print(ans)

Submission status: Wrong Answer

Solution code:
def shift(l, n):
    return l[n:] + l[:n]

def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    v_ = v[::-1]

    time = min(n, k)
    maxv = 0
    for a in range(time + 1):
        for b in range(time - a):
            sumlist = v[:a] + v_[:b]
            sumlist.sort()
            index = -1
            for i in range(min(len(sumlist), k - (a + b))):
                if sumlist[i] < 0:
                    index = i
            sumv = sum(sumlist[index+1:])
            if maxv < sumv:
                maxv = sumv
    print(maxv)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """

    def s(w):
        l = [0] * (k + 1)
        for i in range(1, k + 1):
            _w = sorted(w[:i], reverse=True)
            tmp = sum(_w)
            l[i] = max(l[i], tmp)
            j = 0
            while i + j < k and len(_w) and _w[-1] < 0:
                j += 1
                tmp -= _w.pop()
                l[i + j] = max(l[i + j], tmp)
        return l

    lefts = s(v)
    rights = s(v[::-1])
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    lefts = [0] * (k + 1)
    rights = [0] * (k + 1)
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """
    for i in range(1, k + 1):
        _v = sorted(v[:i], reverse=True)
        lefts[i] = max(lefts[i], sum(_v))
        j = 0
        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            lefts[i + j] = max(lefts[i + j], sum(_v))
    for i in range(1, k + 1):
        _v = sorted(v[-i:], reverse=True)
        rights[i] = max(rights[i], sum(_v))
        j = 0
        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            rights[i+j] = max(rights[i+j], sum(_v))
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))

    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(i, n - k + i + j):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))

    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[n - k + i + j:])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))

    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

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

for a_i in range(k+1):
    for b_i in range(k-a_i+1):
        juwel = v[:a_i:] + v[len(v)-b_i::]
        juwel.sort()
        c = min(k - a_i - b_i, len(juwel))
        for i in range(c):
            if juwel[0] < 0:
                del juwel[0]
            else:
                break
        ans = max(ans, sum(juwel))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10000010
for i in range(k, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        m = k-i
        #print(take[m:])
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
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, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        m = k-i
        #print(take[m:])
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
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, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        l = k-i
        m = 0
        while m < l:
            m += 1
        #print(take[m:])
        #print(l,m)
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
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, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        l = k-i
        m = 0
        while m < l and v[m] < 0:
            m += 1
        #print(take[m:])
        #print(l,m)
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
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, (k+1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        l = k-i
        m = 0
        while m < l and v[m] < 0:
            m += 1
        #print(take[m:])
        #print(l,m)
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

best = 0
for a in range(min([K, N]) + 1):
    for b in range(min([K - a, N]) + 1):
        L = V[:a] + V[N - b:]
        Lp = [i for i in L if i >= 0]
        Lm = [i for i in L if i < 0]
        Lm.sort()
        s = sum(Lp + Lm[min([len(Lm), K - a - b]):])
        best = max([best, s])

print(best)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

best = 0
for a in range(min([K, N])):
    for b in range(min([K - a, N])):
        L = V[:a] + V[N - b:]
        Lp = [i for i in L if i >= 0]
        Lm = [i for i in L if i < 0]
        Lm.sort()
        s = sum(Lp + Lm[min([len(Lm), K - a - b]):])
        best = max([best, s])

print(best)

Submission status: Wrong Answer

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N, K = mi()
Vs = list(mi())

#コードが簡単になるように、全ての宝石の合計価値をとっておく
valuesum = sum(Vs)

maxvalue = -(10**8)
#取らない場合は一旦考えない。取らない場合は0なので最後にちょちょいとやればいいから
for l in range(N): #範囲は0~N-1（全て取る場合もあるし、右端1個だけ取る場合もある)
  for r in range(N): #同様
     if l > r: continue #lとrが同値になってもいいが、lのが大きいのはNG
     remain = K-N+r-l+1 #残り手数
     if remain < 0: continue #残り手数マイナスは矛盾なので無視
     #取った宝石のリストを作って昇順に並べる
     obtainedJewels = sorted(Vs[:l] + Vs[r+1:])
     nowvalue = sum(obtainedJewels)
     #0～remain個の宝石を捨てた場合の価値をmaxvalueと比較していく
     for i in range(0, remain+1):
       maxvalue = max(maxvalue, nowvalue-sum(obtainedJewels[:i]))

#ここまでで、1個でもとると合計価値マイナスになるしかない場合（例えばN=K=1で宝石がマイナスとか）はマイナスになっている
print(max(0, maxvalue))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

que = deque([(0, [], V, 0)])  # total, negative items, deque, count
best = 0
while que:
    t, s, v, n = que.pop()
    if n >= K:
        best = max([best, t + sum(s)])
        continue
    if v:
        if v[0] >= 0:
            que.append((t + v[0], s, v[1:], n + 1))
        else:
            que.append((t, s + [v[0]], v[1:], n + 1))

        if len(v) > 1:
            if v[-1] >= 0:
                que.append((t + v[-1], s, v[:-1], n + 1))
            else:
                que.append((t, s + [v[-1]], v[:-1], n + 1))
    if s:
        for i in range(len(s)):
            que.append((t, [s[j] for j in range(len(s)) if i != j], [s[i]] + v, n + 1))
            que.append((t, [s[j] for j in range(len(s)) if i != j], v + [s[i]], n + 1))

print(best)

Submission status: Wrong Answer

Solution code:
# まず、取れる分だけ取り出す
# 戻す（捨てる）操作は最後で良い。ソートして価値が低いものを捨てる
# 取り出す操作　＋　捨てる操作　＝ Kになる
N, K = map(int, input().split())
V = list(map(int, input().split()))

max_value = 0

for L in range(N+1):
  for R in range(N+1):
    if L+R > K:
      break
    if L+R > N:
      break
    get = V[:L]
    if R > 0:
      get += V[-R:]
    get.sort()
    print(get) ###
    
    rest = K - (L+R) # 残り操作回数
    # 価値が0以下を捨てる個数
    minus = sum((1 if x < 0 else 0 for x in get))
    if rest < minus:
      value = sum(get[rest:])
    else:
      value = sum((x for x in get if x > 0))
      
    if max_value < value:
      max_value = value
print(max_value)

Submission status: Wrong Answer

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

ans = 0
for k in range(min(n,K)+1):
    for a in range(k+1):
        for b in range(k-a+1):
            V = v[:a] + v[n-b:]
            V.sort()
            if k-a-b <= a+b:
                ans = max(ans,sum(V[k-a-b:]))
print(ans)

Submission status: Wrong Answer

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

ans = 0
for k in range(min(n,K)+1):
    for a in range(k+1):
        for b in range(k-a+1):
            V = v[:a] + v[n-b:]
            V.sort()
            ans = max(ans,sum(V[k-a-b:]))
print(ans)

Submission status: Wrong Answer

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

ans = 0
for k in range(K+1):
    for a in range(k+1):
        for b in range(k-a+1):
            V = v[:a] + v[n-b:]
            V.sort()
            ans = max(ans,sum(V[k-a-b:]))
print(ans)

Submission status: Wrong Answer

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

max_value = 0
for l in range(K + 1):
	for r in range(K + 1):
		if K < l + r:
			break
		hand = sorted(V[:l] + V[N - r:])
		for d in range(min(len(hand), K - l - r)):
			hand[d] = max(0, hand[d])
		max_value = max(max_value, sum(hand))
		#print(max_value, l, r, hand)
print(max_value)

Submission status: Wrong Answer

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

max_value = 0
for l in range(K + 1):
	for r in range(K - l + 1):
		if K < l + r:
			break
		hand = sorted(V[:l] + V[N - r:])
		for d in range(min(len(hand), K - l - r)):
			hand[d] = max(0, hand[d])
		max_value = max(max_value, sum(hand))
		#print(max_value, l, r, hand)
print(max_value)

Submission status: Wrong Answer

Solution code:
import math, itertools, copy
 
# 入力受け取り
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

# N, K = 6, 4
# V = [-10, 8, 2, 1, 2, 6]
max = 0

for k in range(K + 1):
	if k == 0:
		lst = V[-(K) :]
	elif k != K:
		lst = V[:k]
		for i in V[-(K - k) :]:
			lst.append(i)
	else:
		lst = (V[: k])
	# print(sorted(lst, reverse = True))
	lst_2 = [i for i in lst if i >= 0]
	# print(sorted(lst_2, reverse = True))
	# print(sum(lst))
	if len(lst_2) < K:
		ans = sum(lst_2)
	else :
		ans = sum(lst_2[ : K])
	# print(ans)
		
	if ans > max:
		max = ans
		
print(max)

Submission status: Wrong Answer

Solution code:
import math, itertools, copy
 
# 入力受け取り
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

# N, K = 6, 4
# V = [-6, -100, 50, -2, -5, -3]

# 新しい頂点をQに追加する関数
def same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score):
	# 手札を合計して、acc_valueを求める
	org_acc_value = sum(org_hand)
	
	# 最大値と等しくなれば、プログラムは終了
	if org_acc_value == max:
		finish = 1
	# ハイスコアを更新した場合は、high_scoreを書き換える
	elif org_acc_value > high_score:
		high_score = org_acc_value
	# 残りの行動回数を1減らす
	org_rem_turn -= 1
	Q.append(vertex(org_queue, org_hand, org_acc_value, org_rem_turn))
	
	return high_score 
	

# 最大値(max)を先に求めておく
max = 0
# 終了フラグ
finish = 0
# ハイスコア
high_score = 0

# 高々4個の宝石しか持てない
# itertoolsを使って、listからnCiを全て列挙し、その和を求める。和の最大値が、高々の最大値として求まる。
for i in range(1, 4 + 1):
	a = list(itertools.combinations(V, i))
	for j in range(len(a)):
		total = sum(a[j])
		if total > max:
			max = total
			
			
# 幅優先探索で、ありうるパターンを調べ上げる。
# ただし、全て負の価値の宝石が与えられているときなど、計算時間がかかってしまうので、最大値に到達した時点で計算をやめる。

# 頂点(vertex)のクラスを定義する。
# vertexは、以下の情報を持つ。
# そのときのキューqueue(list)
# そのときの手元の宝石hand(list)
# そのときの手元の宝石の価値acc_value(int)
# 残りの行動回数rem_turn(int)
# なお、頂点はリストv[]で管理する
v = []
class vertex:
	def __init__(self, queue, hand, acc_value, rem_turn):
		# vertexが持っている情報
		self.queue = queue
		self.hand = hand
		self.acc_value = acc_value
		self.rem_turn = rem_turn

# 処理が必要な頂点をQに格納しておく
Q = []
		
# 初期点の設定
init = vertex(V, [], 0, K)
Q.append(init)

# 取れる行動は以下
# 左端から宝石を取り出す
# 右端から宝石を取り出す
# 左端に宝石を詰める
# 右端に宝石を詰める

# Qが空ではない、かつfinishフラグが0の場合、処理を繰り返す
while len(Q) != 0 and finish == 0:
	# Qから頂点を1つ取り出す
	cur_ver = Q.pop(0)
	
	#残りの回数が残っていることを確認して、処理をスタート
	if cur_ver.rem_turn >= 1:
	
		# 取れる行動4つに関して処理を分ける
		for i in range(4):
			# 親頂点の情報を記録しておく
			org_queue = copy.deepcopy(cur_ver.queue)
			org_hand = copy.deepcopy(cur_ver.hand)
			org_acc_value = cur_ver.acc_value
			org_rem_turn = cur_ver.rem_turn
			
			# その1 左端から宝石を取り出す
			if i == 0 and len(org_queue) != 0:
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 左端をpopして手札に追加
				org_hand.append(org_queue.pop(0))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
			# その2 右端から宝石を取り出す
			elif i == 1 and len(org_queue) >= 2: # もし、len(org_queue) = 1のときは、その1とその2の処理は結果的に同じになるので省ける
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 右端をpopして手札に追加
				org_hand.append(org_queue.pop(-1))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
			# その3 左端に宝石を詰める
			elif i == 2 and len(org_hand) != 0:
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 手札の左端をpopしてqueueに追加
				org_queue.append(org_hand.pop(0))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
			
			# その4 左端から宝石を取り出す
			elif i == 3 and len(org_hand) >= 2: # もし、len(org_hand) = 1のときは、その3とその4の処理は結果的に同じになるので省ける
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 手札の右端をpopしてqueueに追加
				org_queue.append(org_hand.pop(-1))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
				
# 最終的な答えのアウトプット
print(high_score)

Submission status: Wrong Answer

Solution code:
import math, itertools, copy
 
# 入力受け取り
# N, K = list(map(int, input().split()))
# V = list(map(int, input().split()))

N, K = 6, 4
V = [-6, -100, 50, -2, -5, -3]

# 新しい頂点をQに追加する関数
def same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score):
	# 手札を合計して、acc_valueを求める
	org_acc_value = sum(org_hand)
	
	# 最大値と等しくなれば、プログラムは終了
	if org_acc_value == max:
		finish = 1
	# ハイスコアを更新した場合は、high_scoreを書き換える
	elif org_acc_value > high_score:
		high_score = org_acc_value
	# 残りの行動回数を1減らす
	org_rem_turn -= 1
	Q.append(vertex(org_queue, org_hand, org_acc_value, org_rem_turn))
	
	return high_score 
	

# 最大値(max)を先に求めておく
max = 0
# 終了フラグ
finish = 0
# ハイスコア
high_score = 0

# 高々4個の宝石しか持てない
# itertoolsを使って、listからnCiを全て列挙し、その和を求める。和の最大値が、高々の最大値として求まる。
for i in range(1, 4 + 1):
	a = list(itertools.combinations(V, i))
	for j in range(len(a)):
		total = sum(a[j])
		if total > max:
			max = total
			
			
# 幅優先探索で、ありうるパターンを調べ上げる。
# ただし、全て負の価値の宝石が与えられているときなど、計算時間がかかってしまうので、最大値に到達した時点で計算をやめる。

# 頂点(vertex)のクラスを定義する。
# vertexは、以下の情報を持つ。
# そのときのキューqueue(list)
# そのときの手元の宝石hand(list)
# そのときの手元の宝石の価値acc_value(int)
# 残りの行動回数rem_turn(int)
# なお、頂点はリストv[]で管理する
v = []
class vertex:
	def __init__(self, queue, hand, acc_value, rem_turn):
		# vertexが持っている情報
		self.queue = queue
		self.hand = hand
		self.acc_value = acc_value
		self.rem_turn = rem_turn

# 処理が必要な頂点をQに格納しておく
Q = []
		
# 初期点の設定
init = vertex(V, [], 0, K)
Q.append(init)

# 取れる行動は以下
# 左端から宝石を取り出す
# 右端から宝石を取り出す
# 左端に宝石を詰める
# 右端に宝石を詰める

# Qが空ではない、かつfinishフラグが0の場合、処理を繰り返す
while len(Q) != 0 and finish == 0:
	# Qから頂点を1つ取り出す
	cur_ver = Q.pop(0)
	
	#残りの回数が残っていることを確認して、処理をスタート
	if cur_ver.rem_turn >= 1:
	
		# 取れる行動4つに関して処理を分ける
		for i in range(4):
			# 親頂点の情報を記録しておく
			org_queue = copy.deepcopy(cur_ver.queue)
			org_hand = copy.deepcopy(cur_ver.hand)
			org_acc_value = cur_ver.acc_value
			org_rem_turn = cur_ver.rem_turn
			
			# その1 左端から宝石を取り出す
			if i == 0 and len(org_queue) != 0:
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 左端をpopして手札に追加
				org_hand.append(org_queue.pop(0))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
			# その2 右端から宝石を取り出す
			elif i == 1 and len(org_queue) >= 2: # もし、len(org_queue) = 1のときは、その1とその2の処理は結果的に同じになるので省ける
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 右端をpopして手札に追加
				org_hand.append(org_queue.pop(-1))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
			# その3 左端に宝石を詰める
			elif i == 2 and len(org_hand) != 0:
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 手札の左端をpopしてqueueに追加
				org_queue.append(org_hand.pop(0))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
			
			# その4 左端から宝石を取り出す
			elif i == 3 and len(org_hand) >= 2: # もし、len(org_hand) = 1のときは、その3とその4の処理は結果的に同じになるので省ける
				# それぞれの操作について、新しい頂点を作成し、Qに追加する。
				# 手札の右端をpopしてqueueに追加
				org_queue.append(org_hand.pop(-1))
				# 残りの処理は同じなので、関数化
				high_score = same_processing(org_queue, org_hand, org_acc_value, org_rem_turn, high_score)
				
				
# 最終的な答えのアウトプット
print(high_score)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
v = list(map(int, input().split()))
w = v[::-1]

res = 0
for l in range(N):
    for r in range(N):
        if l+r > N or l+r > K:
            break
        x = sorted(v[:l] + w[:r])
        for i in range(l+r):
            if x[i] > 0 or i == K-l-r:
                res = max(res, sum(x[i:]))
                break
print(res)

Submission status: Wrong Answer

Solution code:
N,K=[int(i) for i in input().split()]
Vs=[int(i) for i in input().split()]
def maxchoice(N,K,Vs,acquire):
	maxval=0
	for ac in range(acquire+1):
		acVs=sorted(Vs[:ac]+Vs[-1:-(acquire-ac+1):-1],reverse=True)
		for i in range(K-acquire):
			if acVs==[]:
				break
			if acVs[-1]<0:
				del acVs[-1]
				maxval=max(maxval,sum(acVs))
			else:
				maxval=max(maxval,sum(acVs))
				break
			
	return maxval

res=0
for acquire in range(1,min(N,K+1)):
	res=max(res,maxchoice(N,K,Vs,acquire))
print(res)