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 bisect import bisect_left


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

    score = 0
    for i in range(n + 1):
        for j in range(n - i + 1):
            if k < i + j:
                continue

            hands = v[:i] + v[n - j:]
            hands = sorted(hands)

            
            p = min(bisect_left(hands, 0), k - i - j)

            score = max(score, sum(hands[p:]))

    print(score)


main()

Solution (Python):
N, K = map(int, input().strip().split())
V = list(map(int, input().strip().split()))
H = []
maxi = 0
for i in range(min(N, K)):
    for o in range(i+2):  
        ts = 0
        hand = []
        for p in range(o):
            hand.append(V[p])
        for p in range(N-(i-o)-1, N):  
            hand.append(V[p])
        for values in hand:
            ts += values
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
            for j in range(K-i-1):  
                if len(hand) > 0:
                    if hand[0] < 0:
                        del hand[0]
                    else:
                        break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

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

m = min(N, K)
for l in range(m+1):
    for r in range(m+1-l):
        hold = V[:l] + V[N-r:]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
U = V[::-1]
ret = 0

m = min(N, K)
for l in range(m+1):
    for r in range(m+1-l):
        hold = V[:l] + U[:r]
        hold.sort()
        for d in range(K+1-l-r):
            ret = max(ret, sum(hold[d:]))

print(ret)

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

maxi = 0
for i in range(min(N, K)):
    for o in range(i+2):
        hand = []
        for p in range(o):
            hand.append(V[p])
        for p in range(N-(i-o)-1, N):
            hand.append(V[p])
        hand.sort()
        for j in range(K-i-1):
            if len(hand) > 0:
                if hand[0] < 0:
                    del hand[0]
                else:
                    break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

Solution (Python):
n, k = (int(i) for i in input().split())  
v = [int(i) for i in input().split()] 

r = min(n, k)



def kati(a, b):
  if a == 0 and b == 0:
    return 0
  else:
    ans = 0
    
    l = v[:a]
    ans += sum(l)
    
    r = v[::-1][:b]
    ans += sum(r)
    have = l + r
  
    
    s = k - (a + b)
    
    have = [i for i in have if i < 0]
    
    have.sort()
    

    if len(have) <= s:
      ans -= sum(have)
    else:
      ans -= sum(have[:s])
    return ans



tmp = []
for a in range(r+1):
  for b in range(r-a+1):
    x = kati(a, b)
 
    tmp.append(x)

print(max(tmp))

Solution (Python):
n, k = map(int, input().split())
v = list(map(int, input().split()))
u = v[::-1]
ans = 0

m = min(n, k)
for l in range(m + 1):
    for r in range(m - l + 1):
        s = v[:l] + u[:r]
        s.sort()
        for i in range(k - l - r + 1):
            ans = max(ans, sum(s[i:]))

print(ans)

Solution (Python):
from collections import deque

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

R = min(N, K)
ans = 0
for left in range(R+1):
    
    for right in range(R+1-left):
        if left + right > K:
            continue
        stay = K - left - right
        
        tmp = inputs[:left] + inputs[::-1][:right]
        tmp.sort()
        total = sum(tmp)
        if stay <= len(tmp):
            tmp = tmp[:stay]
        for item in tmp:
            if item < 0:
                total -= item
            else:
                break
        ans = max(ans, total)

print(ans)

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

ans=0

getl_sum=0
for lc in range(min(n,k)+1):
    getl_sum += v[lc]
    getr_sum =0
    for rc in range(min(n,k)-lc+1):
        getr_sum += v[-rc-1]
        get = v[:lc] + v[-rc-1:]
        get.sort()
        get_sum = getl_sum + getr_sum
        ans = max(ans,get_sum)
        for bc in range(min(lc+rc,k-lc-rc)):
            get_sum-=get[bc]
            ans = max(ans,get_sum)
print(ans)

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

N = N + 1 
res = 0
AB = []
if N < K:
	AB = V
	k = min(K-len(AB), len(AB))
	for idx in range(k):
		if AB[idx] < 0:
			AB[idx] = 0
	res = max(res, sum(AB))
else:
	for a in range(N):
		for b in range(N):
			if a + b <= K:
				AB = V[:a] + revV[:b]
				AB.sort()
				k = min(K-len(AB), len(AB))
				for idx in range(k):
					if AB[idx] < 0:
						AB[idx] = 0
				res = max(res, sum(AB))
print(res)

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
c=min(N,K)
p=[0]
for i in range(c+1):
  for j in range(c-i+1):
    a=[0]
    s=0
    for k in range(i):
      s=s+V[k]
      a.append(V[k])
    for l in range(j):
      s=s+V[N-1-l]
      a.append(V[N-1-l])
    for _ in range(K-i-j):
      if min(a) >=0:
        break
      else:
        s=s-min(a)
        a.remove(min(a))
    p.append(s)
print(max(p))

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
for i in range(min(N,K)+1): 
    for j in range(i+1):  
        Juel = V[:j]+V[N-i+j:]
        Juel.sort()
        sum_v = 0
        for k in range(min(K-i,i)):
            Juel[k] = max(Juel[k],0)
        ans = max(ans,sum(Juel))
print(ans)

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

def mc(arr):
    count = 0
    for ar in arr:
        if ar<0:
            count += 1
    return count

def ps(arr):
    sum = 0
    for ar in arr:
        if ar > 0 :
            sum += ar
    return sum

maxsumv = 0
for l in range(0,n+1):
    for r in range(0,n+1):
        if l+r > min(k,n):
            break
        else:
            hand = v[0:l] + v[n-r:n]
            hand.sort()
            c = mc(hand)
            for i in range(0,c+1):
                if l+r+i > k:
                    break
                else:
                    sumv = sum(hand[i:len(hand)])
                    maxsumv = max(sumv,maxsumv)
        
print(maxsumv)

Solution (Python):

import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > M: break
            if l+r == 0: continue
            
            having = V[:l] + V[::-1][:r]
            having.sort(reverse=True)

            restK = K - (l+r)

            for _ in range(restK):
                if len(having)!=0 and having[-1] < 0:
                    having.pop()
                    continue
                break

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

if __name__ == "__main__":
    solve()

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

ans = 0
sz = min(N,K)
for a in range(1+sz):
	for b in range(1+sz-a):
		
		taken = V[:a]+V[N-b:]
		taken.sort()
		for c in range(1+min(K-a-b,a+b)):
			score = sum(taken[c:])
			
			ans = max(ans,score)
print(ans)

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

def main():
	n, k = map(int, input().strip().split())
	v = list(map(int, input().strip().split()))
	ans = 0
	for a in range(k+1):
		for b in range(k-a+1):
			if a + b > n:
				continue
			t = sorted(v[:a] + v[n-b:])
			for i in range(k-a-b):
				if i < len(t) and t[i] < 0:
					t[i] = 0
			ans = max(ans, sum(t))
	print(ans)
if __name__ == '__main__':
	main()

Solution (Python):



import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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

ans = -float('inf')
for a in range(min(n,k)+1):
    for b in range(min(n,k)+1 - a):
        x = v[:a] + v[n-b:]
        x.sort()
        preans = sum(x)
        for i in range(k-a-b):
            if i < len(x):
                if x[i] < 0:
                    preans += abs(x[i])
                else:
                    break
            else:
                break
        ans = max(ans,preans)
print(ans)

Solution (Python):
N,K = map(int,input().split())
li = list(map(int,input().split()))
rim = min(N,K)
mans = 0
for A in range(rim+1):
    for B in range(rim-A+1):
        ansl = sorted(li[:A]+ li[N-B:])
        ans = sum(ansl)
        if ansl ==[]:continue
        
        for i in range(min(K-A-B,len(ansl))):
            if min(ansl) < 0 :
                ans -= min(ansl)
                ansl.remove(min(ansl))
            else:
                break;
        if ans > mans:
            mans = ans
print(mans)

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
R=min(N,K)
ans=0
for i in range(R+1):
	for j in range(R-i+1):
		if i==0 and j==0:continue
		L=sorted(V[:i]+V[N-j:])
		k=min(K-i-j,len(L)-1)
		for p in range(k):
			if L[p]>=0:
				k=p
				break
		ans=max(ans,sum(L[k:]))
print(ans)

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

res = 0
for p in range(min(N, K) + 1):
    q = 0
    while p + q <= min(N, K):
        v = []
        cur = 0
        for i in range(p):
            v.append(V[i])
            cur += V[i]
        for i in range(q):
            v.append(V[N-1-i])
            cur += V[N-1-i]
        v.sort()
        for i in range(min(K-p-q, len(v))):
            if v[i] < 0:
                cur -= v[i]
        res = max(res, cur)
        q += 1

print(res)

Solution (Python):
N, K = map(int, input().split())
jewels = list(map(int, input().split())) + [0]
ans = 0
for get in range(1, min(N, K)+1):
  put = min(get, K-get)
  for left in range(get+1):
    right = get - left
    values = jewels[:left] + jewels[N-right:]
    sum_v = sum(values)
    minus_values = list(filter(lambda x: x<0, values))
    minus_values.sort()
    cnt = 0
    for v in minus_values:
      if cnt < put:
        sum_v -= v
        cnt += 1
      else:
          break
    ans = max(ans, sum_v)
print(ans)

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

ans=0

for l in range(n):
    for r in range(n-l+1):
        if l + r > k:
            break 
        nv = v[:l] + v[n-r:] 
        nv.sort()
        for i in range(k-l-r):
            if not nv or nv[0] >= 0:
                break
            del nv[0]
        ans=max(ans,sum(nv))

print(ans)

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

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

max_value = 0

def calc_max_value(V, K, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(K-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                store.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(min(K+1,N+1)):
    for l in range(k+1):
        for r in range(k+1-l):
            max_value = max(max_value, calc_max_value(copy(V), K, k, l, r))

print(max_value)

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




nk=min(n,k)
discard=0
ans=0
for a in range(nk+1):
    for b in range(nk+1):
        if a+b>nk:
            break
        discard=k-(a+b)     
        dis=v[:a]+v[n-b:]   
        minus=[i for i in dis if i<0]  
        minus.sort()
        ans=max(sum(dis)-sum(minus[:min(discard,len(minus))]),ans)
print(ans)

Solution (Python):
import heapq

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

lesser = min(N, K)
ans = 0
for A in range(lesser + 1):
    for B in range(lesser + 1):
        if A + B > lesser:
            continue
        holding = V[:A] + V[N - B:]
        heapq.heapify(holding)
        S = sum(holding)
        CD = K - (A + B)
        for i in range(min(A + B, CD)):
            p = heapq.heappop(holding)
            if p >= 0:
                break
            S -= p
        ans = max(ans, S)
print(ans)

Solution (Python):
n,k = map(int,input().split())
v = [int(_) for _ in input().split()]
res = 0
for i in range(n+1):
    for j in range(n+1):
        if i+j > 0 and i+j<=min(n,k):
            l = v[0:i] + v[:-j-1:-1]
            l.sort()
            cnt = 0
            for p in range(len(l)):
                if l[p] >= 0 or cnt >= k-i-j:
                    break
                l[p] = 0
                cnt  += 1
            res = max(res,sum(l))
print(res)

Solution (Python):
import sys
from collections import deque, defaultdict
import copy
import bisect

sys.setrecursionlimit(10 ** 9)


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

max_num = 0
V_left = [0,V[0]]
V_right = [0,V[N - 1]]
V_left_m = [[] for i in range(N + 1)]
V_right_m = [[] for i in range(N + 1)]
for i in range(N - 1):
    V_left.append(V_left[-1] + V[i + 1])
    V_right.append(V_right[-1] + V[N - 2 - i])

for i in range(1, N+1):
    if i == 0:
        minus_l = []
        minus_r = []
    else:
        minus_l = copy.deepcopy(V_left_m[i - 1])
        minus_r = copy.deepcopy(V_right_m[i - 1])
    if V[i - 1] < 0:
        minus_l.append(V[i - 1])
    V_left_m[i] = minus_l
    if V[N  - i] < 0:
        minus_r.append(V[N - i])
    V_right_m[i] = minus_r

for k in range(1, min(N, K) + 1):
    for i in range(0, k + 1):
        point = V_left[i] + V_right[k - i]
        minus = V_left_m[i] + V_right_m[k - i]
        minus = list(minus)
        minus = sorted(minus)
        point -= sum(minus[0:K - k])
        
        if point > max_num:
            max_num = point
           

print(max_num)

Solution (Python):

from collections import deque
"""解説
全探索

左からL個、右からR個宝石を取り、K-(L+R)個の宝石を筒に詰めるとき方針として、

・筒に詰める宝石は絶対値の大きい負の値のもの

あとは全探索
"""

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

    ans = 0
    r = min(N, K)
    for a in range(r+1):  
        for b in range(r+1):  
            if r < a+b: continue

            t = V[:a] + V[::-1][:b]  
            t.sort(reverse=True)

            for _ in range(K-a-b):
                if len(t) > 0 and t[-1] < 0:
                    t.pop()
                    continue
                break
            ans = max(ans, sum(t))

    print(ans)



if __name__ == "__main__":
    solve()

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
from collections import deque
res=0
for i in range(min(N+1,K+1)):
    for j in range(min(N+1,K+1)-i):
        cur = list()
        v = deque()
        for f in V:
          v.append(-f)
        ans=0
        for p in range(i):
            cur.append(v.popleft())
        for q in range(j):
            cur.append(v.pop())
        cur=sorted(cur,reverse=True)
        
        for n in range(min(K-i-j,len(cur))):
            if cur[n]>0:
                cur[n]=0
            else:
                break
        res = max(-1*sum(cur),res)
print(res)

Solution (Python):

import sys
import bisect

inf = float('inf')

def solve(N: int, K: int, V: "List[int]"):
    res = -inf
    for k in range(min(K+1, N+1)):
        for left in range(k+1):
            right = k - left
            L = V[:left] + V[N-right:]
            L.sort()
            i = min(bisect.bisect(L, 0), K - k)
            res = max(res, sum(L[i:]))
    print(res)

    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()))
r = min(N, K)

ans = 0
for a in range(r+1):
    for b in range(r+1):
        if r < a+b:
            continue
        t = V[:a] + V[::-1][:b]
        t.sort(reverse=True)
        for _ in range(K-a-b):
            if len(t) > 0 and t[-1] < 0:
                t.pop()
        if ans < sum(t):
            ans = sum(t)
print(ans)

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

ans = 0
m = min(n, k)
for i in range(m + 1):
    for j in range(m - i + 1):
        t = v[:i] + v[n - j:]
        t.sort()
        d = tuple(filter(lambda x: x < 0, t))[:k - i - j]
        ans = max(ans, sum(t) - sum(d))
print(ans)

Solution (Python):
import sys


def val(l, r, d, Vs):
    jewels = Vs[:l] + list(reversed(Vs))[:r]
    return sum(sorted(jewels)[d:])

def cond(l, r, d, k, N):
    return l + r >= 0 and l + r <= min([k, N]) and d <= l + r


def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    ans = 0
    for k in range(K+1):
        for l in range(min([k+1, N+1])):
            for r in range(min([k+1, N+1])):
                d = k - l - r
                if cond(l, r, d, k, N):
                    ans = max(ans, val(l, r, d, Vs))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for lk in range(min(K, N)+1):
    for rk in range(min(K, N)+1-lk):
        k = K - lk - rk
        v = V[:lk] + V[N-rk:]
        v.sort()
        ans = max(sum(v[k:]) + sum([x for x in v[:k] if x > 0]), ans)
if K >= 2 * N:
    ans = sum([v for v in V if v >= 0])
print(ans)

Solution (Python):
from sys import stdin
import numpy as np

N, K = map(int,stdin.readline().rstrip().split())
VList = list(map(int,stdin.readline().rstrip().split()))
VList=np.array(VList)

maxNum=0
tryNum = min(N,K)
for i in range(tryNum+1):
  for j in range(tryNum+1-i):
    getList=VList[0:i]
    getList=np.append(getList,VList[N-j:N])
    getList.sort()
    throwNum=min(K-(i+j), len(getList))
    counter=0
    for k in range(throwNum):
      if getList[k] < 0:
        counter+=1
    getList=getList[counter:len(getList)]
    maxNum=max(maxNum, sum(getList))
print(maxNum)

Solution (Python):
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0
ans = 0

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,upper+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        
        up = min(k-upper,i)
        for l in range(up):
            if a[l] < 0:
                v -= a[l]
            else:
                break
        if v > ans:
            ans = v

print(ans)

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

def main():
	n, k = map(int, input().strip().split())
	v = deque(map(int, input().strip().split()))
	ans = 0
	for i in range(k+1):
		for j in range(k - i + 1):
			now = 0
			s = []
			if i + j > n:
				continue
			md = k - i - j
			for t in range(i):
				s.append(v[t])
				now += v[t]
			for t in range(j):
				s.append(v[n-1-t])
				now += v[n-1-t]
			s.sort()
			
			
			
			
			
			
			for t in range(md):
				if t >= len(s):
					break
				if s[t] >= 0:
					break
				now -= s[t]
			ans = max(now, ans)

	print(ans)


if __name__ == '__main__':
	main()

Solution (Python):

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





ans=0
for i in range(1,k+1):
    if i<=n:
        for j in range(i+1):
            tmp=[]
            
            for l in range(j):
                if l<n:
                    tmp.append(a[l])
            for r in range(i-j):
                if n-1-r>=0:
                    tmp.append(a[-1-r])
            
            tmp.sort(reverse=True)
            
            for m in range(k-i):
                if len(tmp)>0:
                    if tmp[-1]<0:
                        tmp.pop()
            ans=max(ans,sum(tmp))

print(ans)

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
V_=list(reversed(V))
ans=0
for k in range(1,K+1):
    k_=min(k,N)
    for kk in range(k_+1):
        sum=0
        l=[]
        for i in range(kk):
            sum+=V[i]
            l.append(V[i])
        for i in range(k_-kk):
            sum+=V_[i]
            l.append(V_[i])
        l.sort()
        for j in range(min(k_,K-k_)):
            if l[j]>=0:
                break
            sum-=l[j]
        ans=max(ans,sum)
print(ans)

Solution (Python):
import itertools as it

n, k = map(int, input().split())
vs = [int(s) for s in input().split()]
vs_acc = [0] + list(it.accumulate(vs))
vs_total = vs_acc[-1]

ans = 0
for left in range(min(n, k) + 1):
    for right in range(min(n, k) - left + 1):
        cnt = k - (left + right)
        remaining = vs_acc[n - right] - vs_acc[left]
        subsum = vs_total - remaining

        rest = sorted(vs[:left] + vs[n - right :])[:cnt]
        for x in rest:
            if x < 0:
                subsum -= x
            else:
                break
        ans = max(ans, subsum)

print(ans)

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    N, K = i2nn()
    V = i2nn()
    n = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1-i, K+1-i)):
            k = K - i - j
            w = V[0:i] + V[N-j:N]
            w.sort()
            t = 0
            for u in w:
                if u < 0 and k > 0:
                    k -= 1
                    continue
                t += u
            if n < t:
                n = t
    print(n)

main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

    res = 0
    for k in range(1, K+1):
        if lenV < k: break
        for rk in range(k+1):
            pick_list = V[:k-rk]
            if 0 < rk:
                pick_list.extend(V[-rk:])
            pick_list.sort()

            
            not_neg_start_index = 0
            for i, num in enumerate(pick_list):
                if num < 0: continue
                else:
                    not_neg_start_index = i
                    break
            pos_sum = sum(pick_list[i:])
            neg_list = pick_list[:i]
            len_neg_list = len(neg_list)
            if K-k < len_neg_list:
                res = max(res, sum(neg_list[K-k:])+pos_sum)
            else:
                res = max(res, pos_sum)
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

res = 0
for k in range(1, K+1):
    if lenV < k: break
    for rk in range(k+1):
        pick_list = V[:k-rk]
        if 0 < rk:
            pick_list.extend(V[-rk:])
        pick_list.sort()

        
        not_neg_start_index = 0
        for i, num in enumerate(pick_list):
            if num < 0: continue
            else:
                not_neg_start_index = i
                break
        pos_sum = sum(pick_list[i:])
        neg_list = pick_list[:i]
        len_neg_list = len(neg_list)
        if K-k < len_neg_list:
            res = max(res, sum(neg_list[K-k:])+pos_sum)
        else:
            res = max(res, pos_sum)

print(res)

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

nk = min(N, K)

ans = -1
for a in range(nk+1):
    for b in range(nk+1-a):
        have = v[:a] + v[N-b:]
        have = sorted(have)
        i = 0
        while i < K - a - b and i < a + b:
            if have[i] < 0:
                have[i] = 0
            else:
                break
            i += 1
        ans = max(ans, sum(have))

print(ans)

Solution (Python):




def main():
    
    def dbg(*something):
        if DEBUG:
            print(*something)


    
    
    from sys import stdin
    from math import ceil,floor
    DEBUG = False
    N,K = list(map(int,stdin.readline().split()))
    V = list(map(int,stdin.readline().split()))
    
    max_value = 0
    dbg([i for i in range(3,-1,-1)])
    for getnum in range(1,min(N+1,K+1)):  
        for a in range(getnum + 1):  
            J = [0] * getnum 
            b = getnum - a 
            for i in range(a):
                J[i] = V[i]
            for i in range(b):
                J[a+i] = V[-i-1]
            J.sort()
            
            t = min(K-getnum,getnum) 
           
            for i in range(t): 
                if J[i] >= 0:
                    t = i
                    break
            
            tmp_max = sum(J[t:])

            max_value = max(max_value,tmp_max)
    print(max_value)



if __name__ == '__main__':
    main()

Solution (Python):
def main():
	N, K = map(int, input().split())
	V = list(map(int, input().split()))
	pick_num = min(N, K)
	INF = 10**9+7
	dp = [-INF]*(N+K+1)
	dp[0] = 0
	dp[1] = max(V[0], V[-1])
	for i in range(2, pick_num+1):
		for j in range(i+1):
			l = V[:j]
			r = V[-i+j:] if i != j else []
			tmp = sum(l+r)
			dp[i] = max(dp[i], tmp)
			this_list = list(l+r)
			this_list.sort()
			for trash in range(K-i+1):
				dp[i+trash] = max(dp[i+trash], tmp-sum(this_list[:trash]))
	ans = dp[0]
	for i in range(1, K+1):
		ans = max(ans, dp[i])
	print(ans)


if __name__ == '__main__':
	main()

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
R = min(N,K)

ans = 0
for a in range(R):
    for b in range(R):
        if a+b > R: break
        tmpV = sorted(V[:a]+rV[:b])
        if R == 1: tmpV = [max(V[0],rV[0])]
        t = sum(tmpV)
        for i in range(min(len(tmpV),K-(a+b))):
            if tmpV[i] >= 0: break
            else: t -= tmpV[i]
        if ans < t: ans = t
print(ans)

Solution (Python):
n,k=[int(i) for i in input().split()]
v=[int(i) for i in input().split()]
res=0
for right in range(k+1):
    for left in range(min(n,k)-right+1):
        ans=[]
        ans+=v[:right]
        ans+=v[n-left:]
        ans.sort()
        for i in range(min(k-right-left,len(ans))+1):
            res=max(res,sum(ans[i:]))
print(res)

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

ans = 0
for pull_num in range(K+1):
    if pull_num > N:
        continue
    push_num = K - pull_num
    for left_num in range(pull_num+1):
        right_num = pull_num - left_num
        left_D = D[:left_num];  right_D = D[N-right_num:]
        temochi = left_D + right_D
        temochi.sort()
        index = bisect.bisect_left(temochi, 0)
        if push_num >= index:
            ans = max(ans, sum(temochi[index:]))
        else:
            ans = max(ans, sum(temochi[push_num:]))
print(ans)

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

ans=0
for nab in range(1,min(N+1,K+1)):
    nr=K-nab
    for na in range(nab+1):
        nb=nab-na
        vt=sorted(V[:na]+V[N-nb:])
        for ncd in range(min(nr+1,nab+1)):
            np=sum(vt[ncd:])
            ans=max(ans,np)

print(ans)

Solution (Python):
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=[]
for i in range(k+1):
  x=v[0:i]
  for j in range(k-i+1):
    if i<=n-j:
      y=v[n-j:n]
    else:
      y=v[i:n]
    L=x+y
    L.sort()
    ct=0
    ct2=0
    for m in range(len(L)):
      if L[m]>=0:
        ct+=L[m]
      else:
        if ct2<k-i-j:
          ct2+=1
        else:
          ct+=L[m]
    ans.append(ct)
print(max(ans))

Solution (Python):
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
ans=0
chk=[0,0]
c=[]
for x in range(min(n,k)+1):
    if x>0:
        chk[0]+=v[x-1]
        if v[x-1]<0:
            c.append(v[x-1])
    ans=max(ans,chk[0])
    d=[]
    chk[1]=0
    for y in range(min(n,k)+1):
        if x+y>=k or x+y>=n:
            break
        if y>0:
            chk[1]+=v[-y]
            if v[-y]<0:
                d.append(v[-y])
        ans=max(ans,chk[0]+chk[1])
        e=c+d
        e.sort(reverse=True)
        t=0
        for z in range(k):
            if x+y+z>k or len(e)==0:
                break
            if z>0:
                t+=-(e[-1])
                e.pop()
                ans=max(ans,chk[0]+chk[1]+t)
print(ans)