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: Runtime Error

Solution code:
n,k= map(int,input().split())
v= list(map(int,input().split()))
lrwa = [0]*n
rrwa = [0]*n
lminusf = []
rminusf = []
rv=v[::-1]
for i in range(n):
    if i==0:
        lrwa[i] = v[i]
        rrwa[i] = rv[i]
    else:
        lrwa[i] += lrwa[i-1]+v[i]
        rrwa[i] += rrwa[i-1]+rv[i]
    if v[i]<0:lminusf.append(v[i])
    if rv[i]<0:rminusf.append(rv[i])
left2 = [-999999999] * (k+1)
right2 = [-999999999] * (k+1)
lminc = rminc = 0
lgetm2=[]
rgetm2=[]
for i in range(k+1):
    if i!=0:
        if v[i-1]<0:
            lgetm2.append(v[i-1])
            lminc+=1
            lgetm2.sort()
        if rv[i-1]<0:
            rgetm2.append(rv[i-1])
            rminc+=1
            rgetm2.sort()
    left2[i]=max(left2[i],([0]+lrwa)[i])
    tempte = min(i,k-i)
    lreturnable = min(tempte,lminc)
    for j in range(lreturnable):
        left2[i+j+1]=max(left2[i+j+1],([0]+lrwa)[i]-sum(lgetm2[:j+1]))
    right2[i]=max(right2[i],([0]+rrwa)[i])
    rreturnable = min(tempte,rminc)
    for j in range(rreturnable):
        right2[i+j+1]=max(right2[i+j+1],([0]+rrwa)[i]-sum(rgetm2[:j+1]))
for i in range(1,k):
    if left2[i]<left2[i-1]:left2[i]=left2[i-1]
    if right2[i]<right2[i-1]:right2[i]=right2[i-1]
print(max([left2[i]+right2[k-i] for i in range(k+1)]))

Submission status: Runtime Error

Solution code:
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import unittest
from io import StringIO
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    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+1):
            if l+r > K:
                continue
            now = []
            val = 0
            for i in range(l):
                now.append(V[i])
                val += V[i]
            for j in range(r):
                now.append(V[-j-1])
                val += V[-j-1]

            d = K-l-r
            now.sort()
            for i in range(d):
                if i >= len(now):
                    break
                if now[i] > 0:
                    break
                val -= now[i]
            ans=max(ans,val)

    print(ans)

resolve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    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+1):
            if l+r>K: continue
            now=[]
            for i in range(l):
                now.append(V[i])
            for j in range(r):
                now.append(V[-j-1])
            

            now.sort()
            m=K-l-r
            if m==0:
                ans=max(ans,sum(now))
            if range(min(l+r,m)):
                for i in range(min(l+r,m)):
                    if now[i]>=0:
                        temp=i
                        break
                    else: temp=i+1
                ans=max(ans,sum(now[temp:]))

    print(ans)



    
resolve()

Submission status: Runtime Error

Solution code:
from collections import deque
from copy import deepcopy
import heapq

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

minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

for left in range(N+1):
    for right in range(N-left+1):
        v=deepcopy(V)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right]

leftacum=[0]*(N+1)
rightacum=[0]*(N+1)

vl=deepcopy(V)
vr=deepcopy(V)

for i in range(N):
    leftacum[i+1]=leftacum[i]+vl.popleft()
    rightacum[i+1]=rightacum[i]+vr.pop()

ans=-10**10

for left in range(N+1):
    for right in range(N-left+1):
        k=K-left-right
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right])

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
from heapq import *
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 + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            heappop(get)
        SUM = sum(get)
        if ans < SUM:ans = SUM
print(ans)

Submission status: Runtime Error

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

def f(t):
    res=0
    for i in range(t+1):
        a=get(min(t,n),min(i,n))
        tmp=sum(a)
        res=max(res,tmp)
        for j in range(min(k-t,t)):
            tmp-=a[j]
            res=max(res,tmp)
    return res

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

Submission status: Runtime Error

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

def f(t):
    res=0
    for i in range(t+1):
        if 2*t-k>=0:
            if i>n:
                a=get(t,n)
            else:
                a=get(t,i)
            tmp=sum(a)
            for j in range(min(k-t,t)):
                tmp-=a[j]
                res=max(res,tmp)
    return res

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
min_nk=min(n,k)
for i in range(min_nk+1):#左
    for j in range(n-min_nk+i,n+1):#右
        count=a[0:i]+a[j:n]
        count.sort()
       # print(count)
       ans=max(sum(count))
        for x in range(1,min_nk-len(count)+1):#消せれば消す
           # print(sum(count[x:]))
            ans=max(ans,sum(count[x:]))
print(ans)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = m-l-r
        data = have[0]+have[1]
        data.sort()
        for i in range(a):
            if data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
#    v_cum = [x for x in v]
#    for i in range(1, n):
#        v_cum[i] += v_cum[i-1]
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
#            print(L_i, R_i, tmp)
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
#    v_cum = [x for x in v]
#    for i in range(1, n):
#        v_cum[i] += v_cum[i-1]
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
#            print(L_i, R_i, tmp)
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# AtCoder
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+1):
        if l+r > N:
            break
        d = K-l-r
        now = 0
        s = []
        for i in range(l+1):
            s.append(V[i])
            now += V[i]

        for i in range(N-r, N):
            s.append(V[i])
            now += V[i]

        s = sorted(s)
        for i in range(d-1):
            if i >= len(s):
                break
            if s[i] > 0:
                break
            now -= s[i]
        ans = max(ans, now)
print(ans)

Submission status: Runtime Error

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

for pick in range(min(K+1, N+1)):
    for right in range(pick+1):
        left = pick - right
        pick_list = V[:right] + V[N-left:]

        pick_list = sorted(pick_list)
        tmp_ans = sum(pick_list)
        rem = min(K - pick, len(pick_list))
        for i in range(rem):
            if pick_list[i] < 0:
                tmp_ans -= pick_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Runtime Error

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

for pick in range(min(K+1, N+1)):
    for right in range(pick+1):
        left = pick - right
        pick_list = V[:right] + V[N-left:]

        pick_list.sort()
        tmp_ans = sum(pick_list)
        rem = min(K - pick, len(pick_list))
        for i in range(rem):
            if pick_list[i] < 0:
                tmp_ans -= pick_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.readline
from heapq import heapify, heappush, heappop

def main():
    N, K = map(int, readline().rstrip().split())
    V = list(map(int, readline().rstrip().split()))
    V_rev = V[::-1]
    res = 0
    for x in range(K+1):
        for y in range(K-x+1):
            z = K - x - y
            if z > x + y:
                continue
            temp = V[:x] + V_rev[:y]
            heapify(temp)
            for _ in range(z):
                heappop(temp)

            res = max(res, sum(temp))
    
    print(res)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from heapq import heapify,heappush,heappop
import math

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

ans = 0
sum_d = 0
d = []
heapify(d)
# i = 操作回数
# j = 取る回数 (i-j = 捨てる回数)
# k = 左から取る回数 (j-k = 右から取る回数)
for i in range(1,K+1):
	for j in range(math.ceil(i/2),i+1):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d)
print(ans)

Submission status: Runtime Error

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

#a:左からとる b:右からとる K-a-b:捨てる
score=0
torikata=[]
for d in range(101):
    for a in range(101):
        for b in range(101):
            if d+a+b<=K:
                torikata.append((a,b,d))

for i in range(len(torikata)):
    A,B,D=torikata[i][0],torikata[i][1],torikata[i][2]
    H=[]
    for i in range(A):
        H.append(V[i])
    for i in range(B):
        H.append(V[-(i+1)])
    heapq.heapify(H)
    for i in range(min(D,A+B)):
        heapq.heappop(H)
    score=max(score,sum(H))

print(score)

Submission status: Runtime Error

Solution code:
from collections import deque
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(k+1):
    if k-l > 0:
        for r in range(k-l+1):
            hand = []
            for i in range(1, l+1):
                hand.append(v[i-1])
            for i in range(1, r+1):
                hand.append(v[n-i])
            hand.sort()
            if k-l-r > 0:
                for back in range(k-l-r):
                    if len(hand) != 0:
                        if min(hand) < 0:
                            x = hand.index(min(hand))
                            hand[x] = 0
                ans = max(ans, sum(hand))
print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    ans = 0
    for a in range(min(N, K) + 1):
        for b in range(K - a):
            if a + b > N:
                continue
            tmp = []
            for i in range(a):
                tmp.append(V[i])
            for i in range(b):
                tmp.append(V[N - i - 1])
            rest = K - a - b
            tmp.sort()
            s = sum(tmp)
            for r in range(rest):
                if tmp[r] < 0:
                    s -= r
                else:
                    break
            if s > ans:
                ans = s

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# import itertools
# import pprint
import copy
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r>ope:
      break

    x   = ope-l-r
    now = []
    deq = copy.copy(vlist) 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Runtime Error

Solution code:
import heapq 
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) #KがNより大きい場合を防ぐ
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort()
        h = K - len(new_A)
        while h >0:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A))
print(M)

Submission status: Runtime Error

Solution code:
# import itertools
# import pprint
import copy
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r>ope:
      break

    x   = ope-l-r
    now = []
    deq = copy.deepcopy(vlist) 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Runtime Error

Solution code:
# import itertools
# import pprint
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r>ope:
      break

    x   = ope-l-r
    now = []
    deq = vlist.copy() 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r > ope:
      break

    x   = ope-l-r
    now = []
    deq = vlist.copy() 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Runtime Error

Solution code:
# import itertools
# import pprint
from collections import deque

gem,ope = map(int,input().split())
vnowt = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r > ope:
      break

    x   = ope-l-r
    now = []
    deq = vnowt.copy() 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Runtime Error

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+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        if k-L-R < len(neg):
            val -= sum(neg[:k-L-R])
        else:
            val -= sum(neg)
        if ans < val:
            ans = val
print(ans)

Submission status: Runtime Error

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+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:min(k-L-R, len(neg))])
        if ans < val:
            ans = val
print(ans)

Submission status: Runtime Error

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+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:k-L-R])
        if ans < val:
            ans = val
print(ans)

Submission status: Runtime Error

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

lrui = [0] + v + [0]
for i in range(n): lrui[i+1] += lrui[i]
rrui = [0] + v + [0]
for i in range(n, 0, -1): rrui[i] += rrui[i+1]

lminus = [[] for i in range(n+2)]
for i in range(n+1):
    lminus[i+1] = lminus[i].copy()
    if i<n and v[i]<=0: lminus[i+1].append(v[i])

rminus = [[] for i in range(n+2)]
for i in range(n, -1, -1):
    rminus[i] = rminus[i+1].copy()
    if i>=0 and v[i-1]<=0: rminus[i].append(v[i-1])

ans = 0
for i in range(min(n, k)+1):
    for j in range(n+1, n-k+i, -1):
        tmp = lrui[i] + rrui[j]
        st = sorted(lminus[i] or rminus[j])
        for l in range(min(k-(i+n-j+1), len(st))):
            tmp -= st[l]
            ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
for i in range(n):
    for j in range(n-i+1):
        num = lis[:i]
        if j > 0:
            num += lis[-j:]
        if len(num) <= k:
            cnt = min(len(num),k-len(num))
            num.sort()
            for h in range(cnt):
                num[cnt] = max(0,num[cnt])
            ans = max(ans,sum(num))
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque
from copy import deepcopy

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

dq = deque()
dq.extend(V)

ans = - 10**9
for i in range(k+1):
  for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    for l in range(i):      
      heapq.heappush(a,que.popleft())
    for r in range(j):
      heapq.heappush(a,que.pop())    
    
    for ki in range((k+1-i-j)//2+1):            
      ans = max(ans,sum(a))        
      if len(a) > 0:
        heapq.heappop(a)
        
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque

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

dq = deque()
dq.extend(V)

ans = - 10**9
for i in range(k+1):
  for j in range(k+1-i):           
    que = dq.copy()        
    a = []    
    for l in range(i):      
      heapq.heappush(a,que.popleft())
    for r in range(j):
      heapq.heappush(a,que.pop())    
    
    for ki in range((k+1-i-j)//2+1):            
      ans = max(ans,sum(a))        
      if len(a) > 0:
        heapq.heappop(a)
        
print(ans)

Submission status: Runtime Error

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

D = []
ans = 0

for i in range(K+1):
    for j in range(K-i+1):
        if i + j > N:
            break
        D.append(V[:i]+V[N-j:])

for d in D:
    for x in range(K-len(d)):
        if d==[]:
            break
        elif min(d)<0:
            d.pop(d.index(min(d)))

        elif min(d)>0:
            break
        
        if d!=[]:
            s = sum(d)
            if s>0 and s>ans:
                ans = s

print(s)

Submission status: Runtime Error

Solution code:
import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0
# k+1個取って，ret-1個戻す
for k in reversed(range(K)):
    for ret in range(K-k): #戻す個数はret個
        if (ret < k+1) and ((k+1+ret)<=K):
            for i in range(k+1+1): # 左右の分け方
                tmp = []
                V_tmp = copy.copy(V)
                for sp in range(i): # 左端iつ取る
                    tmp.append(V_tmp.pop(0))
                for sp in range(k+1-i): # 右端k+1-iつ取る
                    tmp.append(V_tmp.pop(-1))
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                value = max(value,sum(tmp))

print(value)

Submission status: Runtime Error

Solution code:
import heapq

n,k=map(int,input().split())
v=list(map(int,input().split()))
s=-10**7-1
for l in range(k//2+1):
    for i in range(k+1-l):
        d=v[:i]
        d.extend(v[(n-k+l+i):])
        heap=heapq.heapify(d)
        res=sum(d)
        for j in range(l):
            t=heapq.heappop(d)
            if t<0:
                res-=t
        if s<res:
            s=res
if s<0:
    print(0)
else:
    print(s)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
if N<=K:
  W=sorted(V)
  h=sum(V)
  for i in range(K-N):
    if W[i]<0:
      h-=W[i]
    else:
      break
else:
  ans=[]
  for i in range(1,K):
    for j in range(i+1):
      l=V[0:i-j]+V[N-j:N]
      l.sort()
      H=sum(l)
      for k in range(min(i,K-i)):
        if l[k]<0:
          H-=l[k]
        else:
          break
      ans.append(H)
  h=max(ans)
print(h)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
if N<=K:
  W=sorted(V)
  h=sum(V)
  for i in range(K-N):
    a=W[i]
    if a<0:
      h-=a
    elif a>0:
      break
else:
  ans=[]
  for i in range(1,K):
    for j in range(i+1):
      l=V[0:i-j]+V[N-j:N]
      l.sort()
      H=sum(l)
      for k in range(min(i,K-i)):
        if l[k]<0:
          H-=l[k]
        elif l[k]>0:
          break
      ans.append(H)
  h=max(ans)
print(h)

Submission status: Runtime Error

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

# ----- heapq ここから-----
import heapq
class Reverse:
    def __init__(self, val):
        self.val = val
        
    def __lt__(self, other):
        return self.val > other.val
        
    def __repr__(self):
        return repr(self.val)
    
class Heapq:
    def __init__(self, arr, desc = False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)
 
    def pop(self):
        if self.desc: return heapq.heappop(self.hq).val
        else: return heapq.heappop(self.hq)
 
    def push(self, a):
        if self.desc: heapq.heappush(self.hq, Reverse(a))
        else: heapq.heappush(self.hq, a)
 
    def top(self):
        if self.desc: self.hq[0].val
        else: return self.hq[0]
# ----- heapq ここまで-----

n,k = myinput()
v = list(myinput())
vr = v[::-1]

ans = -float("inf")
for a in range(k+1):
    for b in range(k-a+1):
        have = []
        have.extend( v[:a] )
        have.extend( vr[:b] )
        p = [ i for i in have if i>=0 ]
        m = [ i for i in have if i<0 ]
        m.sort()
        r = k - a - b
        if len(m)!=0
            m = m[r:]
        else:
            m = []
        ans_tmp = sum(p)+sum(m)
        ans = max(ans,ans_tmp)

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    import heapq
    from copy import deepcopy
    from collections import deque
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    _N, K = [int(x) for x in input().strip().split()]
    V = deque([int(x) for x in input().strip().split()])
    max_ans = sum([v for v in V if v > 0])

    def dfs(ans, q, V, k):
        # print(k, q, V)
        ans = max(ans, sum(q))
        if ans == max_ans:
            print(ans)
            eixt()
        if k == K:
            return ans
        if q:
            t = heapq.heappop(q)
            V.appendleft(t)
            ans = max(ans, dfs(ans, q[:], V, k+1))
            V.popleft()
            V.append(t)
            ans = max(ans, dfs(ans, q, V, k+1))
            V.pop()
            heapq.heappush(q, t)
        if V:
            q_l = q[:]
            q_r = q[:]
            t = V.popleft()
            heapq.heappush(q_l, t)
            ans = max(ans, dfs(ans, q_l[:], V, k+1))
            V.appendleft(t)
            t = V.pop()
            heapq.heappush(q_r, t)
            ans = max(ans, dfs(ans, q_r[:], V, k+1))
            V.append(t)
            
        return ans
    print(dfs(-float('inf'), [], V, 0))

if __name__ == '__main__':
    main()