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:
from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []

def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, k):
    i, j, k_ = node
    if i+j+k_ < k:
        return ((i+1, j, k_), (i, j+1, k_), (i, j, k_+1))
    return tuple()

def function(node, ans, W, V, memo):
    if node in memo:
        return memo[node]
    
    i, j, k = node
    W_, V_ = map(deepcopy, (W, V))
    for _ in range(i):
        if V_:
            heappush(W_, V_.pop())
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    for _ in range(k):
        if W_:
            _ = heappop(W_)
    memo[node] = max(ans, sum(W_))
    return memo[node]

ans = dfs((0, 0, 0), partial(option, k=k), partial(function, W=W, V=V, memo={}))
print(ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from collections import deque
from functools import partial
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []

def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, k):
    i, j = node
    if i+j < k:
        return [(i+1, j), (i, j+1)]
    return []

def function(node, ans, k, W, V):
    i, j = node
    W_, V_ = map(deepcopy, (W, V))
    for _ in range(i):
        if V_:
            heappush(W_, V_.pop())
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    for _ in range(k-i-j):
        if W_:
            _ = heappop(W_)
            ans = max(ans, sum(W_))
    return ans

ans = dfs((0, 0), partial(option, k=k), partial(function, k=k, W=W, V=V))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, k = map(int, input().split())
v = deque(list(map(int, input().split())))
m = min(n, k)
ans = 0
for i in range(m):
    for j in range(m - i):
        w = deque(v)
        a = []
        for _ in range(i):
            p = w.pop()
            a.append(p)
        for _ in range(j):
            p = w.popleft()
            a.append(p)
        a.sort()
        for l in range(min(k - i - j, len(a))):
            if a[l] < 0:
                a[l] = 0
            else:
                break
        if sum(a) > ans:
            ans = sum(a)
print(ans)

Submission status: Wrong Answer

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

for i in range(1,N+1):
    left[i] = left[i-1] + V[i-1]

for i in range(1,N+1):
    right[i] = right[i-1] + V[N-i]


for i in range(min(N+1,K+1)):
    nega = []

    l = i
    for count in range(l):
        if l > 0 and V[count] < 0:
            nega.append(V[count])

    for j in range(min(N+1-i,K+1-i)):

        r = j
        for count in range(1,r+1):
            if r > 0 and V[N-count] < 0:
                nega.append(V[N-count])

        b = max(0,K-i-j)
        now = left[i] + right[j]
        nega.sort()
        s = 0
        for k in range(min(b,len(nega))):
            s += nega[k]
        
        if nega:
            ans.append(now-s)
        else:
            ans.append(now)

print(max(ans))

Submission status: Wrong Answer

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

for i in range(1,N+1):
    left[i] = left[i-1] + V[i-1]

for i in range(1,N+1):
    right[i] = right[i-1] + V[N-i]

for i in range(min(N+1,K+1)):
    nega = []

    l = i
    for count in range(l):
        if l > 0 and V[count] < 0:
            nega.append(V[count])

    for j in range(min(N+1-i,K+1-i)):

        r = j
        for count in range(1,r+1):
            if r > 0 and V[N-count] < 0:
                nega.append(V[N-count])

        b = K-i-j
        now = left[i] + right[j]
        nega.sort()
        s = 0
        for k in range(min(b,len(nega))):
            s += nega[k]
        
        if nega:
            ans.append(now-s)
        else:
            ans.append(now)

print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
INF = 10 ** 9
MOD = 10 ** 9 + 7

def main(): 
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    
    
    ans = 0
    for l in range(n+1):
        for r in range(n+1,-1,-1):
            take = k - r - l
            if take < 0:
                continue
            tot = sum(v[:l])
            if r != 0:
                tot += sum(v[-r:])
            
            if take == 0:
                ans = max(ans,tot)
            else:
                minus = []
                for i in range(l):
                    if v[i] < 0:
                        minus.append(v[i])
                for i in range(n-1,n-r-1,-1):
                    if v[i] < 0:
                        minus.append(v[i])
                minus.sort()
                tot -= sum(minus[:take])
                ans = max(ans,tot)
    
    print(ans)
   
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd
import sys
#input = sys.stdin.readline


"""
import copy
from collections import *
from heapq import *
N,K=map(int,input().split())
VVV=list(map(int,input().split()))
value=0

VV=deque(VVV)
Llst=[[]]
V=copy.copy(VV)
for i in range(min(K,N)):
    Llst.append(copy.copy(Llst[-1]))
    a=V.popleft()
    Llst[-1].append(a)

Rlst=[[]]
V=copy.copy(VV)
for i in range(min(K,N)):
    Rlst.append(copy.copy(Rlst[-1]))
    a=V.pop()
    Rlst[-1].append(a)

#print(Llst,Rlst)
for l in range(min(K,N)):
    for r in range(min(K,N)-l):
        lst=Llst[l]+Rlst[r]
        lst.sort(reverse=True)
        for i in range(K-l-r):
            if lst:
                if lst[-1]<0:
                    lst.pop()
                else:
                    break
            else:
                break
        value=max(value,sum(lst))
print(value)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from copy import copy
n,k = map(int,input().split())
d = deque(list(map(int,input().split())))
maxvalue = 0
for i in range(1,k+1):
    for j in range(k-i):
        jewel = []
        x = copy(d)
        for _ in range(i):
            if x:
                jewel.append(x.popleft())
        for _ in range(j):
            if x:
                jewel.append(x.pop())
        heapq.heapify(jewel)
        for _ in range(k-i-j):
            if jewel:
                a = heapq.heappop(jewel)
                if a >= 0:
                    jewel.append(a)
                    break
        maxvalue = max(maxvalue,sum(jewel))
print(maxvalue)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque
from copy import copy
n,k = map(int,input().split())
d = deque(list(map(int,input().split())))
maxvalue = 0
for i in range(k):
    for j in range(k-i+1):
        jewel = []
        x = copy(d)
        for _ in range(i):
            if x:
                jewel.append(x.popleft())
        for _ in range(j):
            if x:
                jewel.append(x.pop())
        heapq.heapify(jewel)
        for _ in range(k-i-j):
            if jewel:
                a = heapq.heappop(jewel)
                if a >= 0:
                    jewel.append(a)
                    break
        maxvalue = max(maxvalue,sum(jewel))
print(maxvalue)

Submission status: Wrong Answer

Solution code:
#python3
from collections import deque
from heapq import heappop, heappush
def main():
    n, k = map(int, input().split())
    vs = [int(i) for i in input().split()]
    #全ての両端からの取り方を試す.筒に戻す操作は最後にまとめて行っても求めるものに影響はない 
    #左から0からk回まで取り出す.lは取り出す回数を表すので引数はk+1
    #右から0からk-l回まで取り出す.rangeの引数はk-l
    ans = 0
    for l in range(k+1):
        for r in range(k-l+1):
            #手持ちから捨てる操作の回数
            dl = k - l - r
            q = deque(vs)
            hav = []
            #実際にとり出す.
            for _ in range(l):
                if len(q) == 0:
                    continue
                v = q.popleft()
                heappush(hav, v)
            for _ in range(r):
                if len(q) == 0:
                    continue
                u = q.pop()
                heappush(hav, u)
            for _ in range(dl):
                if len(hav) == 0:
                    break
                val = heappop(hav)
                if val > 0:
                    heappush(hav, val)
                ans = max(ans, sum(hav))
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(k+1):
        nl = min(n,l)
        vl = v[:nl]
        s = k-nl
        for r in range(k+1-l):
            vr = v[max(0,n-r):]
            nr = len(vr)
            ns = s-nr
            nv = vl+vr
            nv.sort(reverse = True)
            while ns > 0 and nv and nv[-1] < 0:
                nv.pop()
                ns -= 1
            su = sum(nv)
            if ans < su:
                ans = su
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(k+1):
        nl = min(n,l)
        vl = v[:nl]
        s = k-nl
        for r in range(k+1-l):
            vr = v[max(0,n-r):]
            nr = len(vr)
            ns = s-nr
            nv = vl+vr
            nv.sort(reverse = True)
            while ns > 0 and nv and nv[0] < 0:
                nv.pop()
                ns -= 1
            su = sum(nv)
            if ans < su:
                ans = su
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(k+1):
        nl = min(n,l)
        vl = v[:nl]
        s = k-nl
        for r in range(k+1-l):
            vr = v[max(0,n-r):]
            nr = len(vr)
            ns = s-nr
            nv = vl+vr
            nv.sort(reverse = True)
            while ns > 0 and nv:
                nv.pop()
                ns -= 1
            su = sum(nv)
            if ans < su:
                ans = su
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  cnt=min(n,k)
  ans=0
  for i in range(cnt+1):
    for j in range(cnt-i+1):
      q=[]
      for _i in range(i):
        heapq.heappush(q,l[_i])

      for _j in range(j):
        heapq.heappush(q,l[-(_j+1)])

      ln=len(q)
      for _ in range(min(cnt-i-j,ln)):
        x=heapq.heappop(q)
        if x>0:
          q.append(x)
          break
      # print(q)
      ans=max(ans,sum(q))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  cnt=min(n,k)
  ans=0
  for i in range(cnt+1):
    for j in range(cnt-i+1):
      q=[]
      for _i in range(i):
        heapq.heappush(q,l[_i])

      for _j in range(j):
        heapq.heappush(q,l[-(_j+1)])

      ln=len(q)
      for _ in range(min(cnt-i-j,ln)):
        x=heapq.heappop(q)
      # print(q)
      ans=max(ans,sum(q))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  cnt=min(n,k)
  ans=0
  for i in range(cnt+1):
    for j in range(cnt-i+1):
      q=[]
      for _i in range(i):
        heapq.heappush(q,l[_i])

      for _j in range(j):
        heapq.heappush(q,l[-(_j+1)])

      ln=len(q)
      for _ in range(min(cnt-i-j,ln)):
        x=heapq.heappop(q)
        if x>0:
          q.append(x)
      # print(q)
      ans=max(ans,sum(q))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  cnt=min(n,k)
  ans=0
  for i in range(cnt+1):
    for j in range(cnt-i+1):
      q=[]
      for _i in range(i):
        heapq.heappush(q,l[_i])

      for _j in range(j):
        heapq.heappush(q,l[-(_j+1)])

      ln=len(q)
      for k in range(min(cnt-i-j,ln)):
        x=heapq.heappop(q)
        if x>0:
          q.append(x)
      # print(q)
      ans=max(ans,sum(q))

  return ans

# main()
print(main())

Submission status: Wrong Answer

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

ans = 0
for p in range(min(k, n) + 1):
    for q in range(min(k, n) - p + 1):
        #print("左=",p,"右=",q,"捨=",k - p - q)
        now = 0
        s = []
        for i in range(p):
            #print("左",v[i])
            s.append(v[i])
            now += v[i]
        for i in range(q):
            #print("右", v[n - 1 - i])
            s.append(v[n - 1 - i])
            now += v[n - 1 - i]
        s.sort()
        #print(s)
        for i in range(min(k - p - q, len(s))):
            if v[i] < 0:
                now -= s[i]
        #print("now=", now)
        ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

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

ans = 0
for p in range(min(k, n)):
    for q in range(min(k, n) - p + 1):
        #print("左=",p,"右=",q,"捨=",k - p - q)
        now = 0
        s = []
        for i in range(p):
            #print("左",v[i])
            s.append(v[i])
            now += v[i]
        for i in range(q):
            #print("右", v[n - 1 - i])
            s.append(v[n - 1 - i])
            now += v[n - 1 - i]
        s.sort()
        #print(s)
        for i in range(min(k - p - q, len(s))):
            if v[i] < 0:
                now -= s[i]
        #print("now=", now)
        ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

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

ans = 0
for p in range(min(k + 1, n)):
    for q in range(min(k, n) - p + 1):
        #print("左=",p,"右=",q,"捨=",k - p - q)
        now = 0
        s = []
        for i in range(p):
            #print("左",v[i])
            s.append(v[i])
            now += v[i]
        for i in range(q):
            #print("右", v[n - 1 - i])
            s.append(v[n - 1 - i])
            now += v[n - 1 - i]
        s.sort()
        #print(s)
        for i in range(min(k - p - q, len(s))):
            if v[i] < 0:
                now -= s[i]
        #print("now=", now)
        ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(min(k,n)+1):
    for j in range(min(k,n)-i+1):
        t=sorted(v[:i]+v[:~j:-1])
        for l in range(min(min(n,k)-i-j,len(t))):
            if t[l]<0:
                t[l]=0
        ans=max(ans,sum(t))

print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(min(k,n)+1):
    for j in range(min(k,n)-i+1):
        print(v[:i])
        print(v[:~j:-1])
        t=sorted(v[:i]+v[:~j:-1])
        for l in range(min(min(n,k)-i-j,len(t))):
            if t[l]<0:
                t[l]=0
        ans=max(ans,sum(t))

print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(min(k,n)+1):
    for j in range(min(k,n)-i+1):
        t=sorted(v[:i]+v[:~j:-1])
        for l in range(min(min(n,k),len(t))):
            if t[l]<0:
                t[l]=0
        ans=max(ans,sum(t))

print(ans)

Submission status: Wrong Answer

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

D=list(map(int,input().split()))
ans=0

for i in range(k+1):
    for j in range(min(n,k)+1-i):
        if j==0:
            s=D[:i]
        else:    
            s=D[:i]+D[-j:]
        s.sort()        
        for l in range(min(min(n,k)-i-j,len(s))):
            if s[l]<0:
                s[l]=0

        ans=max(ans,sum(s))

print(ans)

Submission status: Wrong Answer

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

D=list(map(int,input().split()))
ans=0

for i in range(min(n,k)+1):
    for j in range(min(n,k)+1-i):
        if j==0:
            s=D[:i]
        else:    
            s=D[:i]+D[-j:]
        s.sort()
        
        for l in range(min(min(n,k)-i-j,len(s))):
            if s[l]<0:
                s[l]=0
        ans=max(ans,sum(s))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from heapq import heappop
import heapq

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
V = LIST()
V = deque(V)

# R = A + B
max_R = min(N, K)
# A, B 全探索
ans = 0
for R in range(1, max_R+1):
    for i in range(0, R+1):
        minus_count = 0

        v = deepcopy(V)
        #print("v:", v)
        pocket = []
        heapq.heapify(pocket)
        A = i
        B = R - i
        #print("a,b:", A, B)
        for a in range(A):
            a_pop = v.popleft()
            if a_pop < 0:
                minus_count += 1
            pocket.append(a_pop)
        for b in range(B):
            b_pop = v.pop()
            if b_pop < 0:
                minus_count += 1
            pocket.append(b_pop)
        # abundant abandon
        dump = K - R
        # pocketの中に捨てなくていいものもあるとき
        dump_time = min(minus_count, dump)
        #print("minus_count:", minus_count)
        #print("time:", dump_time)
        #print("pocket_b:", pocket)

        pocket.sort(reverse=True)
        # OK
        #if dump_time > 0:
        #    for d in range(dump_time):
        #        x = pocket.pop()
        
        # NG
        if dump_time > 0:
            for d in range(dump_time):
                x = heappop(pocket) 

        wa = sum(pocket)
        #print("pocket_c:", pocket)
        if ans <= wa:
            ans = wa
        #print(ans)
print(ans)

Submission status: Wrong Answer

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


def solve(N: int, K: int, V: "List[int]"):
    m = 0
    for k in range(0,K+1):
        for l in range(k+1):
            r = k - l
            d = K - k
            arr = sorted(V[:l]+V[N-(k-l):])
            sum = 0
            n = []
            for a in arr:
                if a < 0 and d > 0:
                    d-=1
                    continue 
                sum += a
                n.append(a)
            # print(f"{arr}-Del:{K-k}->{n}->{sum}")
            m = max(sum,m)
    print(m)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def solve(N: int, K: int, V: "List[int]"):
    m = 0
    for k in range(0,K+1):
        for l in range(k+1):
            r = k - l
            d = K - k
            arr = sorted(V[:l]) if r == 0 else sorted(V[:l]+V[-r:])
            sum = 0
            n = []
            for a in arr:
                if a < 0 and d > 0:
                    d-=1
                    continue 
                sum += a
                n.append(a)
            m = max(sum,m)
    print(m)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def solve(N: int, K: int, V: "List[int]"):
    m = 0
    for k in range(1,K+1):
        for l in range(k+1):
            r = k - l
            d = K - k
            arr = sorted(V[:l]) if r == 0 else sorted(V[:l]+V[-r:])
            sum = 0
            n = []
            for a in arr:
                if a < 0 and d > 0:
                    d-=1
                    continue 
                sum += a
                n.append(a)
            m = max(sum,m)
    print(m)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
from collections import deque
#K=min(N,K)
ans=-10**10
for k in range(0,min(K+1, N)):
    for n in range(0,k+1):
        A=V[0:n]+V[N-(k-n):]
        if len(A)==0:
            A=[0]
        A.sort()
        tmp=sum(A)
        for i in range(0, min(K-k,len(A))):
            if A[i]<0:
                tmp-=A[i]
            else:
                break
        if ans<tmp:
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
for i in range(k+1):
    a_cp=a.copy()

    tmp=a_cp[:i]
    a_cp[i:]
    for j in range(k+1-i):
        cal=tmp+a_cp[max(n-j,0):]
        
        heapq.heapify(cal)
        ans=max(ans,sum(cal))
        for _ in range(k-i-j):
            if len(cal)>0:
                heapq.heappop(cal)
                ans=max(ans,sum(cal))
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
for i in range(k+1):
    a_cp=a.copy()

    tmp=a_cp[:i]
    a_cp[i:]
    for j in range(k+1-i):
        cal=tmp+a_cp[min(n-j,0):]
        
        heapq.heapify(cal)
        ans=max(ans,sum(cal))
        for _ in range(k-i-j):
            if len(cal)>0:
                heapq.heappop(cal)
                ans=max(ans,sum(cal))
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
for i in range(k+1):
    tmp=a[:i]
    for j in range(k+1-i):
        cal=tmp+a[n-j:]
        
        heapq.heapify(cal)
        ans=max(ans,sum(cal))
        for _ in range(k-i-j):
            if len(cal)>0:
                heapq.heappop(cal)
                ans=max(ans,sum(cal))
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
left=[0 for _ in range(k+1)]
right=[0 for _ in range(k+1)]
for i in range(k+1):
    a_tmp=a[:i]
    heapq.heapify(a_tmp)
    left[i]=max(left[i],sum(a_tmp))
    for j in range(k-i):
        if len(a_tmp)<1:
            break
        heapq.heappop(a_tmp)
        left[i+j+1]=max(left[i+j+1],sum(a_tmp))


for i in range(k+1):
    a_tmp=a[n-i:]
    heapq.heapify(a_tmp)
    right[i]=max(right[i],sum(a_tmp))
    for j in range(k-i):
        if len(a_tmp)<1:
            break
        heapq.heappop(a_tmp)
        right[i+j+1]=max(right[i+j+1],sum(a_tmp))
ans=[0 for _ in range(k+1)]
for i in range(k+1):
    ans[i]+=left[i]
    ans[i]+=right[k-i]


print(max(ans))