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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

ans = 0
for a in range(n+1):
    for b in range(n+1):
        if a+b >= n:
            continue
        left = lst1[:a]
        right = lst1[n-b:]
        res = left + right
        rest = k - (a + b)
        if rest <= 0:
            pass
        else:
            res.sort()
            while rest:
                if len(res) == 0:
                    break
                if res[0] < 0:
                    del res[0]
                    rest -=1
                else:
                    break
        ans = max(ans,sum(res))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

ans = 0
for a in range(n+1):
    for b in range(n+1):
        if a+b >= n:
            continue
        left = lst1[:a]
        right = lst1[n-b:]
        res = left + right
        rest = k - (a + b)
        if rest <= 0:
            break
        else:
            res.sort()
            while rest:
                if len(res) == 0:
                    break
                if res[0] < 0:
                    del res[0]
                    rest -=1
                else:
                    break
        ans = max(ans,sum(res))

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    m = 0
    for i in range(min(N, K)+1):
        for j in range(min(K - i, N-i)):
            t = V[:i] + V[N-j:]
            t.sort()
            ind = bisect.bisect_right(t, 0)
            d = min((i+j), K - i - j, ind)
            m = max(m, sum(t[d:]))
    print(m)
main()

Submission status: Wrong Answer

Solution code:
import bisect
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    m = 0
    for i in range(min(N, K)+1):
        for j in range(K - i):
            t = V[:i+1] + V[N-j:]
            t.sort()
            ind = bisect.bisect_left(t, 0)
            d = min((i+j), K - i - j, ind)
            m = max(m, sum(t[d:]))
    print(m)
main()

Submission status: Wrong Answer

Solution code:
import bisect
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    m = 0
    for i in range(min(N, K)+1):
        for j in range(K - i):
            t = V[:i] + V[N-j:]
            t.sort()
            ind = bisect.bisect_left(t, 0)
            d = min((i+j), K - i - j, ind)
            m = max(m, sum(t[d:]))
    print(m)
main()

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
vvv = list(map(int, input().split(' ')))
# 4^100 = 1.6e60 TLE
memo = {}
def f(current, d, i1, i2, l):
    key = (current, i1, i2, l)
    if key not in memo:
        if current == k:
            return l
        ans = -1e9-1
        if i1 < i2:
            ans = max(ans, f(current + 1, d, i1+1, i2, l + d[i1]))
            ans = max(ans, f(current + 1, d, i1, i2-1, l + d[i2]))
            # 即捨てる
            if d[0] < 0 and current + 2 <= k:
                ans = max(ans, f(current + 2, d, i1+1, i2, l))
            if d[-1] < 0 and current + 2 <= k:
                ans = max(ans, f(current + 2, d, i1, i2-1, l))
        else:
            return l
        ans = max(ans, f(current + 1, d, i1, i2, l))

        memo[key] = ans
    return memo[key]
print(f(0, vvv, 0, len(vvv)-1, 0))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
vvv = list(map(int, input().split(' ')))
# 4^100 = 1.6e60 TLE

memo = {}  # 意味ないかも
def f(current, d, l):
    key = (current, sum(l))
    if key not in memo:
        if current == k:
            return sum(l)
        if current > k:
            return -1e9-1
        ans = -1e9-1
        if d:
            ans = max(ans, f(current + 1, d[1:], l + [d[0]]))
            ans = max(ans, f(current + 1, d[:-1], l + [d[-1]]))
            # 即捨てる
            if d[0] < 0:
                ans = max(ans, f(current + 2, d[1:], l))
            if d[-1] < 0:
                ans = max(ans, f(current + 2, d[:-1], l))
        ans = max(ans, f(current + 1, d, l))

        memo[key] = ans
    return memo[key]
print(f(0, vvv, []))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(101)

n, k = list(map(int, input().split()))
vs = list(map(int, input().split()))
memo = {}


def doit(l, r, kk):
    if kk == 0 or l > r:
        return 0
    if (l, r) not in memo:
        memo[(l, r)] = max(vs[l] + doit(l + 1, r    , kk - 1),
                           vs[r] + doit(l    , r - 1, kk - 1),
                                   doit(l + 1, r    , kk - 2) if kk > 1 else 0,
                                   doit(l    , r - 1, kk - 2) if kk > 1 else 0)
    return memo[(l, r)]


print(doit(0, n - 1, k))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
d = list(map(int, input().split()))
ans = 0
for le in range(min([K+1,N])):
    for ri in range(min([K-le+1,N])):
        if (le+ri)>K: break
        if (le+ri)>=N:
            cur = d
        else:
            cur = (d[0:le] + d[(N-ri):] if le!=0 else d[(N-ri):])
        t = K - le - ri
        for ti in range(t):
            if not cur: break
            mi = min(cur)
            if mi>0: break
            cur.remove(mi)
        ans = max(ans, sum(cur))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
d = list(map(int, input().split()))
ans = 0
for le in range(K+1):
    for ri in range(K-le+1):
        if (le+ri-1)==K: break
        cur = (d[0:le] + d[(N-ri):] if le!=0 else d[(N-ri):])
        t = K - le - ri
        for ti in range(t):
            if not cur: break
            mi = min(cur)
            if mi>0: break
            cur.remove(mi)
        ans = max(ans, sum(cur))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
vab=[]
ans=0
for i in range(min(n,k)+1):
  for j in range(min(n,k)-i+1):
    i-=1
    j-=1
    if i<0 and j<0:
      continue
    elif i<0 and j>=0:
      vab=v[n-j-1:]
    elif j<0:
      vab=v[:i+1]
    else:
      vab=v[:i+1]+v[n-j-1:]
    vab.sort()
    if vab[0]<0 and k-len(vab)>0:
      for l in range(min(k-len(vab),len(vab))):
        if vab[l]<0:
          vab[l]=0
    ans=max(ans,sum(vab))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq

N, K=map(int, input().split())
V=list(map(int, input().split()))
lim=min(N, K)
ans=0
for num in range(lim+1):
    for A in range(num+1):
        B=num-A
        d=deque(V)
        hands=[]
        for i in range(A):
            hands.append(d.popleft())
        for i in range(B):
            hands.append(d.pop())
        heapq.heapify(hands)
        cnt=lim-(A+B)
        while len(hands) and hands[0]<0 and cnt>0:
            cnt-=1
            heapq.heappop(hands)
        ans=max(ans, sum(hands))
print(ans)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
# なぜか2つWAが取れない

n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -(10 ** 9)
for a in range(n):
    for b in range(n):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -(10 ** 9) else 0)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
# なぜか2つWAが取れない

n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -(10 ** 15)
for a in range(n):
    for b in range(n):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -10 ** 10 else 0)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
# なぜか2つWAが取れない

n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 15
for a in range(n):
    for b in range(n):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -10 ** 10 else 0)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(n):
    for b in range(n):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -10 ** 10 else 0)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k)):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -10 ** 10 else 0)

Submission status: Wrong Answer

Solution code:
# 捨てるのは最後でいいのはわかったけど実装が浮かばず、結局DPとか必要なんじゃないの？と思った
# ら、単純に左からa個右からb個取ったら捨てる操作はk-(a+b)だから〜、というらしい
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k)):
        if a + b <= min(n, k):
            arr = sorted(v[:a] + v[n - b :])
            tmp = 0
            for ai in range(k - (a + b)):
                if ai >= len(arr):
                    break
                if arr[ai] < 0:
                    tmp += arr[ai]
                else:
                    break
            # print("a, b = {}, {}".format(a, b))
            # print("now arr: {}".format(arr))
            # ans = max(ans, sum(arr) - tmp)
            # print("now ans = {}".format(ans))
        else:
            break
print(ans if ans != -10 ** 10 else 0)

Submission status: Wrong Answer

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

ans = -1e9
for leftops in range(min(N,K)):
  for rightops in range(min(N,K)-leftops):
    X = V[:leftops] if leftops > 0 else []
    X = X + V[-rightops:] if rightops > 0 else X
    work = sum(X)
    #print(leftops,rightops,work)
    #print(X)
    X.sort()
    
    if len(X) == 0:
      ans = max(ans,0)
      continue
      
    for k in range(min(K-leftops-rightops,len(X))):
      if X[k] < 0:
        work -= X[k]
      else:
        break
    ans = max(ans,work)
    
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify

N, K = map(int, input().split())
V = list(map(int, input().split()))
W = V[::-1]

patterns = []

for i in range(1, N+1): # 何個取り出すパターンなのか

  for left in range(i+1): # 左側から何個抽出するのか
    right = i - left
    heap = V[:left] + W[:right]
    heapify(heap)
    patterns.append(heap)

  
ans = 0

for pattern in patterns:
  length = len(pattern)
  subable = K - length

  if subable <= 0:
    continue
  
  while subable > 0 and pattern:
    useless = heappop(pattern)
    subable -= 1
    if useless > 0:
      heappush(pattern, useless)
      ans = max(ans, sum(pattern))
      break
  else:
    ans = max(ans, sum(pattern))

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify

N, K = map(int, input().split())
V = list(map(int, input().split()))
W = V[::-1]

patterns = []

for i in range(1, N+1): # 何個取り出すパターンなのか

  for left in range(i+1): # 左側から何個抽出するのか
    right = i - left
    heap = V[:left] + W[:right]
    heapify(heap)
    patterns.append(heap)

  
ans = 0

for pattern in patterns:
  length = len(pattern)
  subable = K - length
  
  if subable < 0:
    continue
  
  for i in range(subable):
    if len(pattern) == 0:
      break
    
    useless = heappop(pattern)
    if useless >= 0:
      heappush(pattern, useless)
      break
  else:
    ans = max(ans, sum(pattern))

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(K+1):
    for r in range(K-l):
        if l + r > N:
            continue

        score = 0
        S = []

        for i in range(l):
            score += V[i]
            S.append(V[i])

        for i in range(r):
            score += V[-(i+1)]
            S.append(V[-(i+1)])

        d = K - l - r
        S.sort()
        for i in range(d):
            if i >= len(S):
                break
            if S[i] > 0:
                break
            score -= S[i]

        ans = max(ans, score)

print(ans)

Submission status: Wrong Answer

Solution code:
n,K=map(int,input().split())
K=min(n,K)
import math
v=list(map(int,input().split()))
li=[0]
for k in range(1,K+1):
  for i in range(math.ceil(k/2),k+1):
    #i個取り出してk-i個戻すについての全探索
    if i==k-i:
      continue
    else:
      for j in range(i+1):
        #左からj個取り出し，右からi-j個取り出す全探索
        if j == i:
          u = v[:i]
          u.sort()
          if k==i:
            minus=0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) - minus
          li.append(pt)
        elif j == 0:
          u = v[-i:]
          u.sort()
          if k==i:
            minus=0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) - minus
          li.append(pt)
        else:
          u = v[:j] + v[j-i:]
          u.sort()
          if k == i:
            minus = 0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) -minus
          li.append(pt)
print(max(li))

Submission status: Wrong Answer

Solution code:
n,K=map(int,input().split())
import math
v=list(map(int,input().split()))
li=[0]
for k in range(1,K+1):
  for i in range(math.ceil(k/2),k+1):
    #i個取り出してk-i個戻すについての全探索
    if i==k-i:
      continue
    else:
      for j in range(i+1):
        #左からj個取り出し，右からi-j個取り出す全探索
        if j == i:
          u = v[:i]
          u.sort()
          if k==i:
            minus=0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) - minus
          li.append(pt)
        elif j == 0:
          u = v[-i:]
          u.sort()
          if k==i:
            minus=0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) - minus
          li.append(pt)
        else:
          u = v[:j] + v[j-i:]
          u.sort()
          if k == i:
            minus = 0
          else:
            minus = sum(u[:k-i])
          pt = sum(u) -minus
          li.append(pt)
print(max(li))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
import math
v=list(map(int,input().split()))
li=[0]
for i in range(math.ceil(k/2),k+1):
  #i個取り出してk-i個戻すについての全探索
  if i==k-i:
    li.append(0)
  else:
    for j in range(i+1):
      #左からj個取り出し，右からi-j個取り出す全探索
      if j == i:
        u = v[:i]
        u.sort()
        if k==i:
          minus=0
        else:
          minus = sum(u[:k-i])
        pt = sum(u) - minus
        li.append(pt)
      elif j == 0:
        u = v[-i:]
        u.sort()
        if k==i:
          minus=0
        else:
          minus = sum(u[:k-i])
        pt = sum(u) - minus
        li.append(pt)
      else:
        u = v[:j] + v[j-i:]
        u.sort()
        if k == i:
          minus = 0
        else:
          minus = sum(u[:k-i])
        pt = sum(u) -minus
        li.append(pt)
print(max(li))

Submission status: Wrong Answer

Solution code:
import heapq
import copy

N,K=map(int,input().split())
V=list(map(int,input().split()))
V.append(0)#右から撮る時用

S=[0]*(N+1)#累積和
for i in range(N):
    S[i+1]=S[i]+V[i]

#左からi番目まで見たときに，持っている負の宝石
left=[[] for _ in range(N+1)]
for i in range(N):
    left[i+1]=copy.deepcopy(left[i])
    if V[i]<0:
        left[i+1].append(V[i])
       
        
ans=0
#左からi個，右からj個とったときについて考える
for i in range(N+1):
    temp=S[i]
    rem=copy.deepcopy(left[i])
    heapq.heapify(rem)
    for j in range(N+1-i):
        if i+j>K:
            break
        temp+=S[-1]-S[N-j]
        if V[N-j]<0:
            heapq.heappush(rem,V[N-j])
        ret=K-(i+j)#負の宝石を返却できる回数
        if ret>0:
            for k in range(min(ret,len(rem))):
                temp-=rem[k]
        ans=max(ans,temp)
        temp=S[i]
        
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a[::-1]

ans = 0
for left in range(n):
    for right in range(n - left):
        if left + right > k:
            break
        operate = k - left - right
        v = a[:left] + b[:right]
        v.sort(reverse=True)
        for _ in range(operate):
            if len(v) == 0 or v[-1] >= 0:
                break
            v.pop()
        ans = max(ans, sum(v))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import numpy as np
N, K = map(int,input().split())
v = list(map(int,input().split()))
res = 0
if N==1 and K==1:
    print(max([0,v[0]]))
    exit()

for i in range(N):
    for j in range(N):
        if 1<=i+j<=K and i+j<=N:
            tmp = 0
            gem = []
            heapq.heapify(gem)
            for k in range(i):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            for k in range(N-1,(N-1)-(j),-1):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            rest = K - (i+j)
            while 1:
                if tmp != 0 and rest > 0:
                    rest -= 1
                    elem = heapq.heappop(gem)
                    if elem >= 0:
                        heapq.heappush(gem, elem)
                        break
                    tmp -= elem
                else:
                    break
            res = max([res,tmp])
print(int(res))

Submission status: Wrong Answer

Solution code:
import heapq
import numpy as np
N, K = map(int,input().split())
v = list(map(int,input().split()))
res = 0
if N==1 and K==1:
    print(v[0])
    exit()

for i in range(N):
    for j in range(N):
        if 1<=i+j<=K and i+j<=N:
            tmp = 0
            gem = []
            heapq.heapify(gem)
            for k in range(i):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            for k in range(N-1,(N-1)-(j),-1):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            rest = K - (i+j)
            while 1:
                if tmp != 0 and rest > 0:
                    rest -= 1
                    elem = heapq.heappop(gem)
                    if elem >= 0:
                        heapq.heappush(gem, elem)
                        break
                    tmp -= elem
                else:
                    break
            res = max([res,tmp])
print(int(res))

Submission status: Wrong Answer

Solution code:
import heapq
import numpy as np
N, K = map(int,input().split())
v = list(map(int,input().split()))
r = np.zeros((N+1))
r[1:] = v
res = 0
for i in range(N):
    for j in range(N):
        if 1<=i+j<=K and i+j<=N:
            tmp = 0
            gem = []
            heapq.heapify(gem)
            for k in range(i):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            for k in range(N-1,(N-1)-(j),-1):
                heapq.heappush(gem, v[k])
                tmp += v[k]
            rest = K - (i+j)
            while 1:
                if tmp != 0 and rest > 0:
                    rest -= 1
                    elem = heapq.heappop(gem)
                    if elem >= 0:
                        heapq.heappush(gem, elem)
                        break
                    tmp -= elem
                else:
                    break
            res = max([res,tmp])
print(res)

Submission status: Wrong Answer

Solution code:
# ABC128 D - equeue
n,k=map(int,input().split())
v=list(map(int,input().split()))
c=min(n,k)
ans=0

for a in range(c+1):
    for b in range(c-a+1):
        t=v[:a]+v[n-b:]
        t.sort()
        for i in range(k-(a+b)):
            if len(t) and t[0]<0:
                t.pop(0)
            ans=max(ans,sum(t))
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    V = LI()
    in_deq = deque()
    ans = 0
    for v in V:
        in_deq.append(v)
    for toru in range(K+1):
        for left_toru in range(toru+1):
            deq = copy.copy(in_deq)
            modosu = K - toru
            temochi = []
            right_toru = toru - left_toru
            while len(deq) and left_toru > 0:
                x = deq.popleft()
                temochi.append(x)
                left_toru -= 1
            while len(deq) and right_toru > 0:
                x = deq.pop()
                temochi.append(x)
                right_toru -= 1
            temochi = sorted(temochi)[::-1]
            while len(temochi) > 0 and modosu > 0:
                extra = temochi[-1]
                temochi = temochi[:-1]
                modosu -= 1
            ans = max(ans, sum(temochi))
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    V = LI()
    in_deq = deque()
    ans = 0
    for v in V:
        in_deq.append(v)
    for toru in range(K):
        for left_toru in range(toru+1):
            deq = copy.copy(in_deq)
            modosu = K - toru
            temochi = []
            right_toru = toru - left_toru
            while len(deq) and left_toru > 0:
                x = deq.popleft()
                temochi.append(x)
                left_toru -= 1
            while len(deq) and right_toru > 0:
                x = deq.pop()
                temochi.append(x)
                right_toru -= 1
            temochi = sorted(temochi)[::-1]
            while len(temochi) > 0 and modosu > 0:
                extra = temochi[-1]
                temochi = temochi[:-1]
                modosu -= 1
            ans = max(ans, sum(temochi))
    print(ans)
main()

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 l in range(m+1):
  L=V[:l]
  for r in range(m-l):
    k=m-(l+r)
    if r==0:
      R=[]
    else:
      R=V[-r:]
    GET=L+R
    GET=sorted(GET)
    if k>0:
      if k>=len(GET) or GET[k]>=0:
        for i in range(len(GET)):
          if GET[i]<0:
            GET[i]=0
          else:
            break
      else:
        GET=GET[k:]
    ans=max(ans,sum(GET))
    #print(ans,L,R,k)
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 l in range(m+1):
  L=V[:l]
  for r in range(m-l):
    k=m-(l+r)
    if r==0:
      R=[]
    else:
      R=V[-r:]
    GET=L+R
    GET=sorted(GET)
    if k>0:
      if k>=l+r or GET[k]>=0:
        for i in range(len(GET)):
          if GET[i]<0:
            GET[i]=0
          else:
            break
      else:
        GET=GET[k:]
    ans=max(ans,sum(GET))
    #print(ans,L,R,k)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

def calculate(arr,n,k):
    max = 0

    maxSum = min(n,k)
    # A+B的和


    for i in range(0,maxSum+1):

        for l in range(0,i+1):
            larr = arr[:l]
            if i-l == 0:
                rarr = np.array([])
            else:
                rarr = arr[-(i-l):]

            ab = np.concatenate([larr,rarr])


            abs = np.sort(ab,axis=0)

            # 扔东西
            # k - i

            thaw = (k-i)

            if thaw > len(abs):
                continue

            abs[:thaw] = 0

            tmpSum =  np.sum(abs,axis=0)

            if tmpSum > max:
                max = tmpSum

    return int(max)



S = input().split(" ")
N = int(S[0])
K = int(S[1])

arr = [int(s) for s in input().split(" ")]
arr = np.array(arr)

result = calculate(arr,N,K)

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

def calculate(arr,n,k):
    max = 0

    maxSum = min(n,k)
    # A+B的和

    for i in range(0,maxSum):

        for l in range(0,i+1):
            larr = arr[:l]
            if i-l == 0:
                rarr = np.array([])
            else:
                rarr = arr[-(i-l):]


            ab = np.concatenate([larr,rarr])


            abs = np.sort(ab,axis=0)

            # 扔东西
            # k - i

            thaw = (k-i)

            if thaw > len(abs):
                continue

            abs[:thaw] = 0

            tmpSum =  np.sum(abs,axis=0)

            if tmpSum > max:
                max = tmpSum

    return int(max)



S = input().split(" ")
N = int(S[0])
K = int(S[1])

arr = [int(s) for s in input().split(" ")]
arr = np.array(arr)

result = calculate(arr,N,K)

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

def calculate(arr,n,k):
    max = 0

    maxSum = min(n,k)
    # A+B的和

    for i in range(0,maxSum):

        for l in range(0,i+1):
            larr = arr[:l]
            if i-l == 0:
                rarr = np.array([])
            else:
                rarr = arr[-(i-l):]


            ab = np.concatenate([larr,rarr])


            abs = np.sort(ab,axis=0)

            # 扔东西
            # k - i

            thaw = (k-i)

            if len(abs) > thaw:
                abs[:thaw] = 0
            else:
                abs[:] = 0

            tmpSum =  np.sum(abs,axis=0)

            if tmpSum > max:
                max = tmpSum

    return int(max)



S = input().split(" ")
N = int(S[0])
K = int(S[1])

arr = [int(s) for s in input().split(" ")]
arr = np.array(arr)

result = calculate(arr,N,K)

print(result)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for i in range(K//2):
        for j in range(min(K-i+1,N)):
            l=V[:j]+V[max(j,N-(K-i-j)):]
            l.sort()
            for k in range(i+1):
                ans=max(ans,sum(l[k:]))
    print(ans)
            
        

if __name__ == '__main__':
    main()