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):
from heapq import heappop,heappush


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


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res)

if __name__=="__main__":
    main()

Solution (Python):
from collections import deque

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

mini = min(n,k)
mx = 0

for a in range(mini+1):
    for b in range(mini+1-a):
        if b==0:
            bb=-n-2
        else:
            bb=b
        ttl = sum(v[:a])+sum(v[-bb:])
        
        hu = [i for i in v[:a] if i<0 ]
        hu.extend([i for i in v[-bb:] if i<0] )
        hu.sort()
        ttl-= sum(hu[:k-a-b])
        mx = max(mx,ttl)
print(mx)

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:
            break
        d = K-(l+r)

        j = V[:l]+V[N-r:]
        j.sort()

        for i in range(d):
            if not j:
                break
            elif j[0] >= 0:
                break
            j.pop(0)

        ans = max(ans, sum(j))
print(ans)

Solution (Python):



input_list = lambda : list(map(int,input().split()))

from collections import Counter 
from collections import defaultdict
from collections import deque
import itertools
import heapq
import sys
sys.setrecursionlimit(10**6)

n,m=input_list()
a=deque(input_list())
ans=0
for get_num in range(m+1):
    ret_num=min(m-get_num,get_num)
    if get_num>n:
        get_num=n
        ret_num=min(m-get_num,get_num)
    
    for get_left in range(get_num+1):
        cal=[]
        get_right=get_num-get_left
        val=0
        for i in range(get_left):
            val+=a[i]
            heapq.heappush(cal,a[i])
        for i in range(get_right):
            ii=n-1-i
            val+=a[ii]
            heapq.heappush(cal,a[ii])
        for _ in range(ret_num):
            p_val=heapq.heappop(cal)
            if p_val>0:
                break
            val-=p_val
        ans=max(ans,val)
print(ans)

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

max_value = 0
for num_take in range(min(N, K)+1):
    max_put = min(K - num_take, max(num_take-1, 0))
    for i in range(num_take+1):
        deque = list(V)
        gems = []
        left_right = [0 for _ in range(i)] + [1 for _ in range(num_take-i)]
        for which in left_right:
            if len(deque) > 0:
                if which == 0:
                    gems.append(deque.pop(0))
                else:
                    gems.append(deque.pop(-1))
        gems.sort()

        value = max([sum(gems[num_put:]) for num_put in range(max_put+1)])
        max_value = max(max_value, value)
        
print(max_value)

Solution (Python):
N, K = map(int,input().split())
v = list(map(int,input().split()))
ans = [0]
for i in range(1,min(K,N)+1):
    for a in range(i+1):
        b = i-a
        if b == 0:
           get = v[:a]
        else:
            get = v[:a]+v[-b:]
        get.sort()
        for j in range(min(K-i,len(get))):
            p = get.pop(0)
            if p >= 0:
                A = p + sum(get)
                break
        else:
            A = sum(get+[0])
        ans.append(A)
print(max(ans))

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
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)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
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)

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

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans)
if __name__ == '__main__':
    solve()

Solution (Python):
n,k = map(int,input().split())
v = list(map(int,input().split()))
m = 0
for i in range(n+1):
  if i > k:
    break
  s = sum(v[:i])
  h = [j for j in v[:i] if j < 0]
  h.sort()
  m = max(m,s-sum(h[:min(len(h),k-i)]))
  for j in range(1,n-i+1):
    if i+j > k:
      break
    cs = s
    cs += sum(v[-j:])
    ch = h[:]
    ch += [j for j in v[-j:] if j < 0]
    ch.sort()
    cs -= sum(ch[:min(len(ch),k-i-j)])
    m = max(m,cs)
print(m)

Solution (Python):
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(min(N, K)+1):
        for y in range(min(N, K)-x+1):
            z = K - x - y
            temp = V[:x] + V_rev[:y]
            heapify(temp)
            for _ in range(z):
                if temp:
                    m = heappop(temp)
                    if m >= 0:
                        heappush(temp, m)
                        break
                else:
                    break

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


if __name__ == '__main__':
    main()

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



for i in range(1,K+1):
	for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			
			
			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)

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

ans = 0  



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)

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


score=0
torikata=[]
for d in range(101):
    for a in range(101):
        for b in range(101):
            if d+a+b<=K and a+b<=N:
                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)

Solution (Python):

n, k = map(int, input().split())
v = list(map(int, input().split()))
q = []
ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        
        if a == 0:
            pass
        else:
            q.extend(v[:a])
        if b == 0:
            pass
        else:
            q.extend(v[-b:])
        q.sort()
        pick = k - (a + b)
        for i in range(min(pick, len(q))):
            
            if q[0] < 0:
                q.reverse()
                q.pop()
                q.reverse()
            else:
                break
        s = sum(q)
        
        if ans < s:
            ans = s
        q.clear()
print(ans)

Solution (Python):
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
min_nk = min(n, k)
for l in range(min_nk+1):
    for r in range(min_nk-l+1):
        hand = []
        for i in range(l):
            hand.append(v[i])
        for i in range(r):
            hand.append(v[n-1-i])
        hand.sort()
        
        for i in range(min(k-l-r, len(hand))):
            if hand[i] < 0:
                hand[i] = 0
        ans = max(ans, sum(hand))
print(ans)

Solution (Python):
import bisect
N, K = map(int, input().split())
V = list(map(int, input().split()))
num_get = min(N, K)
ans = -float('inf')
for left in range(0, num_get+1):
    for right in range(N-1, N-1 - (num_get+1 - left), -1):
        inhand = sorted(V[0:left] + V[right+1:N])
        zero_idx = bisect.bisect_left(inhand, 0)
        tmp_ans = sum(inhand[min(zero_idx, max(K - len(inhand), 0)):])
        ans = max(ans, tmp_ans)
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

value = np.array(list(map(int, input().split())))
r_value = value[::-1]

max_value = max(value[0], r_value[0])
for i in range(1, k):
    total_value = 0
    if i > n:
        s = value
        s.sort()
        s = list(s)
        for j in range(k-n):
            if s[0] < 0:
                s.pop(0)
        total_value = sum(s)
    else:
        for j in range(i+1):
            a = value[0:j]
            b = r_value[0:i-j]
            c = np.concatenate((a, b))
            c.sort()
            c = list(c)
            for l in range(k-i):
                if c[0] < 0:
                    c.pop(0)
                if c == []:
                    break
            total_value = sum(c)
            if total_value > max_value:
                max_value = total_value
print(max_value)

Solution (Python):
import heapq

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_cnt = min(N, K)
ans = 0

for a in range(0, max_cnt + 1):
    for b in range(0, max_cnt + 1):
        if a + b > max_cnt:
            break
        if a == b == 0:
            continue
        hoseki = V[:a] + V[::-1][:b]
        heapq.heapify(hoseki)
        tmp = K - (a + b)
        while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
        ans = max(ans, sum(hoseki))

print(ans)

Solution (Python):
import heapq,copy

N,K = (int(x) for x in input().split())
V = list(map(int, input().split()))
v = copy.copy(V)
ans = 0
if N <= K:
    heapq.heapify(v)
    for i in range(min(K-N,len(v))):
        temp = heapq.heappop(v)
        if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
    else:
        ans = max(ans,sum(v))
for i in range(min(N,K+1)):
    for j in range(min(N,K+1)-i):
        if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l))
print(ans)

Solution (Python):
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for i in range(K+1):
        cand = []
        for left in range(i+1):
            cand = V[:left]
            right = i - left
            cand += V[max(left,N-right):N]

            cnt = K - i
            n = len(cand)
            tmp = sum(cand)
            cand.sort()
            for c in range(min(cnt,n)):
                if cand[c] < 0:
                    tmp -= cand[c]
                else:
                    break
            ans = max(ans, tmp)


    print(ans)




        

if __name__ == "__main__":
    main()

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

def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V


def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                
                
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Solution (Python):
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 n_pick in range(min(N, K) + 1):
        for left in range(n_pick + 1):
            right = n_pick - left
            tmp = []
            for i in range(left):
                tmp.append(V[i])
            for i in range(right):
                tmp.append(V[N - i - 1])
            tmp.sort()
            s = sum(tmp)
            n_return = min(K - n_pick, len(tmp))
            for i in range(n_return):
                if tmp[i] < 0:
                    s -= tmp[i]
                else:
                    break
            if s > ans:
                ans = s

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq
from collections import deque


def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value


def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v)


if __name__ == '__main__':
    main()

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

ans=0
minnk = min(n,k)
for r in range(minnk+1):
    for l in range(minnk+1-r):
        sute = k-(r+l)
        
        temoti = sorted(v[:l] +v[n-r:])
        n_nega = bisect.bisect(temoti,0)
        score = sum(temoti[min(n_nega,sute):])

        if ans<score:
            ans=score
print(ans)

Solution (Python):


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 or l+r>gem:
      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)

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

ans = -10**7
for i in range(min(N,K)+1):
    for j in range(min(N-i,K-i)+1):
        for k in range(min(i+j,K-i-j)+1):
            T = V[:i+1]
            T.extend(V[N-j+1:])
            T.sort()
            for t in range(k):
                if T[t] < 0:
                    T[t] = 0
                else:
                    break
            ans = max(ans,sum(T))

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K)
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        v=A[:i]+A[N-j:]
        v.sort()
        v=v[::-1]
        h=K-len(v)
        while h >0 and v:
            if v[-1] > 0:
                break
            else:
                v.pop()
                h-=1
        M=max(M,sum(v))
print(M)

Solution (Python):
import heapq 
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) 
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(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A))
print(M)

Solution (Python):
import heapq 

N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) 
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
        heapq.heapify(new_A)
        h = K - len(new_A)
        while new_A  and h >0:
            q = heapq.heappop(new_A)
            h-=1
            if q > 0:
                heapq.heappush(new_A,q)
                break
        M = max(M,sum(new_A))
print(M)

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
import heapq 
m = min(N,K)
M = 0
for i in range(m+1):
    for j in range(m-i+1):
        v1 = V[:i]
        v3 = V[N-j:]
        v = v1+v3
        heapq.heapify(v)
        h = K - len(v)
        while v  and h >0:
            q = heapq.heappop(v)
            h-=1
            if q > 0:
                heapq.heappush(v,q)
                break
        tmp = sum(v)
        
        M = max(M,tmp)
print(M)

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
import heapq 
m = min(N,K)
M = 0
for i in range(m+1):
    for j in range(m-i+1):
        v1 = V[:i]
        v3 = V[N-j:]
        
        v = v1+v3
        heapq.heapify(v)
        h = K - len(v)
        while v and len(v) and h >0:
            q = heapq.heappop(v)
            h-=1
            if q > 0:
                heapq.heappush(v,q)
                break
        tmp = sum(v)
        
        M = max(M,tmp)
print(M)

Solution (Python):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = a[::-1]
ans = 0
from heapq import heapify,heappop,heappush

for l in range(1,k+1):
  ans_l = 0
  ans_r = 0
  for j in range(1,l+1):
    left = a[:j]
    chk_l = sum(left)
    heapify(left)
    heappush(left,0)
    for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
    ans_l = max(ans_l,chk_l)
    
  if l < n:
    r = k-l
    for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
  ans = max(ans,ans_r+ans_l)
print(ans)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
R = min(N, K)
ans = 0
for a in range(R+1):
  for b in range(R+1-a): 
    tmp = list()
    for i in range(a): 
      tmp.append(V[i])
    for j in range(b):
      tmp.append(V[N-1-j])
    tmp.sort()
    x = min(K-a-b, len(tmp))
    for k in range(x):
      if(tmp[k] < 0):
        tmp[k] = 0
    ans = max(ans, sum(tmp))
print(ans)

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


ruil=[0]*N
ruil[0]=V[0]

ruir=[0]*N
ruir[0]=V[-1]
for i in range(1,N):
    ruil[i]=ruil[i-1]+V[i]
    ruir[-(i+1)]=ruir[-i]+V[-(i+1)]



ans=0
for i in range(K+1):
    if i==0:
        continue
    for j in range(0,i+1):
        if i/2<=j:
            continue
        
        
        for k in range(i-j+1):
            if i-j>=N:
                get=V
            else:
                if i-j-k==0:
                    get=V[:k]
                else:
                    get=V[:k]+V[-(i-j-k):]
            get=sorted(get)
            
            score=sum(get)-sum(get[:j])
            ans=max(ans, score)
            

print(ans)

Solution (Python):
def main():
    N, K = map(int, input().split())
    *V, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    main()

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 n < L+R:
            break
        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)

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

ans = 0
for i in range(n+1):
    for j in range(n+1):
        if i+j>k: continue
        cand = sorted([v[I] for I in range(n) if I<i or n-j<=I])
        cnt = sum(cand)
        ans = max(ans, cnt)
        for l in range(min(k-i-j, len(cand))):
            cnt -= cand[l]
            ans = max(ans, cnt)
print(ans)

Solution (Python):
N, K = map( int, input().split() )
V = list( map( int, input().split() ) )
 
max_number = 0
for A in range( K + 1 ):
  for B in range( K + 1 ):
    J = K - A - B
    if J < 0:
      continue
    if A + B >= N:
      hand = V
    else:
      hand = V[ : A ] + V[ N - B : ]
    hand = sorted( hand )
      
    hand_num = 0
    for i, j in enumerate(hand):
      if ( j > 0 ) or ( i == J ):
        hand = hand[ i: ]
        break
    max_number = max( max_number, sum( hand ))
print( max_number )

Solution (Python):
import sys
from collections import deque
import heapq

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    for i in range(1, k + 1):
        
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
from bisect import bisect_right
N,K = map(int,input().split())
V = list(map(int,input().split()))
cmax = -10**9
for x in range(K+1):
    for y in range(K-x+1):
        A = V[:x]
        if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
print(cmax)

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

import heapq
ans = 0
for a in range(min(N,K)+1):
    for b in range(min(N,K)-a+1):
        A = V[:a] + V[N-b:]
        heapq.heapify(A)
        for c in range(min(a+b, K-a-b)):
            small = heapq.heappop(A)
            if small > 0:
                heapq.heappush(A,small)
                break
        ans = max(ans, sum(A))
print(ans)

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

import heapq
ans = 0
for a in range(min(N,K)+1):
    for b in range(min(N,K)-a+1):
        A = V[:a] + V[N-b:]
        heapq.heapify(A)
        for c in range(min(a+b, K-a-b)):
            small = heapq.heappop(A)
            if small > 0:
                heapq.heappush(A,small)
                break
        ans = max(ans, sum(A))
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations, combinations_with_replacement
from bisect import bisect_left, bisect_right  


N, K = read_ints()
V = read_ints()

ans = 0
for i, j in combinations_with_replacement(range(N + 1), r=2):
    
    k = i + (N - j)
    if k > K:
        continue
    Vsub = V[:i] + V[j:]
    
    
    Vsub.sort(reverse=True)
    while Vsub and k < K and Vsub[-1] < 0:
        k += 1
        Vsub.pop()
    
    ans = max(sum(Vsub), ans)
print(ans)

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

def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans
print(solve(N,K,V))