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:
import sys
sys.setrecursionlimit(10**9)

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

    X,Y = [],[]

    if N % 2 == 0:
        for i in range(N//2):
            v = V[i]
            X.append(v)

        for i in reversed(range(N//2,N)):
            v = V[i]
            Y.append(v)
    else:
        for i in range(N//2+1):
            v = V[i]
            X.append(v)

        for i in reversed(range(N//2+1,N)):
            v = V[i]
            Y.append(v)

    # print(X,Y)
    ans = 0
    for k in range(K+1):
        for x in range(k+1):
            _X = X[0:x]
            for y in range(k+1):
                _Y = Y[0:y]
                z = k-(x+y)
                if z < 0: continue

                W = _X + _Y
                W.sort()
                ans = max(ans,sum(W[z:]))

                # print(k,_X,_Y,W)


    print(ans)





if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = map(int,input().split())
A = list(map(int,input().split()))
res = 0
# 左からa個
for a in range(N):
  # 右からb個
  for b in range(N):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for _ in range(a):
      hands.append(A1.popleft())
    for _ in range(b):
      hands.append(A1.pop())
    hands.sort()
    hands = deque(hands)

    rem = K - a - b
    for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
    res = max(res, sum(hands))

print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
N,K = map(int,input().split())
A = list(map(int,input().split()))
res = 0
# 左からa個
for a in range(N):
  # 右からb個
  for b in range(N):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for i in range(a):
      hands.append(A1.popleft())
    for j in range(b):
      hands.append(A1.pop())
    heapq.heapify(hands)
    rem = K - a - b
    for _ in range(rem):
      if hands:
        heapq.heappop(hands)
      if not hands:
        break
    res = max(res, sum(hands))

print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from bisect import bisect_left


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            if idx > 0:
                ans = max(ans, sum(cand[idx:]))

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from bisect import bisect_left


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = min(bisect_left(cand, 0), k - i)
            if idx > 0:
                ans = max(ans, sum(cand[idx:]))

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
t = min(n, k)
dq = [int(i) for i in input().split()]
ans = []

for i in range(t+1):
  for j in range(i+1):
    have = dq[:j] + dq[(n-i+j):]
    drop = t - len(have)
    
    for k in range(drop):
      if not have:
        break
      
      cand = min(have)
      if cand < 0:
        have.remove(cand)
      else:
        break
    
    ans.append(sum(have))
    
print(max(ans))

Submission status: Wrong Answer

Solution code:

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

MAX = 0
for ka in range(K+1):
    for kb in range(K+1-ka):
        kcd = K - ka - kb
        
        if (ka+kb <= kcd) or (ka+kb > N):
            continue
        
        #print(ka, kb, kcd)
        s = V[:ka] + V[-kb:] if kb != 0 else V[:ka]
        s.sort()
        #print(s[kcd:], sum(s[kcd:]))
        MAX = max(MAX, sum(s[kcd:]))

print(MAX)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)

n,k = map(int,input().split())
v = list(map(int,input().split()))
ans=0
for l in range(k+1):
    for r in range(k-l):
        if l+r > n :break

        d=k-l-r
        now=0
        s=[]

        for i in range(l):
            now+=v[i]
            s.append(v[i])
        for i in range(n-r,n):
            now+=v[i]
            s.append(v[i])

        s.sort()

        for i in range(d):
            if(i >= len(s)):break
            if s[i] > 0 :break

            now -= s[i]

        ans=max(now,ans)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for left in range(min(n, k)):
    for right in range(min(n-left+1, k-left+1)):
        l = v[:left]
        l+=v[::-1][:right]
        l.sort()
        temp = sum(l)
        for t in range(max(0, min(len(l), k-left-right))):
            if l[t]<0:
                temp-=l[t]
        ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for left in range(min(n, k)):
    for right in range(min(n-left+1, k-left+1)):
        l = v[:left]
        l+=v[::-1][:right]
        l.sort()
        temp = sum(l)
        for t in range(max(0, min(len(l), k-left-right+1))):
            if l[t]<0:
                temp-=l[t]
        ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for left in range(min(n, k)):
    for right in range(min(n-left, k-left)):
        l = v[:left]
        l+=v[::-1][:right]
        l.sort()
        temp = sum(l)
        for t in range(max(0, min(len(l), k-left-right))):
            if l[t]<0:
                temp-=l[t]
        ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from functools import lru_cache

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

for l in range(0,K):
  for r in range(0,K - l + 1):
    d = K - r - l
    tmp = 0
    have = []
    if l + r > N:
      continue
    for i in range(0,l):
      tmp += V[i]
      have.append(V[i])
    for j in range(N - r,N):
      tmp += V[j]
      have.append(V[j])
    h = len(have)
    have = sorted(have)
    #print(have)
    for k in range(0,d):
      #print(have,sum(have))
      if k >= h:
        break
      if have[k] > 0:
        break
      tmp -= have[k]
    ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

lim = min(N,K)

for l in range(0,lim):
  for r in range(0,lim - l):
    d = K - r - l
    tmp = 0
    have = []
    heapq.heapify(have)
    for i in range(0,l):
      tmp += V[i]
      heapq.heappush(have,V[i])
    for j in range(N - r,N):
      tmp += V[j]
      heapq.heappush(have,V[j])
    h = len(have)
    d = min(d,h)
    for k in range(0,d):
      if have[0] < 0:
        m = heapq.heappop(have)
        tmp -= m
      else:
        break
    ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

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

for l in range(0,K):
  for r in range(0,K - l):
    d = K - r - l
    tmp = 0
    have = []
    if l + r > N:
      continue
    for i in range(0,l):
      tmp += V[i]
      have.append(V[i])
    for j in range(N - r,N):
      tmp += V[j]
      have.append(V[j])
    h = len(have)
    have = sorted(have)
    for k in range(0,d):
      if k >= h:
        break
      if have[k] > 0:
        break
      tmp -= have[k]
      ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from functools import lru_cache

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

for l in range(0,K):
  for r in range(0,K - l):
    d = K - r - l
    tmp = 0
    have = []
    if l + r > N:
      continue
    for i in range(0,l):
      tmp += V[i]
      have.append(V[i])
    for j in range(N - r,N):
      tmp += V[j]
      have.append(V[j])
    h = len(have)
    have = sorted(have)
    for k in range(0,d):
      if k >= h:
        break
      if have[k] > 0:
        break
      tmp -= have[k]
    ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from functools import lru_cache

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

for l in range(0,K):
  for r in range(0,K - l):
    d = K - r - l
    tmp = 0
    have = []
    if l + r > N:
      continue
    for i in range(0,l):
      tmp += V[i]
      have.append(V[i])
    for j in range(N - r,N):
      tmp += V[j]
      have.append(V[j])
    h = len(have)
    have = sorted(have)
    print(have)
    for k in range(0,d):
      if k >= h:
        break
      if have[k] > 0:
        break
      tmp -= have[k]
    ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from functools import lru_cache

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

for l in range(0,K):
  for r in range(0,K - l):
    d = K - r - l
    tmp = 0
    have = []
    if l + r > N:
      continue
    for i in range(0,l):
      tmp += V[i]
      have.append(V[i])
    for j in range(N - r,N):
      tmp += V[j]
      have.append(V[j])
    h = len(have)
    have = sorted(have)
    for k in range(0,d):
      if k >= h:
        break
      if have[k] >= 0:
        break
      tmp -= have[k]
    ans = max(tmp,ans)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
N,K = map(int,input().split())
lim = min(N,K)
V = list(map(int,input().split()))
D = V.copy()
have = []

heapq.heapify(have)

def recur(d,h,n):
  if n == lim:
    #print(h)
    return sum(h)
  else:
    a = d[0]
    b = d[-1]
    reta = recur(d[1:],h + [a],n + 1)
    retb = recur(d[:-1],h + [b],n + 1)
    if len(h) == 0:
      return max(reta,retb)
    else:
      h = sorted(h)
      retc = recur(d,h[1:],n + 1)
      return max(reta,retb,retc)
    
print(recur(D,have,0))

Submission status: Wrong Answer

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

#左からl個、右からr個取り出す
for l in range(K):
    for r in range(K - l):
        if l + r > N: continue
        d = K - l - r
        now = 0
        having = []
        for i in range(l):
            now += V[i]
            having.append(V[i])
        for i in range(N-r, N):
            now += V[i]
            having.append(V[i])

        having.sort()
        #d回まで持っている宝石(価値がマイナス)を戻す
        for i in range(d):
            if i >= len(having): break
            if having[i] >= 0: break
            now -= having[i]
        ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
N, K = map(int, input().split())
d = [int(x) for x in map(int, input().split())]

res = 0
k = min(N, K)
for i in range(k + 1):
    for j in range(k + 1 -i):
        s = d[:i] + d[-j:] if j != 0 else d[:i]
        heapq.heapify(s)
        l = min(k - (i + j), i + j)
        for m in range(l):
            tmp = heapq.heappop(s)
            if tmp > 0:
                heapq.heappush(s, tmp)
                break
        res = max(res, sum(s))
print(res)

Submission status: Wrong Answer

Solution code:
import heapq
N, K = map(int, input().split())
d = [int(x) for x in map(int, input().split())]
e = d[::-1]

res = 0
k = min(N, K)
for i in range(k + 1):
    for j in range(k - i, -1, -1):
        s = d[:i] + e[:j]
        if not s:
            continue
        heapq.heapify(s)
        res = max(res, sum(s))
        l = min(i + j, k - (i + j))
        while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1
print(res)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(0, K):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        npush = K - npop
        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import *
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            heappop(get)
        SUM = sum(get)
        if ans < SUM:ans = SUM
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
   
    n, k, *v = map(int, open(0).read().split())
    w = v[::-1]

    ans = 0
    m = min(n, k)
    for i in range(m + 1):
        for j in range(m + 1 - i):
            g = sorted(v[:i] + w[:j])
            tmp = sum(g[k - i - j:])
            ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
k=min(n,k)
ans=0
for i in range(k+1):
    l=v[:i]
    for j in range(k-i+1):
        r=l+v[n-j:]
        rest=k-i-j
        val=sum(r)
        r=sorted(r)
        if len(r)>0:
            for p in range(min(rest,len(r))):
                if r[p]<0:
                    val-=r[p]
                else:
                    break
        if val>ans:
            ans=val
print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(k+1):
    l=v[:i]
    for j in range(k-i+1):
        r=l+v[n-j:]
        rest=k-i-j
        val=sum(r)
        r=sorted(r)
        if len(r)>0:
            for p in range(min(rest,len(r))):
                if r[p]<0:
                    val-=r[p]
                else:
                    break
        if val>ans:
            ans=val
print(ans)

Submission status: Wrong Answer

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

if k<=n*2:
    ans=0
    for i in range(k+1):
        for j in range(k-i+1):
            v_r=v[:i]
            v_l=v[(n-j):]
            sute_cnt=k-(i+j)
            v_new=v_r+v_l
            v_new.sort()
            # print(i, j, v_r, v_l, sute_cnt, v_new)
            s=sum(v_new)
            if not v_new:
                continue
            for indx in range(len(v_new)):
                if v_new[indx]<0 and sute_cnt>0:
                    s-=v_new[indx]
                    sute_cnt-=1
                else:
                    break
            ans=max(ans,s)
    print(ans)

else:
    ans=0
    for i in range(n):
        if v[i]>=0:
            ans+=v[i]
    print(ans)

Submission status: Wrong Answer

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

if 2*k>=n:
    ans=0
    for i in range(k+1):
        for j in range(k-i+1):
            v_r=v[:i]
            v_l=v[(n-j):]
            sute_cnt=k-(i+j)
            v_new=v_r+v_l
            v_new.sort()
            # print(i, j, v_r, v_l, sute_cnt, v_new)
            s=sum(v_new)
            if not v_new:
                continue
            for indx in range(len(v_new)):
                if v_new[indx]<0 and sute_cnt>0:
                    s-=v_new[indx]
                    sute_cnt-=1
            ans=max(ans,s)
    print(ans)

else:
    ans=0
    for i in range(n):
        if v[i]>=0:
            ans+=v[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int,input().split())
v=list(map(int,input().split()))
cs_l=[0]
cs_r=[0]
cs1,cs2=0,0
for i in range(n):
  cs1+=v[i]
  cs2+=v[-i-1]
  cs_l.append(cs1)
  cs_r.append(cs2)

ans=0
for i in range(k//2,k+1):# 取得する操作をi回行う
  for l in range(i+1): # 左からl個取る
    r=i-l
    if r+l>=n:
      point=cs_r[-1]
      minus=[vi for vi in v if vi<0]
      n_minus=min(n-k,len(minus))
    else:
      point=cs_l[l]+cs_r[r]
      minus=[vi for vi in v[:l] if vi<0]
      if r>0:
        minus+=[vi for vi in v[-r:] if vi<0]
      n_minus=min(k-r-l,len(minus))
    if n_minus>0:
      minus.sort()
      point-=sum(minus[:n_minus])
    ans=max(ans,point)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import heapq

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

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = k-(a+b)
        # print(a, b, c, "", end="")
        # print(a, b, c)
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        print(ans_tmp)
        ans = max(ans, ans_tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import heapq

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

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = max_try-(a+b)
        # print(a, b, c)

        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
        ans = max(ans, ans_tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import heapq

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

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = max_try-(a+b)
        # print(a, b, c, "", end="")

        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)
        for i in range(c):
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            ans_tmp += num

        # print(ans_tmp)
        ans = max(ans, ans_tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): # t個のうち前からs個
    return sorted(v[:s]+v[n-(t-s):],reverse=1)

def f(t,k):
    a=-10**12
    for i in range(t+1):
        a=max(sum(get(t,i)[:2*t-k]),a)
    return a

ans=0
for i in range(k+1):
    for j in range(i+1):
        ans=max(ans,f(j,i))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): # t個のうち前からs個
    return sorted(v[:s]+v[n-(t-s):],reverse=1)

def f(t,k):
    a=-10**12
    for i in range(t+1):
        if 2*t-k>0:
            a=max(sum(get(t,i)[:2*t-k]),a)
    return a

ans=0
for i in range(k+1):
    for j in range(i+1):
        ans=max(ans,f(j,i))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
rev_V = V[::-1]
 
for L in range(0, K+1):
        for R in range(0, K+1):
            if L + R > K:
                break
            
            tmp = V[0:L] + rev_V[0:R]
            tmp.sort()
            tmp = deque(tmp)
            num = K - (L+R)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= num or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                if stone >= 0:
                    tmp.append(stone)
                    break
                cnt += 1
            ans = max(ans, sum(tmp))
 
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
rev_V = V[::-1]

for L in range(0, K+1):
        for R in range(0, K+1):
            if L + R > K:
                break
            
            tmp = V[0:L] + rev_V[0:R]
            tmp.sort()
            tmp = deque(tmp)
            num = K - (L+R)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= num or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                if stone >= 0:
                    tmp.append(stone)
                    break
                cnt += 1
            ans = max(ans, sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
rev_V = V[::-1]

for L in range(0, K+1):
        for R in range(0, K+1):
            if L + R > N:
                break
            
            tmp = V[0:L] + rev_V[0:R]
            tmp.sort()
            tmp = deque(tmp)
            num = K - (L+R)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= num or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                if stone >= 0:
                    tmp.append(stone)
                    break
                cnt += 1
            ans = max(ans, sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]

M = min(K, N)

ans = 0
rev_V = V[::-1]

for a in range(0, M+1):
        for b in range(M - a):
            tmp = V[0:a] + rev_V[0:b]
            tmp.sort()
            tmp = deque(tmp)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= M - (a+b) or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                cnt += 1
            ans = max(ans, sum(tmp))
for i in range(M):
    tmp = V[0:i]
    tmp.sort()
    tmp = deque(tmp)
    stone = -1
    cnt = 0
    while stone < 0:
        if cnt >= M - (a+b) or len(tmp) == 0:
            break
        stone = tmp.popleft()
        if stone >= 0:
            tmp.append(stone)
            break
        cnt += 1
    ans = max(ans, sum(tmp))

for i in range(M):
    tmp = rev_V[0:i]
    tmp.sort()
    tmp = deque(tmp)
    stone = -1
    cnt = 0
    while stone < 0:
        if cnt >= M - (a+b) or len(tmp) == 0:
            break
        stone = tmp.popleft()
        if stone >= 0:
            tmp.append(stone)
            break
        cnt += 1
    ans = max(ans, stone + sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
M = min(K, N)

ans = 0
rev_V = V[::-1]

for a in range(0, M+1):
        for b in range(M - a):
            tmp = V[0:a] + rev_V[0:b]
            tmp.sort()
            tmp = deque(tmp)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= M - (a+b) or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                cnt += 1
            ans = max(ans, sum(tmp))
for i in range(M):
    tmp = V[0:i]
    tmp.sort()
    tmp = deque(tmp)
    stone = -1
    cnt = 0
    while stone < 0:
        if cnt > M - (a+b) or len(tmp) == 0:
            break
        stone = tmp.popleft()
        if stone >= 0:
            tmp.append(stone)
            break
        cnt += 1
    ans = max(ans, sum(tmp))

for i in range(M):
    tmp = rev_V[0:i]
    tmp.sort()
    tmp = deque(tmp)
    stone = -1
    cnt = 0
    while stone < 0:
        if cnt > M - (a+b) or len(tmp) == 0:
            break
        stone = tmp.popleft()
        if stone >= 0:
            tmp.append(stone)
            break
        cnt += 1
    ans = max(ans, stone + sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
max_cnt = -float('inf')
for k in range(K+1):
  for i in range(k+1):
    j = k - i
    le = V[:i]
    ri = V[-j:] if j > 0 else []
    ne = [i for i in le if i < 0]+[j for j in ri if j < 0]
    ne.sort()
    max_cnt = max(sum(le)+sum(ri) - sum(ne[:K-i-j]),max_cnt)
    
print(max_cnt)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
Vrev = V[::-1]
ans = 0
for a in range(N+1):
    for b in range(min(K-a,N-a)+1):
        VV = V[:a] + Vrev[:b]
        VV.sort()
        for k in range(K-a-b):
            ans = max(sum(VV[k+1:]),ans)
print(ans)

Submission status: Wrong Answer

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

for i in range(k+1):
    if i > n:
        continue
    for j in range(i+1):
        li = v[:j]+v[n-(i-j):]
        li.sort()
        li = li[k-i:]
        ma = max(ma,sum(li))
print(ma)

Submission status: Wrong Answer

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

for i in range(k+1):
    if i > n:
        continue
    for j in range(i+1):
        li = v[:j]+v[n-(i-j):]
        li.sort()
        li = li[k-i:]
        ma = max(ma,sum(li))

print(ma)