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.

Solution (Python):

import sys
import bisect

def solve(N: int, K: int, V: "List[int]"):
    a = 0
    answer = []
    reverse_V = V[::-1]
    for i in range(1,min(N,K)+1): 
        for left in range(i+1): 
            right = i-left
            if left>0 and right>0:
                answer =  V[:left]+reverse_V[:right]
            elif left>0:
                answer = V[:left]
            elif right>0:
                answer = V[:right]
            else:
                answer = []
            answer.sort()
            zero_index = bisect.bisect_left(answer,0)
            
            answer = answer[min(zero_index,K-i):]
            a = max(a,sum(answer))
    
    print(a)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    V = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, V)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import bisect

def solve(N: int, K: int, V: "List[int]"):
    a = 0
    answer = []
    reverse_V = V[::-1]
    for i in range(1,min(N,K)+1): 
        for left in range(i+1): 
            right = i-left
            if left>0 and right>0:
                answer =  V[:left]+reverse_V[:right]
            elif left>0:
                answer = V[:left]
            elif right>0:
                answer = V[:right]
            else:
                answer = []
            answer.sort()
            zero_index = bisect.bisect_left(answer,0)
            
            answer = answer[min(zero_index,K-i):]
            a = max(a,sum(answer))
    
    print(a)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    V = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, V)

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
lists=list(map(int,input().split()))
ans=0
nums=min(n,k)
for j in range(1,nums+1):
    
    
    
    for m in range(j+1):
        sublist=lists[:m]+lists[n+m-j:]
        sublist=sorted(sublist)
        sublist.append(0)
        del_some=0
        while del_some<k-j and min(sublist)<0:
            del sublist[0]
            del_some+=1
        p=sum(sublist)
        ans=max(ans,p)
print(ans)

Solution (Python):
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=0
for i in range(min(n+1,k+1)):
    for j in range(min(n-i+1,k-i+1)):
        a=v[:i]+v[n-j:]
        a.sort(reverse=True)
        for _ in range(k-i-j):
            if a and a[-1]<0:
                a.pop()
        ans=max(ans,sum(a))
print(ans)

Solution (Python):
n,k = map(int,input().split())
v=list(map(int,input().split()))
kaisuu = min(n,k)
res = 0
for i in range(kaisuu+1):
    for j in range(kaisuu-i+1):
        tres = v[:i]+v[n-j:]
        tres.sort(reverse=True)
        for l in range(k-i-j):
            if not tres: break
            if tres[-1] < 0:
                tres.pop()
            else:break
        res = max(res,sum(tres))

print(res)

Solution (Python):

import sys
from collections import deque
import heapq

sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, K: int, V: "List[int]"):
    
    
    n = min(N,K)

    ans = 0
    for i in range(n+1):
        for j in range(n-i+1):
            s = V[:i] + V[::-1][:j]
            
            s.sort(reverse=True)
            for k in range(max(0, K-(i+j))):
                t = s.copy()
                if(len(s) >= 1 and s[-1] < 0):
                    s.pop()

                    
            ans = max(ans, sum(s))

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    V = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, V)

if __name__ == '__main__':
    main()

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

def chmax(a,b):
    if a < b:
        a = b
    return a

Max = 0

for i in range(K+1):
    if i > N:
        break

    for j in range(K+1-i):
        if i+j > min(N,K):
            break
        list = V[0:i]
        list.extend(V[N-j:N])

        for k in range(K-i-j):

            if len(list) == 0:
                break

            if min(list) < 0:
                list.pop(list.index(min(list)))
            else:
                break

        Max = chmax(Max,sum(list))

print(Max)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=0
for i in range(min(n,k+1)):
  for j in range(min(n-i+1,k-i+1)):
    h=sorted(a[:i]+a[n-j:])[::-1]
    for _ in range(k-i-j):
      if h and h[-1]<0:h.pop()
    x=max(x,sum(h))
print(x)

Solution (Python):
from heapq import heapify, heappop, heappush
N, K = map(int, input().split())
V = list(map(int, input().split()))
answer = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        if i + j > N:
            break
        heap = V[:i] + V[N - j:]
        heapify(heap)
        for _ in range(K - i - j):
            if heap:
                temp = heappop(heap)
            else:
                heappush(heap, 0)
                break
            if temp >= 0:
                heappush(heap, temp)
                break
        answer = max(answer, sum(heap))

print(answer)

Solution (Python):
from collections import deque
import copy

N, K=map(int, input().split())
v =  deque(list(map(int, input().split())))
score = []
k = min(N,K)

for A in range(k + 1):
  for B in range(k - A + 1):
    V = copy.deepcopy(v)
    temochi = []
    
    
    for _ in range(A):
      temochi.append(V.popleft())
    for _ in range(B):
      temochi.append(V.pop())
    for _ in range(K - A - B):
      if K - A - B == 0:
        break
        
      temochi.sort()
      if len(temochi)==0 or temochi[0] >= 0:
        break
      else:
        del temochi[0]
    
    score.append(sum(temochi))
print(max(score))

Solution (Python):
from collections import deque
import copy

N, K=map(int, input().split())
v =  deque(list(map(int, input().split())))
score = []
k = min(N,K)

for A in range(k + 1):
  for B in range(k - A + 1):
    V = copy.deepcopy(v)
    temochi = []
    
    
    for _ in range(A):
      temochi.append(V.popleft())
    for _ in range(B):
      temochi.append(V.pop())
    for _ in range(K - A - B):
      if K - A - B == 0:
        break
        
      temochi.sort()
      if len(temochi)==0 or temochi[0] >= 0:
        break
      else:
        del temochi[0]
    
    score.append(sum(temochi))
print(max(score))

Solution (Python):
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(k + 1 - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)

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

Solution (Python):
import math
N, K = map(int,input().split())
V = list(map(int,input().split()))
V_rev = list(reversed(V))
ans = 0
tmp = -1 * float("inf")
L = 0
R = 0

for A in range(min(N+1,K+1)):
  for B in range(min(N+1,K+1) - A):
    if A == 0 and B == 0:continue
    tmp = -1 * float("inf")
    if A == 0:
      tmp = sum(V_rev[:B])
    elif B == 0:
      tmp = sum(V[:A])
    else:
      tmp = sum(V[:A])+sum(V_rev[:B])
    if A == 0:
      push_list = V_rev[:B]
    elif B == 0:
      push_list = V[:A]
    else:
      push_list = V[:A] + V_rev[:B]
    push_list = sorted(push_list)
    
    for i in range(min(len(push_list),K-(A+B))):
      if push_list[i] < 0:
        tmp += abs(push_list[i])
        
    ans = max(ans,tmp)
print(ans)

Solution (Python):
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
gems = list(map(int, stdin.readline().rstrip().split()))
total = -99999999999
for i in range(min(n, k)+1):
    for j in range(min(n, k)+1):
        if i + j <= min(n, k):
            tmp = gems[:i] + gems[n-j:]
            tmp.sort()
            for l in range(min(k-i-j, len(tmp))):
                if tmp[l] < 0:
                    tmp[l] = 0
            total = max(total, sum(tmp))


print(total)

Solution (Python):
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



for i in range(min(N+1,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()



for i in range(min(N+1,K+1)):
    for j in range(max(0,min(N+1,K-i+1))):
        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]

                    if m < 0:
                        del dp[i][j][0][0]


            if dp[i][j][0]:

                ansC.append(sum(dp[i][j][0][::]))


if ansC:
    print(max(ansC))
else:
    print("0")

Solution (Python):
import sys
input = sys.stdin.readline

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

for dq in range(min(n+1, k+1)):
    for rdq in range(dq+1):
        ldq = dq-rdq
        jwl = v[n-rdq:]+v[:ldq]
        jwl.sort()

        val = sum(jwl)
        for i in range(min(k-dq, len(jwl))):
            if jwl[i] < 0:
                val -= jwl[i]
            else:
                break

        ans = max(ans, val)

print(ans)

Solution (Python):
N,K = map(int, input().split())
V = list(map(int, input().split()))
ans = -1 * (10**10)
for left in range(min(K+1, N+1)):
	for right in range(0, min(K-left+1, N-left+1)):
		push = K - left - right
		items = []
		items.extend(V[:left])
		items.extend(V[N-right:])
		items.sort()
		tmp = 0
		for idx in range(len(items)):
			if(idx >= push or items[idx] > 0):
				tmp += items[idx]
		ans = max(ans, tmp)
print(ans)

Solution (Python):
N,K = map(int, input().split())
V = list(map(int, input().split()))
import sys
max_value=-1000000000

for i in range(K+1):
        for left in range(i+1):
                right= i - left

                leftlist = V[:left]

                if right == 0:
                        rightlist = []
                else:
                        rightlist = V[-right:]

        
        


                leftlist.extend(rightlist)

                leftlist.sort()




                num = 0
                for value in range(len(leftlist)):
                        num+=1
                        if num > K-i:
                                break

                        if leftlist[value] > 0:
                                break
                        leftlist[value]=0
        



                s = sum(leftlist)
                if max_value < s:
                        max_value = s

                if left+right > N:
                        print(max_value)
                        sys.exit()

print(max_value)

Solution (Python):





from collections import deque
import heapq

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

ans = []

for n_take in range(K+1):
    for n_take_from_left in range(n_take+1):
        have = []
        V = deque(original_V)
        
        for _ in range(n_take_from_left):
            if V:
                heapq.heappush(have, V.popleft())
            else:
                break
        
        for _ in range(n_take - n_take_from_left):
            if V:
                heapq.heappush(have, V.pop())
            else:
                break
    
        for _ in range(K - n_take):
            if have and have[0] < 0:
                heapq.heappop(have)
            else:
                break
                
        ans.append(sum(have))
    
print(max(ans))

Solution (Python):





from collections import deque
import heapq

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

ans = []

for n_take in range(K+1):
    for n_take_from_left in range(n_take+1):
        have = []
        V = deque(original_V)
        
        for _ in range(n_take_from_left):
            if V:
                heapq.heappush(have, V.popleft())
            else:
                break
        
        for _ in range(n_take - n_take_from_left):
            if V:
                heapq.heappush(have, V.pop())
            else:
                break
    
        for _ in range(K - n_take):
            if have and have[0] < 0:
                heapq.heappop(have)
            else:
                break
                
        ans.append(sum(have))
    
print(max(ans))

Solution (Python):
N, K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
ans = 0
for i in range(min(N, K)+1):
    for j in range(min(N, K)+1-i):
        tmp = []
        tmp.extend(V[:i])
        tmp.extend(V[N-j:])
        lst = sorted(tmp)
        for k in range(K-(i+j)+1):
            if (k >= len(lst)): break
            if (k==0): 
                s = sum(lst)
                if (ans < s):
                    ans = s
                continue
            if (lst[k-1] < 0):
                s = sum(lst[k:])
                if (ans < s):
                    ans = s
            else:
                break
print(ans)

Solution (Python):
N,K = map(int, input().split())
V = [int(v) for v in input().split()]

ans = 0
for i in range(K+1):
    if i < N:
        for j in range(i+1):
            temp1 = V[:j]
            temp2 = V[N-(i-j):]
            temp = temp1 + temp2
            temp.sort()
            s = sum(temp)
            k = 0
            while k < len(temp) and k < K-i:
                if temp[k] < 0:
                    s -= temp[k]
                k += 1
            ans = max(ans, s)
    else:
        temp = sorted(V)
        s = sum(temp)
        j = 0
        while j < min(N, i-N):
            if temp[j] < 0:
                s -= temp[j]
            j += 1
        ans = max(ans, s)
        
print(ans)

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

m = min(n, k)
ans = -10**9
for l in range(m+1):
    for r in range(m-l+1):
        jry = v[:l]+v[-r:] if r>=1 else v[:l]
        t = k - l - r
        s = 0
        for i in sorted(jry):
            if i<0 and t>0:
                s += -i
                t -= 1
        ans = max(ans, sum(jry)+s)

print(ans)

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

ans = 0
for left in range(K + 1):
    for right in range(K + 1):
        if left > N - right or left + right > K:
            continue

        G = V[:left] + V[N - right:]
        G.sort()
        dis = 0
        for i, g in enumerate(G):
            if i >= K - (left + right) or g >= 0:
                break
            dis += g

        ans = max(ans, sum(G) - dis)

print(ans)

Solution (Python):
import heapq
n,k=map(int,input().split())
v=[int(x) for x in input().split()]

p = list()
m = min(n,k)
t = 0

for h in range(1,m+1):
  for i in range(0,h+1):
    a = b = 0
    p.clear()
    for j in range(0,i):
      if v[j]<0:
        p.append(v[j])
      a += v[j]
    for j in range(1,h-i+1):
      if v[-j]<0:
        p.append(v[-j])
      b += v[-j]
    p.sort(reverse=True)
    n = a+b
    for i in range(min(k-h,len(p))):
      n -= p.pop()
    t = max(t,n)
print(t if t > 0 else 0)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

vals = []
for L in range(N+1):
    for R in range(N+1):
        if L+R > N:
            break
        get = V[:L] + V[N-R:]
        rest = K - (L+R)
        if rest < 0:
            break
        get.sort(reverse=True)
        for _ in range(min(len(get),rest)):
            if get[-1] < 0:
                get.pop()
            else:
                break
        vals.append(sum(get))

answer = max(vals)
print(answer)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

N, K = map(int, input().split())
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, min(N, K)+1): 
    for j in range(0, min(i, K-i)+1): 
        for k in range(max(0, N-i), N+1): 
            use = V2[k:k+i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s

print(answer)

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

answer = 0
limit = min(N, K)
for a in range(limit+1): 
    for b in range(limit-a+1): 
        for c in range(K-(a+b)+1):
            use = V[:a] + V[N-b:]
            use.sort()
            s = sum(use[c:])
            answer = max(answer, s)

print(answer)

Solution (Python):

import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,k=map(int,input().split())
v1=list(map(int,input().split()))
v2=v1[::-1]
r=min(n,k)
ans=0
for i in range(r+1):
    for j in range(i+1):
        tmp=v1[:j]+v2[:i-j]
        tmp.sort()
        for l in range(min(k-i,i)):
            if tmp[l]<0:
                tmp[l]=0
        ans=max(ans,sum(tmp))

print(ans)

Solution (Python):
n,k = map(int,input().split())
D = list(map(int,input().split()))

def opab(left,right,m,D):
    jew = []
    for i in range(left):
        jew.append(D[i])
    for i in range(n-right,n):
        jew.append(D[i])
    jew.sort()
    count = 0
    while count < m:
        if not jew:
            break
        if jew[0] <0:
            jew.pop(0)
            count +=1
        else:
            break
    return sum(jew)
ans = 0
for k_i in range(k+1):
    for left in range(min(k_i,n)+1):
        for right in range(max(0,min(k_i,n)-left)+1):
            m = max(0,k - left -right)
            ans = max(ans,opab(left,right,m,D))
print(ans)

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

valu=[]
for a in range(min(N,K)+1):
  for b in range(min(N,K)-a+1):
    val=sorted(V[:a]+V[N-b:])
    n=K-(a+b)
    for i in range(n):
      if len(val)>0:
        if val[0]<0:
          val.pop(0)
        else:
          break
    valu.append(sum(val))
    
print(max(valu))

Solution (Python):

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

res = -1

for l in range(min(n+1,k+1)):
    for r in range(min(n+1,k+1)):
        if l+r>k or l+r>n:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(min(w,len(sl))):
            if sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)

Solution (Python):
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 > N:
                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-r,N):
                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()

Solution (Python):
import heapq

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

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

ans = 0

for R in range(N-1):

    R += 1

    for L in range(R):

        if (L + 1) + (N - R) > K:
            continue

        q = []
        vsum = 0
        for i in range(N):

            if i <= L or R <= i:
                heapq.heappush(q,V[i])
                vsum += V[i]

        for i in range(K - (L + 1) - (N - R)):

            if len(q) > 0 and q[0] < 0:
                now = heapq.heappop(q)
                vsum -= now

            else:
                break

        ans = max(ans,vsum)

for L in range(N):

    if L+1 > K:
        continue

    q = []
    vsum = 0

    for i in range(N):
        if i <= L:
            heapq.heappush(q,V[i])
            vsum += V[i]

    for i in range(K - (L+1)):
        if len(q) > 0 and q[0] < 0:
            now = heapq.heappop(q)
            vsum -= now
        else:
            break
    ans = max(ans,vsum)

for R in range(N):

    if N-R > K:
        continue

    q = []
    vsum = 0

    for i in range(N):
        if R <= i:
            heapq.heappush(q,V[i])
            vsum += V[i]

    for i in range(K - (L+1)):
        if len(q) > 0 and q[0] < 0:
            now = heapq.heappop(q)
            vsum -= now
        else:
            break
    ans = max(ans,vsum)

print (ans)

Solution (Python):
from collections import deque
from copy import deepcopy
N, K = map(int, input().split())

V = deque(map(int, input().split()))
ans = 0
for l in range(K + 1):
    for r in range(0 , K - l + 1):
        work_V = deepcopy(V)
        if l + r > N:
            continue
        d = K - (r + l)
        now = 0
        hold = []
        for i in range(l):
            
            hold.append(work_V.popleft())
        for i in range(N - r , N):
            
            hold.append(work_V.pop())
        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)

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

for i in range (0,min(N,K)+1):
    for j in range(0,min(N,K)+1-i):
        CNT = []
        tmp =K-(i+j)
        for k in range(0,i):
            CNT.append(V[k])
        for l in range(0,j):
            CNT.append(V[N-1-l])
        heapq.heapify(CNT)
        for h in range(0,tmp):
            if len(CNT)>0 and CNT[0]<0:
                heapq.heappop(CNT)
        ans=max(ans,sum(CNT))

print(ans)

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

def query(l, r):
    if (l+r > K):
        return 0
    rest = K-l-r
    lst = V[:l] + V[N-r:]
    lst.sort()
    return sum([x for i, x in enumerate(lst) if x > 0 or i >= rest])

ans = 0
for l in range(N+1):
    for r in range(N+1-l):
        ans = max(ans, query(l, r))
print(ans)

Solution (Python):
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
        if j > n:
            continue
        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])


        r = max(r, m)
print(r)

Solution (Python):
N,K = map(int,input().split())
V = [int(v) for v in input().split()]

ans = 0
for i in range(min(N+1,K+1)):
  que1 = V[:i]
  for j in range(min(N+1-i,K+1-i)):
    que2 = que1 + V[N-j:]
    que2.sort()
    for k in range(min(K+1-i-j,i+j+1)):
      ans = max(ans,sum(que2[k:]))
print(ans)

Solution (Python):
import math
from collections import deque 
import heapq 
N,K=map(int,input().split())
box=list(map(int,input().split()))

ans=0
for M in range(0,K+1):
    for i in range(math.ceil(M//2),min(M+1,N+1)):
        for l in range(0,i+1):
          keep=[]
          boxA=deque(box)
          for _ in range(l):
            heapq.heappush(keep, boxA.popleft())
          for _ in range(i-l):
            heapq.heappush(keep, boxA.pop())
          for _ in range(min(M-i,i)):
            S=heapq.heappop(keep)
            if int(S)>0:
                heapq.heappush(keep,S)
          ans= max(ans,sum(keep))
print(ans)

Solution (Python):
import heapq
import sys
input = sys.stdin.readline
 
 
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    M = min(N, K)
    ans = 0
    for m in range(1, M+1):
        for l in range(m+1):
            Q = []
            for i in range(l):
                Q.append(V[i])
            r = m-l
            for i in range(r):
                Q.append(V[N-1-i])
            heapq.heapify(Q)

            for _ in range(K-m):
                if len(Q)==0:
                    break
                q = heapq.heappop(Q)
                if q>=0:
                    heapq.heappush(Q, q)
                    break
            
            ans = max(ans, sum(Q))
    
    print(ans)

 
if __name__ == '__main__':
    main()

Solution (Python):
import heapq

N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = -float('inf')
for ai in range(K+1):
  for bi in range(K+1):
    if ai+bi > min(K,N): continue
    have_plus = []
    have_neg = []
    if ai != 0: [heapq.heappush(have_plus, v) if v >= 0 else heapq.heappush(have_neg, v) for v in V[:ai]]
    if bi != 0: [heapq.heappush(have_plus, v) if v >= 0 else heapq.heappush(have_neg, v) for v in V[N-bi:]]
    for i in range(K-ai-bi):
      if len(have_neg) != 0:
        heapq.heappop(have_neg)
    ans = max(ans,sum(have_plus)+sum(have_neg))
print(ans)

Solution (Python):
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)

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

ans = 0
for i in range(0,K+1):
    for j in range(0,K+1-i):
        D = deque(V)
        num = 0 
        a = []  

        cnt = 0
        while cnt < i and len(D):   
            x = D.pop()
            heapq.heappush(a,x)
            num += x
            cnt += 1
        cnt = 0
        while cnt < j and len(D):   
            x = D.popleft()
            heapq.heappush(a,x)
            num += x
            cnt += 1
        sub = K-i-j
        while sub and len(a):       
            y = heapq.heappop(a)
            if y <= 0:              
                num -= y
            sub -= 1
        ans = max(ans,num)
print(ans)

Solution (Python):
from collections import deque
from copy import deepcopy
from heapq import heappop, heappush, heapify


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

mx = -10**9
mn = min(n, k)+1

for i in range(mn):
    for j in range(mn-i):

        heap = []
        u = deepcopy(v)
        heapify(heap)

        if i >= 1:
            for x in range(i):
                heappush(heap, u.pop())

        if j >= 1:
            for x in range(j):
                heappush(heap, u.popleft())

        if k - i - j >= 1:
            for x in range(k-i-j):
                if heap:
                    min = heappop(heap)

                    if min >= 0:
                        heappush(heap, min)
                        break
        mx = max(mx, sum(heap))

print(mx)

Solution (Python):
import heapq
from collections import deque
from copy import deepcopy

n,k = map(int,input().split())
U = deque(map(int,input().split()))

Max = -10**9

for a in range(min(n,k)+1):
  for b in range(min(n,k)+1-a):
    
    V = deepcopy(U)
    Heap = []
    heapq.heapify(Heap)
    
    if a >= 1:
      for i in range(a):
        heapq.heappush(Heap,V.pop())
        
    if b >= 1:  
      for i in range(b):
        heapq.heappush(Heap,V.popleft())
        
    if k-a-b >= 1:
      for i in range(k-a-b):
        
        if len(Heap) >= 1:
          Min = heapq.heappop(Heap)
                 
          if Min >= 0:
            heapq.heappush(Heap,Min)
            break

    Max = max(Max,sum(Heap))
    
print(Max)

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


ret = -1 * 10**7 * 50 

for left in range(K+1):
    for right in range(K-left+1):
        if left + right > N:
            continue

        disposable = K - right - left
        
        chosen = V[:left] +  V[N - right:]
        chosen.sort()
        chosen_count = sum(chosen)

        dispoase_value = 0
        for i in range(disposable):
            if i >= len(chosen):
                break
            if chosen[i] >= 0:
                break
            dispoase_value += chosen[i]


        val = chosen_count - dispoase_value

        ret = max(ret, val)

print(ret)