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:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        if i<N-j:
            dp[i][j] = [V[:i]+V[N-j:]+[0]]
        else:
            dp[i][j] = [V+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K)):
    for j in range(min(N,K)):
        if i+j!=0:
            t = max(K - (i + j),0)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC))

Submission status: Runtime Error

Solution code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        if i<N-j:
            dp[i][j] = [V[:i]+V[N-j:]+[0]]
        else:
            dp[i][j] = [V+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K)):
    for j in range(min(N,K)):
        if i+j!=0:
            t = K - (i + j)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC))

Submission status: Runtime Error

Solution code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        dp[i][j] = [V[:i]+V[N-j:]+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K)):
    for j in range(min(N,K)):
        if i+j!=0:
            t = K - (i + j)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC))

Submission status: Runtime Error

Solution code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        dp[i][j] = [V[:i]+V[N-j:]+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K)):
    for j in range(min(N,K)):
        if i+j!=0:
            t = K - (i + j)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC,0))

Submission status: Runtime Error

Solution code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(K+1):
    for j in range(K-i+1):
        dp[i][j] = [V[:i]+V[N-j:]+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(K):
    for j in range(K):
        if i+j!=0:
            t = K - (i + j)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
print(max(ansC))

Submission status: Runtime Error

Solution code:
def main():
    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+1-l):
            if l + r > K:
                continue
            D = K - l - r
            S = []
            now = 0
            for i in range(l):
                now += V[i]
                S.append(V[i])
            for j in range(N-1, N-1-r, -1):
                now += V[j]
                S.append(V[j])

            sort_S = sorted(S)
            for d in range(min(D,len(S))):
                if sort_S[d] < 0:
                    now += abs(sort_S[d])

            ans = max(ans, now)

    print(ans)
            

main()

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):
    for r in range(0 , K - l):
        if l + r > K:
            continue
        d = K - (r + l)
        now = 0
        hold = []
        for i in range(l):
            hold.append(V[i])
        for i in range(1 , r + 1):
            hold.append(V[-i])
        hold.sort()
        now = sum(hold)
        for i in range(d):
            if i >= len(hold):
                break
            if hold[i] > 0:
                break
            else:
                now -= hold[i]
        ans = max(ans , now)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
xs=[0]+list(map(int,input().split()))
from itertools import accumulate
As=list(accumulate(xs))
r = 0
for i in range(k+1):
    for j in range(i, k+1):
        x = i, j-i, k-j
        m1 = As[i] + As[n] - As[n-j+i]
        ss = sorted(z for z in (xs[:i]+xs[n-j+i+1:]) if z < 0)
        m = m1 - sum(ss[:min(k-j,len(ss))])
#        if m > r:
#            print(x, m, m1)
        r = max(r, m)
print(r)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
xs=[0]+list(map(int,input().split()))
from itertools import accumulate
As=list(accumulate(xs))
r = 0
for i in range(k+1):
    for j in range(i, k+1):
        x = i, j-i, k-j
        m = As[i] + As[n] - As[n-j+i]
        #m = m1 - sum(sorted(z for z in (xs[:i]+xs[n-j+i+1:]) if z < 0)[:k-j])
#        if m > r:
#            print(x, m, m1)
        r = max(r, m)
print(r)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
xs=[0]+list(map(int,input().split()))
from itertools import accumulate
As=list(accumulate(xs))
r = 0
for i in range(k+1):
    for j in range(i, k+1):
        x = i, j-i, k-j
        m1 = As[i] + As[n] - As[n-j+i]
        m = m1 - sum(sorted(z for z in (xs[:i]+xs[n-j+i+1:]) if z < 0)[:k-j])
#        if m > r:
#            print(x, m, m1)
        r = max(r, m)
print(r)

Submission status: Runtime Error

Solution code:
from itertools import combinations
from bisect import bisect_left
N, K = map(int,input().split())
A = list(map(int,input().split()))

ans=0

for i,j in combinations(range(N+2),2):
  l = sorted(A[0:i]+A[j-1:N])
  if len(l)<=K:
    m = bisect_left(l,0)
  	ans = max(ans,sum(l[min(K-len(l),m):]))
print(ans)

Submission status: Runtime Error

Solution code:
#D
from heapq import heappush,heappop
from collections import deque
n,k=map(int,input().split())
v=deque(list(map(int,input().split())))
w=[[] for i in range(n)]
answer=-100000000
for i in range(k+1):
    for j in range(k-i+1):
        h=[]
        for a in range(i):
            heappush(h,v[a])
        for a in range(j):
            heappush(h,v[n-1-a])
        for a in range(k-i-j):
            if h and h[0]<0:
                heappop(h)
            else:
                break
        answer=max(answer,sum(h))
print(answer)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, p = LI()
    print((a*3+p)//2)
    return

#B
def B():
    n = II()
    sp = []
    for i in range(n):
        sp.append(input().split())
    another = [(s[0], -1 * int(s[1]),num) for num,s in enumerate(sp)]
    another.sort()
    for s,p,q in another:
        print(q+1)

    return

#C
def C():
    n, m = LI()
    ks = LIR_(m)
    p = LI()
    patern = itertools.product(range(2), repeat=n)
    ans = 0
    for on in patern:
        ki = [0 for i in range(m)]
        for num, oni in enumerate(on):
            if oni:
                for i,ksi in enumerate(ks):
                    ksi = ksi[1::]
                    if num in ksi:
                        ki[i] ^= 1
        for num, pi in enumerate(p):
            if pi != ki[num]:
                break
        else:
            ans += 1
    print(ans)

            
    return

#D
def D():
    N, K = LI()#random.randint(1,50),random.randint(1,100)
    v = LI()#[random.randint(-10 ** 2, 10 ** 2) for i in range(N)]
    ans = 0
    for a in range(min(N, K) + 1):
        for b in range(min(N, K) + 1):
            if a + b > min(K,N):
                break
            deletes = K - a - b
            left = v[:a]
            right = v[:-b-1:-1]
            x = left + right
            x.sort()
            tmp = sum(x)
            for delete in range(min(deletes, len(x))):
                if x[delete] >= 0:
                    break
                tmp -= x[delete]
                ans = max(ans,tmp)
    
    print(ans)
                

    return

#E
def E():
    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    for _ in range(10):
        D()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

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


def put_queue(dd, l, v):
    if l == k:
        return 0

    a = dd[0] + put_queue(dd[1:], l + 1, v + [dd[0]])
    b = dd[-1] + put_queue(dd[:-1], l + 1, v + [dd[-1]])
    if len(v) > 0:
        vs = sorted(v)
        minv = vs[0]
        c = -minv + put_queue([minv] + dd, l + 1, vs[1:])
        d = -minv + put_queue(dd + [minv], l + 1, vs[1:])
        return max(a, b, c, d)
    return max(a, b)


print(max(0, put_queue(d, 0, [])))

Submission status: Runtime Error

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


def put_queue(dd, l, v):
    if l == k:
        return 0

    a = dd[0] + put_queue(dd[1:], l + 1, v + [dd[0]])
    b = dd[-1] + put_queue(dd[:-1], l + 1, v + [dd[-1]])
    if len(v) > 0:
        vs = sorted(v)
        minv = vs[0]
        c = -minv + put_queue([minv] + dd, l + 1, vs[1:])
        d = -minv + put_queue(dd + [minv], l + 1, vs[1:])
        return max(a, b, c, d)
    return max(a, b)


print(max(0, put_queue(d, 0, [])))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
r = min(n, k)
ans = -float('inf')
for i in range(r+1):
    a = v[:i]
    for j in range(r+1-i):
        candi=a[:]
        if j !=0 :candi += v[-j:]
        candi.sort()
        for t in range(k-i-j):
            if not 0<=t<len(candi):break
            if candi[t] < 0:
                candi[t] = 03023
            else:
                break
        candi = sum(candi)
        ans = max(candi, ans)
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):
		hand = []
        if l + r > n:
          break
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(k - l - r, l + r)):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

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):
		hand = []
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(k-l-r,l + r)):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

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):
		hand = []
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(hand.sort())):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

print(ans)

Submission status: Runtime Error

Solution code:
#17:09
n,k = map(int,input().split())
v = list(map(int,input().split()))
'''
左からa個
右からb個
k-a-b個以内捨てて良し
'''
can = []
x = []
for a in range(k):
  if a != 0:
    x.append(v[a-1])
  y = []
  for b in range(k):
    if b != 0:
      y.append(v[-b])
    if a + b <= min(n,k):
      z = x + y
      z.sort()
      r = k - a - b
      tmp = 0
      if r < a+b:
        if z[r] < 0:
          for i in range(r,a+b):
            tmp += z[i]
        else:
          for i in range(a+b):
            if z[i] > 0:
              tmp += z[i]
      else:
        for i in range(r,a+b):
          if z[i] > 0:
            tmp += z[i]
      can.append(tmp)
print(max(can))

Submission status: Runtime Error

Solution code:
#coding utf-8
N,K=map(int,input().split())
V =list(map(int,input().split())) 

Vl =V
Vr =V[::-1]

dpl=[]
dpr=[]
ansl=[0]
ansr=[0]

for i in range(K):
  if not dpl:
    dpl.append(Vl.pop(0))
  elif min(dpl)<0 and min(dpl)<Vl[i]:
    del dpl[dpl.index(min(dpl))]
  else:
    dpl.append(Vl.pop(0))
  ansl.append(max(sum(dpl),ansl[i]))

for i in range(K):
  if not dpr:
    dpr.append(Vr.pop(0))
  elif min(dpr)<0 and min(dpr)<Vr[i]:
    del dpr[dpr.index(min(dpr))]
  else:
    dpr.append(Vr.pop(0))
  ansr.append(max(sum(dpr),ansr[i]))
    
ans=0    
for i in range(1,K):
  tmp=ansr[i]+ansl[K-i]
  if ans<tmp:
    ans=tmp
  
print(ans)

Submission status: Runtime Error

Solution code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    v = list(map(int, input().rstrip('\n').split()))
    ms = 0
    t = []
    heapq.heapify(t)
    for i in range(1, k + 1):
        for j in range(i + 1):
            t.clear()
            if j != 0:
                for w in v[:j]:
                    heapq.heappush(t, w)
            if -i+j != 0:
                for w in v[-i+j:]:
                    heapq.heappush(t, w)
            for _ in range(k-i):
                if len(t) != 0:
                    p = heapq.heappop(t)
                    if p >= 0:
                        heapq.heappush(t, p)
                        break
            ms = max(ms, sum(t))
    print(ms)


if __name__ == '__main__':
    slove()

Submission status: Runtime Error

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

m=min(n,k)
ans=0

for i in range(m):
    for j in range(i):
        q=[]
        tmp=0
        for k in range(j):
            q.append(v.pop(0))
        for k in range(i-j):
            q.append(v.pop(-1))
        q.sort()
        for l in range(len(q)):
            if q[i]>0:
                tmp+=q[i]
        ans=max(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 0
for l in range(K+1):
    for r in range(K+1-l):
        if l + r > N:
            break
        get = lst[:l]
        get.extend(lst[N-r:])
        cnt = sum(get)
        get.sort()
        for i in range(min(K-l-r, l+r)):
            if get[i] >= 0:
                break
            tmp -= get[i]
        ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

ans = 0
for t in range(k//2 + 1):
    hand = deque(V[:k-t])
    rest = deque(V[k-t:])
    cur_sum = sum(hand)

    for j in range(k-t+1):
        put_back = 0
        rev_hand = sorted(hand)

        for i in range(t):
            jewel = rev_hand[i]
            if jewel < 0:
                put_back -= jewel
            else:
                break

        candidate = cur_sum + put_back
        ans = max(candidate, ans)

        to_rest = hand.pop()
        to_hand = rest.pop()
        hand.appendleft(to_hand)
        rest.appendleft(to_rest)
        cur_sum += to_hand - to_rest

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for n in range(k+1):
        if n >= N:
            break
        A = V[:n]
        A.extend(V[N-k+n:])
        A.sort()
        c = sum(A)
        for i in range(min(K-k, k)):
            if A[i] <= 0:
                c -= A[i]
        #print(k, n, c)
        ans = max(ans, c)
print(ans)

Submission status: Runtime Error

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

ans = 0
nums = min(N, K)
for i in range(nums+1): # 左端から取り出す回数
    for j in range(nums+1-i): # 右端から取り出す回数
        for k in range(nums+1-i-j): # Dに戻す個数
            L = V[:i]
            R = V[N-j:]
            #print(L, R, i, j, k)
            if L == []: L = [0]
            if R == []: R = [0]
            LR = L + R
            LR.sort()
            cnt = 0
            while cnt < k:
                if LR[cnt] < 0:
                    cnt += 1
                else:
                    break
            ans = max(ans, sum(LR[cnt:]))

print(ans)

Submission status: Runtime Error

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

ans = 0
nums = min(N, K)
for i in range(nums): # 左端から取り出す回数
    for j in range(nums-i): # 右端から取り出す回数
        for k in range(nums-i-j): # Dに戻す個数
            L = V[:i]
            R = V[N-j:]
            if L == []: L = [0]
            if R == []: R = [0]
            LR = L + R
            LR.sort()
            cnt = 0
            while cnt <= k:
                if LR[cnt] < 0:
                    cnt += 1
                else:
                    break
            ans = max(ans, sum(LR[cnt:]))

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
res = []
for i in range(k):
  throwaways = i
  take = k - throwaways
  # throwaways cannot be larger than take
  if throwaways > take:
    break
  for j in range(take+1):
    right = lst[:j]
    if len(right) < take:
      l = take - len(right)
      left = lst[l*-1:]
    else:
      left = []
    m_lst = sorted(right + left)
    for t in range(throwaways):
      del m_lst[0]
    res.append(m_lst)
max_num = max([sum(e) for e in res])
if max_num <= 0:
  print(0)
else:
  print(max_num)

Submission status: Runtime Error

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

def heap_push(my_lst, a):
    heapq.heappush(my_lst, a)
    return my_lst


def dfs(k, lst, my_lst):
    if k == 0:
        return max(sum(my_lst), 0)
    
    if len(my_lst) == 0:
        a = dfs(k-1, lst[1:], heap_push(my_lst[:], lst[0]))
        b = dfs(k-1, lst[:-1], heap_push(my_lst[:], lst[-1]))
        return max(a, b)
                
    if len(lst) == 0:
        return dfs(k-1, lst + heapq.nsmallest(1,my_lst), my_lst[1:])
                
    else:
        a = dfs(k-1, lst[1:], heap_push(my_lst[:], lst[0]))
        b = dfs(k-1, lst[:-1], heap_push(my_lst[:], lst[-1]))
        
        c = dfs(k-1, lst + heapq.nsmallest(1,my_lst), my_lst[1:])
        d = dfs(k-1, heapq.nsmallest(1,my_lst) + lst, my_lst[1:])
        return max(a,b,c,d)
    
my_lst = []
heapq.heapify(my_lst)
print(dfs(k, lst, []))

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)   
maxnum=0
times=min(n,k)
for limit in range(times):
    for a in range(limit+1):
        get=[]
        Dcopy=D[:]
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(times-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
print(maxnum)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)
Dcopy=D.copy()

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)
    
maxnum=0
times=min(n,k)
for limit in range(times):
    for a in range(limit+1):
        get=[]
        Dcopy=D.copy()

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)
    
maxnum=0
times=min(n,k)
for limit in range(times):
    for a in range(limit+1):
        get=[]
        Dcopy=D.copy()
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(times-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
print(maxnum)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque(int(i) for i in input().split()

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque(int(i) for i in input().split())
maxnum=0
times=min(n,k)
for limit in range(times):
    for a in range(limit+1):
        get=[]
        Dcopy=D.copy()
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(times-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
print(maxnum)

Submission status: Runtime Error

Solution code:
import heapq

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

x1, k1, l1, lim1 = 0, K, [], N
if lim1 > K:
    lim1 = K
lim1 += 1
for i in range(lim1):
    ans.append(sum(l1[min(k1, x1):]))
    x2, k2, l2, lim2 = x1, k1, l1[:], N - i
    if lim2 > k2:
        lim2 = k2 + 1
    for vr in V[:-lim2:-1]:
        k2 -= 1
        heapq.heappush(l2, vr)
        if vr <= 0:
            x2 += 1
        ans.append(sum(l2[min(k2, x2):]))
    vl = V[i]
    k1 -= 1
    heapq.heappush(l1, vl)
    if vl <= 0:
        x1 += 1
print(max(ans))

Submission status: Runtime Error

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

V = deque(V)
ans = []
for i in range(min(N,K)):
  for j in range(min(N,K)-i):
    Vc = V.copy()
    cal = deque([0])
    for k in range(i):
      cal.append(Vc.popleft())
    for l in range(j):
      cal.append(Vc.pop())
    cal = deque(sorted(cal))
    for m in range(K-i-j):
      if cal[0] < 0:
        cal.popleft()
      else:
        break
    ans.append(sum(cal))

print(max(ans))

Submission status: Runtime Error

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

V = deque(V)

ans = []
for i in range(min(N,K)):
  for j in range(min(N,K)-i):
    Vc = V.copy()
    cal = deque([])
    for k in range(i):
      cal.append(Vc.popleft())
    for l in range(j):
      cal.append(Vc.pop())
    cal = sorted(cal)
    for m in range(K-i-j):
      if cal[0] < 0:
        cal.popleft()
      else:
        break
    ans.append(sum(cal))

print(max(ans))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
*V, = map(int, input().split())
M = [v if v < 0 else 0 for v in V]
S = []
for i in range(min(k, n)):
    for j in range(i):
        s = sum(V[:i-j])+sum(V[n-j:])
        M_sub = sorted(M[:i-j]+M[n-j:])
        s -= sum(M_sub[:k-i])
        S += [s]
print(max(S))

Submission status: Runtime Error

Solution code:


def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,k = getints()
vs = getints()

res = 0
for take in range(0, k + 1):
    sute = k - take
    for from_left in range(0, take+1):
        from_right = k - from_left
        value = []
        for i in range(0, from_left):
            value.append(vs[i])
        for i in range(-1, -from_right, -1):
            value.append(vs[i])
        value = list(sorted(value, reverse=True))
        for i in range(sute):
            if value and value[-1] < 0:
                value.pop()
        tmp = 0 if not value else sum(value)
        res = max(res, tmp)
print(res)

Submission status: Runtime Error

Solution code:
ans = 0

for i in range(k+1):
    l = min(i,n)
    for j in range(l+1):
        left_gain = jewel_list[:j]
        if l-j != 0:
            right_gain = jewel_list[-l+j:]
        else:
            right_gain = []
        
        gabage = sorted(left_gain+right_gain)[:k-i]

        total = sum(left_gain+right_gain)
        for p in gabage:
            if p < 0:
                total -= p
            else:
                break
        ans = max(ans,total)

print(ans)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
que = list(map(int, input().split()))
ans = 0
pop_push = [(i, k-i) for i in range(k, math.ceil(k/2)-1, -1)]
for pop, push in pop_push:
    left_right = [(j, pop-j) for j in range(pop+1)]
    for left, right in left_right:
        ans = max(ans, sum(sorted(que[:left] + list(reversed(que))[:right])[push:]))
print(ans)

Submission status: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-r, N):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = K - l - r
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-1, max(N-1-r,-1), -1):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = max(K - l - r, 0)
        ret = max(ret, calc(l, r, d))
print(ret)