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 heapq
import copy
n,k = map(int,input().split())
v_ls = list(map(int, input().split()))
i_leftminus = {}
i_sum = [0] * n
minus_heapq = []

Sum = 0
for i in range(n):
    if v_ls[i] < 0:
        heapq.heappush(minus_heapq,v_ls[i])
    i_leftminus[i] = copy.copy(minus_heapq)
    
    Sum += v_ls[i]
    i_sum[i] = Sum

# fromleft[i]:=i回左から操作した時のmaxvalue を求める
fromleft = [-float('inf')] * (k+1)
fromleft[0] = 0

for i in range(n):
    # 途中ごとに、何回操作してvalueいくつか、を求めてfromleftを更新
    times = i+1
    q = i_leftminus[i]
    Sum = i_sum[i]
    if times <= k:
        fromleft[times] = max(Sum,fromleft[times])
    else:
        break
    while q:
        Sum -= heapq.heappop(q)
        times += 1
        if times > k:
            break
        fromleft[times] = max(Sum,fromleft[times])
#print(fromleft)

minus_heapq_r = []
Sum_r = 0
i_rightminus = {}
i_sumr = [0] * n
t = 0
for i in range(n-1,-1,-1):
    if v_ls[i] < 0:
        heapq.heappush(minus_heapq_r,v_ls[i])
    i_rightminus[t] = copy.copy(minus_heapq_r)
    
    Sum_r += v_ls[i]
    i_sumr[t] = Sum_r
    t+= 1

# fromleft[i]:=i回左から操作した時のmaxvalue を求める
fromright = [-float('inf')] * (k+1)
fromright[0] = 0

for i in range(n):
    # 途中ごとに、何回操作してvalueいくつか、を求めてfromleftを更新
    times = i+1
    q = i_rightminus[i]
    Sum = i_sumr[i]
    if times <= k:
        fromright[times] = max(Sum,fromright[times])
    else:
        break
    while q:
        Sum -= heapq.heappop(q)
        times += 1
        if times > k:
            break
        fromright[times] = max(Sum,fromright[times])
#print(fromright)

ans = 0
for t in range(k+1):
    for l in range(t+1):
        now = fromleft[l] + fromright[t-l]
        ans = max(ans, now)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc128/tasks/abc128_d

n, k = map(int, input().strip().split())
value = list(map(int, input().strip().split()))
res = 0
for l in range(k):
    for r in range(k-l):
        if l + r > n:
            continue
        d = k - l - r
        now = 0
        s = []
        for i in range(l):
            now += value[i]
            s.append(value[i])
        for i in range(n-r, n):
            now += value[i]
            s.append(value[i])
        s.sort()
        for i in range(d):
            if i >= len(s):
                break
            if s[i] > 0:
                break
            now -= s[i]
        res = max(res, now)

print(res)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r,ans=min(N,K),0
for a in range(r+1):
  for b in range(r-a+1):
    tmp,minus=0,[]
    for i in range(a):
      tmp+=V[i]
      if V[i]<0:
        minus.append(V[i])
    for j in range(b):
      tmp+=V[N-j-1]
      if V[N-j-1]<0:
        minus.append(V[N-j-1])
    tmp-=sum(sorted(minus)[:r-(a+b)])
    ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r,ans=min(N,K),0
for a in range(r+1):
  for b in range(r-a+1):
    tmp,minus=0,[]
    for i in range(a):
      tmp+=V[i]
      if V[i]<0:
        minus.append(V[i])
    for j in range(b):
      tmp+=V[j]
      if V[j]<0:
        minus.append(V[j])
    tmp-=sum(sorted(minus)[:r-(a+b)])
    ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

ans = 0

for a in range(K + 1):
    for b in range(K - a + 1):
        # 左からa個、右からb個取り出す
        get = []
        get += V[:a]
        get += list(reversed(V))[:b]
        # (a+b)回操作した時点でのスコア
        tmp = sum(get)

        # まだ操作出来るなら、負の数を取り除く
        get.sort()
        for i in range(min(K - a - b, len(get))):
            if get[i] < 0:
                tmp += -get[i]
        ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heapify


N, K, *V = map(int, open(0).read().split())

# i: 操作回数
# j: iのうち戻す回数
# k: i - j のうち左から取り出す範囲 [0, k)
ans = 0
for i in range(K + 1):
    res = 0
    pq = []
    for j in range(i + 1):
        for k in range(i - j + 1):
            put_out_cnt = j
            tmp = V[:k]
            l = i - j - k
            if l > 0:
                tmp += V[-l:]
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=0
for a in range(min(n,k)):
  for b in range(min(n,k)-a):
    if a+b>k:
      continue
    lst=(v[:a]+v[n-b-1:])
    #print(lst)
    val=sum(lst)
    if k-a-b==0:
      ans=max(ans,val)
    else:
      lst.sort()
      r=k-a-b
      if lst==[]:
        break        
      for i in range(r):
        if i+1>=len(lst):
          break
        if lst[i]<0:
          val-=lst[i]
        else:
          break
      ans=max(ans,val)
    #print(a,b,val)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=0
for a in range(min(n,k)):
  for b in range(min(n,k)-a):
    if a+b>k:
      continue
    lst=(v[:a]+v[n-b:])
    #print(lst)
    val=sum(lst)
    if k-a-b==0:
      ans=max(ans,val)
    else:
      lst.sort()
      r=k-a-b
      if lst==[]:
        break        
      for i in range(r):
        if i+1>=len(lst):
          break
        if lst[i]<0:
          val-=lst[i]
        else:
          break
      ans=max(ans,val)
    #print(a,b,val)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(r+1):
    for j in range(r-i+1):
        tmp,minus=0,[]
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(r+1):
    for j in range(r-i):
        tmp,minus=0,[]
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = inpl()
res = 0
for ope in range(k+1):
    for pick in range(ope+1):
        if pick > n: continue
        back = ope - pick
        for lp in range(pick+1):
            rp = pick - lp
            # print(ope,back,lp,rp)
            q = []
            for i in range(lp):
                q.append(a[i])
            for i in range(rp):
                q.append(a[-i-1])
            q.sort()
            ans = sum(q)
            for i in range(back):
                if i >= pick: break
                ans -= q[i]
                res = max(res,ans)
print(res)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(r+1):
    for j in range(r-i+1):
        tmp,minus=0,[]
        for ii in range(min(i,N)):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(min(j,N)):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(min(r+1,N)):
    for j in range(min(r-i+1,N)):
        tmp,minus=0,[]
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(min(r+1,N)):
    for j in range(r-i):
        tmp,minus=0,[]
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(r):
    for j in range(r-i):
        tmp,minus,m=0,[],0
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
                m+=1
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
                m+=1
        tmp-=sum(sorted(minus)[:min(r-(i+j),m)])
        ans=max(ans,tmp)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for i in range(r):
    for j in range(r-i):
        tmp,minus=0,[]
        for ii in range(i):
            a=V[ii]
            tmp+=a
            if a<0:
                minus.append(a)
        for jj in range(j):
            a=V[N-1-jj]
            tmp+=a
            if a<0:
                minus.append(a)
        tmp-=sum(sorted(minus)[:r-(i+j)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k, *a = map(int, open(0).read().split())

ans = 0
for l in range(0, k + 1):
    for r in range(0, k + 1 - l):
        if l == r == 0:
            continue
        b = sorted(a[:l] + a[n - r:])
        c = min(max(0, k - l - r), len(b) - 1)
        ans = max(ans, sum(b[c:] if b[c] < 0 else filter(lambda x: x > 0, b)))
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(n):
    for r in range(n+1)[::-1]:
        if l <= r and 0 <= (l+n-r) <= k: # l+n-r個拾う
            q = v[:l] + v[r:n]
            q.sort(reverse = True)
            delete = 0
            first = True
            
            for i in range(k-(l+n-r)):
                if q == []:
                    break
                if q[-1] >= 0 and first:
                    delete = q.pop()
                    first = False
                elif q[-1] < 0:
                    delete = q.pop()
                    first = False
                else:
                    q.append(delete)
                    first = True

            ans = max(ans, sum(q))
        else:
            continue

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(n):
    for r in range(n+1)[::-1]:
        if l < r and 0 <= (l+n-r) <= k: # l+n-r個拾う
            q = v[:l] + v[r:n]
            q.sort(reverse = True)
            delete = 0
            for i in range(k-(l+n-r)):
                if q == []:
                    break
                if q[-1] < 0:
                    delete = q.pop()
                else:
                    q.append(delete)
            ans = max(ans, sum(q))
        else:
            continue

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(n):
    for r in range(n+1)[::-1]:
        if l < r and 0 <= k-(l+n-r) <= (l+n-r): # l+n-r個拾って、k-(l+n-r)捨てる
            q = v[:l] + v[r:n]
            q.sort()
            ans = max(ans, sum(q[k-(l+n-r):]))
        else:
            continue

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(n + 1):
    for right in range(n + 1):
        if left == 0 and right == 0:
            continue
        if left + right > n:
            break
        rest = k - left - right
        arr = v[:left] + v[n - right:]
        minus = 0
        neg = [x for x in arr if x < 0]
        if len(neg) > 0:
            neg.sort()
            minus = sum(neg[:rest])
        ans = max(ans, sum(arr) - minus)

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(n + 1):
    for right in range(n + 1):
        if left == 0 and right == 0:
            continue
        if left + right > k:
            break
        arr = v[:left] + v[n - right:]
        rest = k - left - right
        minus = 0
        neg = [x for x in arr if x < 0]
        if len(neg) > 0:
            neg.sort()
            minus = sum(neg[:rest])
        ans = max(ans, sum(arr) - minus)

print(ans)

Submission status: Wrong Answer

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

ans = []
for left in range(n + 1):
    for right in range(n + 1):
        if left == 0 and right == 0:
            continue
        if left + right > k:
            break
        arr = v[:left] + v[n - right:]
        arr.sort(reverse=True)
        rest = k - left - right

        for _ in range(min(len(arr), rest)):
            if arr[-1] < 0:
                arr.pop()
            else:
                break
        ans.append(sum(arr))

print(max(ans))

Submission status: Wrong Answer

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

ans = []
for left in range(n + 1):
    for right in range(n + 1):
        if left == 0 and right == 0:
            continue
        if left + right > k:
            break
        arr = v[:left] + v[n - right:]
        rest = k - left - right
        minus = 0
        neg = [x for x in arr if x < 0]
        if len(neg) > 0:
            neg.sort()
            minus = sum(neg[:rest])
        ans.append(sum(arr) - minus)

print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

ans = 0

for i in range(N+1):
    if K < i:
        continue
    score = sum(V[0:i])
    # print("i", i, "l score", score)
    ans = max(ans, score)
    for j in range(N+1):
        if N < i + j or K < i + j:
            continue
        tmp = score
        tmp += sum(V[N-j:])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "lr score", tmp)

        # 捨てる最大個数
        k = K - i - j
        if i + j < k:
            continue
        negs = []
        for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
        for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
        negs.sort()
        # print(negs)
        tmp -= sum(negs[:k])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "k", k, "lrd score", tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
vv = copy.deepcopy(v)
vv = vv[::-1]

# ABを全探索
ans = 0
for i in range(k + 1):
    for j in range(k - i + 1):
        if i == 0 and j == 0:
            continue
        elif j == 0:
            arr = v[:i]
        elif i == 0:
            arr = vv[:j]
        else:
            arr = v[:i] + vv[:j]
        tmp_ans = sum(arr)
        can_del = k - len(arr)
        cand = [x for x in arr if x < 0]
        if len(cand) == 0:
            ans = max(ans, tmp_ans)
        else:
            cand.sort()
            tmp_ans -= sum(cand[:can_del])
            ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N,K = MI()
V = LI()

ans = 0
for i in range(K+1):
    for j in range(N):  # 左から
        for k in range(N):
            if j + k > min(N,i):
                break
            l = i-j-k
            if l > j+k:
                continue
            A = [V[m] for m in range(j)] + [V[N-1-m] for m in range(k)]
            A.sort(reverse=True)
            ans = max(ans,sum(A[m] for m in range(j+k-l)))
print(ans)

Submission status: Wrong Answer

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

ans = 0
ab_max = min(n,k)
for l in range(ab_max+1):
    for r in range(ab_max+1):
        if l+r > k: continue
        ls = vl[:l]
        rs = vl[n-r:]
        # print(ls,rs)
        vals = ls + rs
        rem = k-(l+r)
        vals.sort()
        curr_sum = sum(vals)
        for i in range(rem):
            if i >= len(vals): break
            if vals[i] < 0: curr_sum -= vals[i]
            else: break
        ans = max(ans,curr_sum)

print(ans)

Submission status: Wrong Answer

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

ans = 0
ab_max = min(n,k)
for l in range(ab_max):
    for r in range(ab_max):
        if l+r > k: continue
        ls = vl[:l]
        rs = vl[n-r:]
        vals = ls + rs
        rem = k-(l+r)
        vals.sort()
        curr_sum = sum(vals)
        for i in range(rem):
            if i >= len(vals): break
            if vals[i] < 0: curr_sum -= vals[i]
            else: break
        ans = max(ans,curr_sum)

print(ans)

Submission status: Wrong Answer

Solution code:
import random

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

if 0:
    V=[random.randint(-10**7,10**7) for i in range(N)]

base=sum(V)
ans=base
for i in range(K+1):
    for j in range(min(K-i,N-i)+1):
        tmp=[V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1-k])
        tmp.sort()
        rest=K-i-j
        minus=0
        base=sum(tmp)
        for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
        test=base-minus
        ans=max(ans,test)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

ans = 0
acc = [0] + list(accumulate(v))
for l in range(min(n, k) + 1):
    for r in range(min(n, k) - l + 1):
        minus = sorted(v[:l] + v[n-r:])
        bag = acc[l] - acc[0] + acc[n] - acc[n - r]
        bag -= sum(vi for vi in minus[:min(n, k) - l - r] if vi < 0)
        ans = max(ans, bag)
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")

def main():
    N, K = LI()
    V = LI()
    V2 = V + V

    ans = 0
    for k in range(1, K + 1):
        for i in range(len(V) - k, len(V) + 1):
            neg = []
            tmp = 0
            for vi in V2[i:i + k]:
                if vi < 0:
                    neg.append(vi)
                tmp += vi
            tmp -= sum(sorted(neg)[:K - k])
            ans = max(ans, tmp)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    for a in range(max(1, k+1)):  # 左から取る個数
        for b in range(max(1, k-a+1)):  # 右から取る個数
            arr = []
            if a > 0: arr += v[:a]
            if b > 0: arr += v[-b:]
            arr.sort()
            for j in range(max(1, a+b)):  # 戻す個数
                if a+b+j > k: break
                score = sum(arr[j:])
                ans = max(ans, score)
                #print(a, b, j, score, arr)
    print(ans)

if __name__ == '__main__':
    abc128_d()

Submission status: Wrong Answer

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

    for a in range(max(1, k+1)):  # 左から取る個数
        for b in range(max(1, k-a+1)):  # 右から取る個数
            arr = []
            if a > 0: arr += v[:a]
            if b > 0: arr += v[-b:]
            arr.sort()
            for j in range(max(1, a+b)):  # 戻す個数
                if a+b+j > k: break
                score = sum(arr[j:])
                ans = max(ans, score)
                print(a, b, j, score, arr)
    print(ans)

if __name__ == '__main__':
    abc128_d()

Submission status: Wrong Answer

Solution code:
def abc128_d():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = -10**9

    for i in range(k+1):  # 操作の回数
        for j in range(max(1, i)):  # 戻す個数
            for a in range(max(1, i-j+1)):  # 左から取る個数
                b = i-j-a  # 右から取る個数
                arr = []
                if a > 0: arr += v[:a]
                if b > 0: arr += v[-b:]
                arr.sort()
                score = sum(arr[j:])
                ans = max(ans, score)
                #print(i, j, a, b, score, arr)
    print(ans)

if __name__ == '__main__':
    abc128_d()

Submission status: Wrong Answer

Solution code:
import sys
import heapq
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(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for k in range(i-1):
                heapq.heappush(h, v[k])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
   
    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import heapq
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(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
              
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import heapq
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(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
              
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(h))
    print(ans)
   
    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def max2(x,y):
    return x if x > y else y

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

res = 0
for l in range(N):
    for r in range(N-l):
        if l+r>K:
            continue
        temp = V[:l] + V[N-r:]
        L = len(temp)
        if K-L>=L:
            if (K-L)%2==1 and temp:
                res = max2(max(temp), res)
            continue
        temp.sort()
        res = max2(sum(temp[K-l-r:]), res)
print(res)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from random import randint
from time import time

class PriorityQueue(object):
    def __init__(self):
        self.queue = []
    def push(self, value):
        heappush(self.queue, value)
    def pop(self):
        return heappop(self.queue)
    def __len__(self):
        return len(self.queue)
    def __contains__(self, item):
        return item in self.queue

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

ans = 0

#取得する操作を何回行うか？
for i in range(0,m+1):

	#操作残り回数
	lef = m-i

	hp = PriorityQueue()
	#左側からどれだけ取るか？
	for j in range(0,i+1):
		if j>n :
			break

		#左側からj個取る
		for k in range(0,j):
			hp.push(v[k])

		#右側から i-j個取る
		for k in range(0,i-j):
			if n-1-k < i:
				break
			hp.push(v[n-1-k])

		#minusは戻す
		for k in range(0,lef):
			if len(hp) == 0:
				break
			top = hp.pop()
			if top>=0 :
				hp.push(top)
				break

		ss = 0
		while len(hp)>0:
			ss += hp.pop()

		ans = max(ans,ss)

print(ans)