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:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, p = LI()
    print((a*3+p)//2)
    return

#B
def B():
    n = II()
    sp = []
    for i in range(n):
        sp.append(input().split())
    another = [(s[0], -1 * int(s[1]),num) for num,s in enumerate(sp)]
    another.sort()
    for s,p,q in another:
        print(q+1)

    return

#C
def C():
    n, m = LI()
    ks = LIR_(m)
    p = LI()
    patern = itertools.product(range(2), repeat=n)
    ans = 0
    for on in patern:
        ki = [0 for i in range(m)]
        for num, oni in enumerate(on):
            if oni:
                for i,ksi in enumerate(ks):
                    ksi = ksi[1::]
                    if num in ksi:
                        ki[i] ^= 1
        for num, pi in enumerate(p):
            if pi != ki[num]:
                break
        else:
            ans += 1
    print(ans)

            
    return

#D
def D():
    N, K = LI()
    v = LI()
    ans = 0
    for k in range(K // 2 + 1):
        #print(k)
        for n in range(1, N - 1):
            v0 = v[:n]
            v1 = v[n:]
            v1 = v1[::-1]
            for v0i in range(n + 1):
                for v1i in range(N - n + 1):
                    if v0i + v1i > K :
                        break
                    if v0i == v1i == 0:
                        continue
                    v0l = v0[:v0i]
                    v1l = v1[:v1i]
                    v0l.sort()
                    v1l.sort()
                    sumvl = sum(v0l) + sum(v1l)
                    for v0k in range(k + 1):
                        for v1k in range(k - v0k + 1):
                            if v0i + v0k + v1i + v1k > K:
                                break
                            ans = max(ans, sumvl - sum(v1l[:v1k]) - sum(v0l[:v0k]))
                            #print(v0,v1,v0i,v1i,v0l,v1l,v0k,v1k,sumvl - sum(v1l[:v1k]) - sum(v0l[:v0k]))
    print(ans)
                

    return

#E
def E():
    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# 入力
n, k = li()
v = list(li())

# 個数m決め打ち
ans = 0
for m in range(n):
    rest = k
    rest -= m

    if rest < 0:
        continue

    # 左からi個、右からm-i個
    for i in range(m+1):
        a = v[:i] + v[n-m+i:]
        a.sort()
        suma = sum(a)

        # kを超えないようにマイナスを詰める
        for j in range(rest):
            if j >= len(a):
                break

            if a[j] < 0:
                suma += abs(a[j])
            else:
                break

        ans = max(ans, suma)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,K=map(int,input().split())
V=[int(x) for x in input().split()]
M=min(N,K)
ans=0
for a in range(M+1):
    for b in range(0,M+1-a):
        #print(a,b)
        if b != 0:
            #print(V[0:a] + V[-b:])
            Get = V[0:a] + V[-b:]
            Get.sort()
            del Get[0:K-(a+b)]
            ans=max(ans,sum(Get))
            #print(Get,sum(Get))
        else:
            Get = V[0:a]
            #print(V[0:a])
            Get.sort()
            del Get[0:K-(a+b)]
            ans=max(ans,sum(Get))
            #print(Get,sum(Get))
print(ans)

Submission status: Wrong Answer

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

ans = 0
for l in range(K + 1):
    for r in range(K - l + 1):
        lst = []
        if l > 0:
            lst += V[:l]
        if r > 0:
            lst += V[-r:]

        lst.sort()
        # print('lst', lst)
        max_drop = K - (l + r)
        for d in range(max_drop + 1):
            if d >= len(lst):
                break

            ans = max(sum(lst[d:]), ans)
print(ans)

Submission status: Wrong Answer

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

R = min(N,K)


MAX = 0

for a in range(R+1):
    G = []
    for i in range(a):
        G.append(V[i])
    
    for b in range(R-a):
        H = G[:]
        for j in range(min(b,N-a)):
            H.append(V[N-j-1])
        H.sort()
        
        kari = sum(H)
        for k in range(R-(a+b)):
            if k >= a+b:
                break
            else:
                if H[k] >= 0:
                    break
                else:
                    kari+=(-H[k])
                    
        MAX = max(MAX,kari)
        
print(MAX)

Submission status: Wrong Answer

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

R = min(N,K)


MAX = 0

for a in range(R+1):
    G = []
    for i in range(a):
        G.append(V[i])
    
    for b in range(R-a):
        H = G[:]
        for j in range(b):
            H.append(V[N-j-1])
        H.sort()
        
        kari = sum(H)
        for k in range(R-(a+b)):
            if k >= a+b:
                break
            else:
                if H[k] >= 0:
                    break
                else:
                    kari+=(-H[k])
                    
        MAX = max(MAX,kari)
        
print(MAX)

Submission status: Wrong Answer

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

R = min(N,K)


MAX = 0
for a in range(R+1):
    G = []
    for i in range(a):
        G.append(V[i])
    for b in range(R+1-a):
        H = G[:]
        for j in range(min(b,N-a)):
            H.append(V[N-j-1])
        H.sort()
        kari = sum(H)
        for k in range(R-(a+b)):
            if k >= a+b:
                break
            else:
                if H[k] >= 0:
                    break
                else:
                    kari+=(-H[k])
        MAX = max(MAX,kari)
        
print(MAX)

Submission status: Wrong Answer

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

R = min(N,K)


MAX = 0
for a in range(R+1):
    G = []
    for i in range(a):
        G.append(V[i])
    for b in range(R-a):
        H = G[:]
        for j in range(min(b,N-a)):
            H.append(V[N-j-1])
        H.sort()
        kari = sum(H)
        for k in range(R-(a+b)):
            if k >= a+b:
                break
            else:
                if H[k] >= 0:
                    break
                else:
                    kari+=(-H[k])
        MAX = max(MAX,kari)
        
print(MAX)

Submission status: Wrong Answer

Solution code:
import bisect

n, k = map(int, input().split())
vs = list(map(int, input().split()))
res = 0
for i in range(k):
    for j in range(k):
        if i + j > min(k, n):
            continue
        ls = vs[:i]
        if j > 0:
            ls += vs[-j:]
        ls.sort()
        index = bisect.bisect(ls, 0)
        l = min(index, k - i - j)
        res = max(res, sum(ls[l:]))
print(res)

Submission status: Wrong Answer

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

ANS=[]
c = 0
for i in range(min(n, k)):
    for j in range(min(n, k) - i):
        c += 1
        ans1 = V[:i]
        if j == 0:
            ans2 = []
        else:
            ans2 = V[-j:]
        A = ans1 + ans2
        A.sort()
        for l in range(k-(i+j)):
            if A != [] and A[0] < 0:
                A = A[1:]
        ANS.append(sum(A))

print(max(ANS))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for a in range(min(n, k)+1):
  for b in range(min(n, k)-a+1):
    bag = []
    bag += v[:a]
    bag += v[len(v)-b:]
    bag.sort()
    if len(bag) > 0:
      for c in range(min(n, k)-a-b):
        if bag[0] < 0:
          bag.pop(0)
        if len(bag) == 0:
          break
    ans = max(ans, sum(bag))
      
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

N,K = (int(i) for i in input().split())
V = list(map(int,input().split()))
minimum = min(N,K)

ans = 0
for i in range(minimum+1):
    for j in range(minimum-i):
        AB = []
        for k in range(i):
            heapq.heappush(AB,V[k])
        for k in range(j):
            heapq.heappush(AB,V[N-k-1])
        cnt = K - i - j
        if i == 0 and j == 0:
            continue
        while cnt > 0 and AB[0] < 0:
            heapq.heappop(AB)
            cnt -= 1
            if AB == []:
                break
        if sum(AB) > ans:
            ans = sum(AB)

print(ans)

Submission status: Wrong Answer

Solution code:
#17:09
n,k = map(int,input().split())
v = list(map(int,input().split()))
'''
左からa個
右からb個
k-a-b個以内捨てて良し
'''
can = []
x = []
for a in range(k):
  if a != 0:
    try:
      x.append(v[a-1])
    except:
      continue
  y = []
  for b in range(k):
    if b != 0:
      try:
        y.append(v[-b])
      except:
        continue
    if a + b <= min(n,k):
      z = x + y
      z.sort()
      r = k - a - b
      tmp = 0
      if 0 <= r < a+b:
        if z[r] < 0:
          for i in range(r,a+b):
            tmp += z[i]
        else:
          for i in range(a+b):
            if z[i] > 0:
              tmp += z[i]
      else:
        for i in range(r,a+b):
          if z[i] > 0:
            tmp += z[i]
      can.append(tmp)
print(max(can))

Submission status: Wrong Answer

Solution code:
#17:09
n,k = map(int,input().split())
v = list(map(int,input().split()))
'''
左からa個
右からb個
k-a-b個以内捨てて良し
'''
can = []
x = []
for a in range(k):
  if a != 0:
    try:
      x.append(v[a-1])
    except:
      continue
  y = []
  for b in range(k):
    if b != 0:
      try:
        y.append(v[-b])
      except:
        continue
    if a + b <= min(n,k):
      z = x + y
      z.sort()
      r = k - a - b
      tmp = 0
      if r < a+b:
        if z[r] < 0:
          for i in range(r,a+b):
            tmp += z[i]
        else:
          for i in range(a+b):
            if z[i] > 0:
              tmp += z[i]
      else:
        for i in range(r,a+b):
          if z[i] > 0:
            tmp += z[i]
      can.append(tmp)
print(max(can))

Submission status: Wrong Answer

Solution code:
#coding utf-8
N,K=map(int,input().split())
V =list(map(int,input().split())) 

Vl =V
Vr =V[::-1]

dpl=[]
dpr=[]
ansl=[0]
ansr=[0]
T =min(N,K)+1
ans=0
for i in range(T):
  for j in range(T-i):
    for k in range(K-i-j+1):
      if i>0:
        dpl.extend(V[0:i])
      if j>0:
        dpl.extend(V[N-j:N])
      if k>0:
        if dpl !=[] and min(dpl)<0:
          del dpl[dpl.index(min(dpl))]        
      if ans<sum(dpl):
        ans=sum(dpl)
      dpl.clear()

print(ans)

Submission status: Wrong Answer

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

queue_list = [int(i) for i in input().split()]


res = 0
for s in range(min(N, K)):
    for u in range(min(K, N)-s):
        have_list = []
        for r in range(s):
            have_list.append(queue_list[r])
        for r in range(u):
            have_list.append(queue_list[N-r-1])
        have_list = sorted(have_list)
        for r in range(min(K - s - u, len(have_list))):
            if have_list[0] < 0:
                have_list.pop(0)
        res = max(res, sum(have_list))

print(res)

Submission status: Wrong Answer

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

ab_max = min(n, k)
got_max = 0
for ab_i in range(ab_max+1):
    for a_i in range(ab_i+1):
        b_i = ab_i - a_i
        taken = v[:a_i] + v[-1:-(b_i+1):-1]
        taken.sort(reverse=True)
        cd_max = min(k-ab_i, ab_i)
        got_sum = sum(taken)
        for cd_i in range(cd_max+1):
            if cd_i == 0:
                continue
            if taken[-1] >= 0:
                break
            else:
                taken.pop()
            got_sum = sum(taken)
            if got_sum > got_max:
                got_max = got_sum

print(got_max)

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy
n, k = map(int, input().split())
v = deque(map(int, input().split()))
t = min(n, k)
ans = 0
for a in range(t + 1):
    for b in range(t + 1 - a):
        vv = deepcopy(v)
        belongings = []
        for _ in range(a):
            if vv:
                jewelry = vv.popleft()
                belongings.append(jewelry)
        for _ in range(b):
            if vv:
                jewelry = vv.pop()
                belongings.append(jewelry)
        belongings.sort()
        for i in range(a + b):
            if belongings[i] < 0 and i < (t - a - b):
                belongings[i] = 0
        ans = max(sum(belongings), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

N,K = map(int,input().split(" "))

V = deque(map(int,input().split(" ")))

def search(N,K,V,score,a,b):
    if a+b < min(N,K):
        Vb = copy.copy(V)
        scoreA = copy.copy(score)
        scoreB=copy.copy(score)
        scoreA.append(V.popleft())
        A = search(N,K,V,scoreA,a+1,b)
        scoreB.append(Vb.pop())
        B = search(N,K,Vb,scoreB,a,b+1)
    score.sort
    counter = 0
    while(counter < K-a-b):
        if len(score)== 0:
            break;
        if score[0]<0:
            score.pop(0)
            counter +=1
        else:
            break;
    ans = sum(score)
    if a+b < min(N,K):
        return max(0,ans,A,B)
    else:
        return max(0,ans)

score = []
a = 0
b= 0
ans = search(N,K,V,score,a,b)
print(ans)

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(min(N, K)):
    for B in range(min(N, K)-A):
        count = K - (A + B)
        v = []
        for i in range(A):
            v.append(V_list[i])
        for j in range(B):
            v.append(V_list[-(j+1)])
        v.sort()
        for k in range(min(count, len(v))):
            if v[k] < 0:
                v[k] = 0
            else:
                break
        if ans < sum(v):
            ans = sum(v)

print(ans)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    v = list(map(int, input().rstrip('\n').split()))
    ms = 0
    for i in range(1, min(k + 1, len(v))):
        for j in range(i + 1):
            t = []
            heapq.heapify(t)
            if j != 0:
                for w in v[:j]:
                    heapq.heappush(t, w)
            if -i+j != 0:
                for w in v[-i+j:]:
                    heapq.heappush(t, w)
            for _ in range(k-i):
                if len(t) != 0:
                    p = heapq.heappop(t)
                    if p >= 0:
                        heapq.heappush(t, p)
                        break
                else:
                    break
            ms = max(ms, sum(t))
    print(ms)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    v = list(map(int, input().rstrip('\n').split()))
    ms = 0
    for i in range(1, k + 1):
        for j in range(i + 1):
            t = []
            heapq.heapify(t)
            if j != 0:
                for w in v[:j]:
                    heapq.heappush(t, w)
            if -i+j != 0:
                for w in v[-i+j:]:
                    heapq.heappush(t, w)
            for _ in range(k-i):
                if len(t) != 0:
                    p = heapq.heappop(t)
                    if p >= 0:
                        heapq.heappush(t, p)
                        break
            ms = max(ms, sum(t))
    print(ms)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for i in range(1,operation+1):
    for j in range(i+1):
        q=[]
        A = j   #左側から取り出す回数
        B = i-j    #右側から取り出す回数
        t = v[:j]
        if B>0:
            t+=v[-B:]
        t.sort()

        l = min(k-i,i-1)
        for ll in range(l):
            if v[ll] >= 0:
                l = ll
                break
        s = sum(t[l:])
        ans = max(ans,s)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for i in range(operation+1):
    for j in range(i+1):
        q=[]
        A = j   #左側から取り出す回数
        B = i-j    #右側から取り出す回数
        t = v[:j]
        if B>0:
            t+=v[-B:]
        t.sort()

        l = min(k-i,i-1)
        for ll in range(l):
            if v[ll] >= 0:
                l = ll
                break
        s = sum(t[l:])
        ans = max(ans,s)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation+1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            tmp.append(v[j])
        for j in range(B):
            tmp.append(v[n-j-1])

        sm = sum(tmp)
        tmp.sort()
        for j in range(k-op):
            if len(tmp)!=0:
                sm -= tmp.pop(0)
        ans = max(sm,ans)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation+1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            if len(v)>0:
                tmp.append(v[j])
        for j in range(B):
            if len(v)>0:
                tmp.append(v[n-j-1])

        sm = sum(tmp)
        tmp.sort()
        for j in range(k-op):
            if len(tmp)>j and tmp[j]<0:
                sm -= tmp.pop(0)
        ans = max(sm,ans)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation+1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            tmp.append(v[j])
        for j in range(B):
            tmp.append(v[len(v)-j-1])

        sm = sum(tmp)
        tmp.sort()
        for j in range(k-op):
            if len(tmp)>j and tmp[j]<0:
                sm -= tmp.pop(0)
        ans = max(sm,ans)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation+1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            tmp.append(v[j])
        for j in range(B):
            tmp.append(v[len(v)-j-1])

        sm = sum(tmp)
        tmp.sort()
        for j in range(k-op):
            if len(tmp)>j:
                sm -= tmp.pop(0)
        ans = max(sm,ans)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation+1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            tmp.append(v[j])
        for j in range(B):
            tmp.append(v[len(v)-j-1])

        sm = sum(tmp)
        tmp.sort()
        for j in range(k-op):
            if len(tmp)!=0:
                sm -= tmp.pop(0)
        ans = max(sm,ans)

print(ans)

Submission status: Wrong Answer

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

n,k = MAP()
*v, = MAP()

operation = min(n,k) #取り出すMAX回数
ans=0

for op in range(operation,-1,-1):
    for i in range(op+1):
        tmp=[]
        A = i   #左側から取り出す回数
        B = op-i    #右側から取り出す回数
        for j in range(A):
            tmp.append(v[j])
        for j in range(B):
            tmp.append(v[len(v)-j-1])

        for j in range(k-op):
            if len(tmp)!=0:
                tmp.remove(min(tmp))
        ans = max(sum(tmp),ans)

print(ans)

Submission status: Wrong Answer

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

m=min(n,ki)
ans=0

for i in range(m):
    for j in range(i+1):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v[k])
        for kz in range(i-j):
            if len(v)>0:
                q.append(v[n-1-kz])
        tmp=sum(q)
        q.sort()
        for l in range(ki-i):
            if l<len(q) and q[l]<0:
                tmp-=q[l]
        ans=max(ans,tmp)
        
print(ans)

Submission status: Wrong Answer

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

m=min(n,ki)
ans=0

for i in range(m):
    for j in range(i+1):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v[k])
        for kz in range(i-j):
            if len(v)>0:
                q.append(v[n-1-kz])
        tmp=sum(q)
        q.sort()
        for l in range(ki-j):
            if l<len(q) and q[l]<0:
                tmp-=q[l]
        ans=max(ans,tmp)
        
print(ans)

Submission status: Wrong Answer

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

m=min(n,ki)
ans=0

for i in range(m):
    for j in range(i+1):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v[k])
        for kz in range(i-j):
            if len(v)>0:
                q.append(v[n-1-kz])
        tmp=sum(q)
        for l in range(ki-j):
            if l<len(q) and q[l]<0:
                tmp-=q[l]
        ans=max(ans,tmp)
        
print(ans)

Submission status: Wrong Answer

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

m=min(n,ki)
ans=0

for i in range(m):
    for j in range(i+1):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v[k])
        for kz in range(i-j):
            if len(v)>0:
                q.append(v[n-1-kz])
        for l in range(len(q)):
            if q[l]>0:
                tmp+=q[l]
        ans=max(ans,tmp)
        
print(ans)

Submission status: Wrong Answer

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

m=min(n,ki)
ans=0

for i in range(m+1):
    for j in range(i+1):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v[k])
        for kz in range(i-j):
            if len(v)>0:
                q.append(v[n-1-kz])
        for l in range(len(q)):
            if q[l]>0:
                tmp+=q[l]
        ans=max(ans,tmp)
        
print(ans)

Submission status: Wrong Answer

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

m=min(n,k)
ans=0

for i in range(m+1):
    for j in range(i):
        q=[]
        tmp=0
        for k in range(j):
            if len(v)>0:
                q.append(v.pop(0))
        for k in range(i-j):
            if len(v)>0:
                q.append(v.pop(-1))
        
        for l in range(len(q)):
            if q[l]>0:
                tmp+=q[l]
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

from collections import deque
from heapq import heappush, heappop

res = 0

for i in range(min(k, n)):
    for j in range(0, i + 1):
        a = j
        b = i - j
        hq = []
        dq = deque(vs)

        for _ in range(a):
            heappush(hq, dq.popleft())
        for _ in range(b):
            heappush(hq, dq.pop())

        c = k - a - b
        for _ in range(c):
            if len(hq) == 0:
                break
            v = heappop(hq)
            if v > 0:
                heappush(hq, v)
                break

        res = max(res, sum(hq))

print(res)

Submission status: Wrong Answer

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

from collections import deque
from heapq import heappush, heappop

res = -float("inf")

for i in range(min(k, n)):
    for j in range(0, i + 1):
        a = j
        b = i - j
        hq = []
        dq = deque(vs)

        for _ in range(a):
            heappush(hq, dq.popleft())
        for _ in range(b):
            heappush(hq, dq.pop())

        c = k - a - b
        for _ in range(c):
            if len(hq) == 0:
                break
            v = heappop(hq)
            if v > 0:
                break

        res = max(res, sum(hq))

print(max(0, res))

Submission status: Wrong Answer

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

from collections import deque
from heapq import heappush, heappop

res = -float("inf")

for i in range(min(k, n)):
    for j in range(0, i + 1):
        a = j
        b = i - j
        hq = []
        dq = deque(vs)

        for _ in range(a):
            heappush(hq, dq.popleft())
        for _ in range(b):
            heappush(hq, dq.pop())

        c = k - a - b
        for _ in range(c):
            if len(hq) == 0:
                break
            v = heappop(hq)
            if v > 0:
                break

        res = max(res, sum(hq))

print(res)

Submission status: Wrong Answer

Solution code:
"D equeue"
N, K = map(int, input().split())  # N個の宝石, K回数の操作
V = list(map(int, input().split()))

max_value = 0
for a in range(K+1):
    for b in range(K+1):
        gain_tresure = []
        if (a + b) > K:
            continue
        gain_tresure.extend(V[:a])
        gain_tresure.extend(V[N-b:])
        # print("------------------")
        # print(gain_tresure)
        # print(K-(a+b))
        # print("------------------")
        gain_tresure = sorted(gain_tresure)
        for i in range(min(K-(a+b), len(gain_tresure))):
            if gain_tresure[i] < 0:
                gain_tresure[i] = 0
        max_value = max(max_value, sum(gain_tresure))

print(max_value)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=0
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)+1):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        while len(s)>=c+1 and t+1<=k:
            if s[c]<0:
                p-=s[c]
                c+=1
                t+=1
            else:
                break
        g=max(g,p)
print(g)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=0
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        while len(s)>=c+1 and t<=k:
            if s[c]<0:
                p-=s[c]
                c+=1
                t+=1
            else:
                break
        g=max(g,p)
print(g)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=0
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        while len(s)>=c+1 and t+1<=k:
            if s[c]<0:
                p-=s[c]
                c+=1
                t+=1
            else:
                break
        g=max(g,p)
print(g)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=-float("INF")
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)):
        s=sorted(a[:i]+a[n-j:])
        p=sum(s)
        t=i+j
        c=0
        if len(s)>0:
            while len(s)>=c+1 and t+1<=k:
                if s[c]<0:
                    p-=s[c]
                    c+=1
                    t+=1
                else:
                    break
        g=max(g,p)
print(g)