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 collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)


def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if not lstop:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)

        if not rstop:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, True, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) > 0:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, True, True))
        V.popleft()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

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

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)


def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, -1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) > 0:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, -1))
        V.popleft()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Solution (Python):
n,k=map(int,input().split())
*v,=map(int,input().split())
motimono=[]
ans=[]
if k>=n:
    k-=n
    ans.append(sum(v))
    v.sort()
    for i in range(min(k,len(v))):
        v[i]=0
        ans.append(sum(v))
    print(max(ans))
else:
    v*=2
    for j in range(1,k+1):
        for i in range(n-j,n+1):
            motimono=v[i:i+j]
            ans.append(sum(motimono))
            motimono.sort()
            for i in range(min(k-j,len(motimono))):
                motimono[i]=0
                ans.append(sum(motimono))
    print(max(ans))

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

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

ans = 0
D = V_array + V_array


for pop_count in range(K, 0, -1):
    if pop_count > N:
        continue
    append_count = K - pop_count
    for pop_index in range(N - pop_count, N + 1, 1):
        popped_object = D[pop_index:pop_index + pop_count]
        popped_object.sort()
        popped_sum = sum(popped_object)
        for i in range(min(pop_count, append_count)):
            if popped_object[i] < 0:
                popped_sum -= popped_object[i]
            else:
                break
        ans = max(ans, popped_sum)

print(ans)

Solution (Python):
ans=0

n,k=map(int,input().split())
v=[int(i) for i in input().split()]
b=sum(v)
f=[0]*(n+1)
ne=[i for i in v if i<0]
acn=[0]*(1+len(ne))
pre=0
for i in range(n):
    f[i+1]=f[i]+v[i]
    if i<len(ne):
        acn[i+1]=acn[i]+ne[i]

for lp in range(k+1):
    for rp in range(k+1):
        for back in range(lp+rp+1):
            if lp+rp+back>k or lp+rp>n:
                break
            tmp=v[:lp]+v[len(v)-rp:]
            tmp.sort(reverse=True)
            ans=max(ans,sum(tmp[:len(tmp)-back])) 
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
v_list = list(map(int, input().split()))
ans = 0
for i in range(1, min(n,k)+1):
    for j in range(i+1):
        cnt = k - i
        tmp_list = []
        if j == 0:
            tmp_list += v_list[-i:]
        elif j == i:
            tmp_list += v_list[:i]
        else:
            tmp_list += v_list[:j]
            tmp_list += v_list[-(i-j):]
        tmp_list.sort()
        tmp_sum = sum(tmp_list)
        for tmp in tmp_list:
            if cnt <= 0:
                break
            if tmp < 0:
                tmp_sum -= tmp
                cnt -= 1
            else:
                break
        ans = max(ans, tmp_sum)
print(ans)

Solution (Python):
from bisect import bisect
n,K = (int(i) for i in input().split())
v = [int(i) for i in input().split()]
ans = 0
for i in range(min(n,K)+1):
    for j in range(i+1):
        z = []
        for k in range(j): z.append(v[k])
        for k2 in range(i-j): z.append(v[-k2-1])
        z.sort()
        ans = max(ans,sum(z[min(bisect(z,0),K-i):]))
print(ans)

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

ans = 0

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

        ans = max(ans, sum(s) - m)

print(ans)

Solution (Python):




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

ans = 0

for l in range(K + 1):
    
    for r in range(K - l + 1):
        
        if l + r > N:
            continue
        
        d = max(0, K - (l + r))

        
        s = sum(V[:l]) + sum(V[N - r:])
        
        neg = filter(lambda x: x < 0, V[:l] + V[N - r:])
        neg = list(neg)
        neg.sort()
        for i in range(d):
            if i < len(neg):
                s -= neg[i]

        
        ans = max(ans,s)

print(ans)

Solution (Python):
import itertools
import numpy as np
import copy

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

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

	allCandidates = []
	for i in range(K + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		vs = copy.deepcopy(Vs)
		exceptFlag = False
		for k in candidate:
			if k == 0:
				try:
					have.append(vs[0])
					del vs[0]
				except:
					exceptFlag = True
					break
			if k == 1:
				try:
					have.append(vs[-1])
					del vs[-1]
				except:
					exceptFlag = True
					break
			if k == -1:
				try:
					if have[np.argmin(have)] < 0:
						del have[np.argmin(have)]
				except:
					exceptFlag = True
					break
		if not exceptFlag:
			score = sum(have)
			if score > maxScore:
				maxScore = score

	print(maxScore)

if __name__ == '__main__':
	main()

Solution (Python):
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
r=min(n,k)
ans=0
for a in range(r+1):
    for b in range(r-a+1):
        if b>=1:
            q=v[:a]+v[-b:]
        else:
            q=v[:a]
        t=k-a-b
        m=0
        for i in sorted(q):
            if i<0 and t>0:
                m+=abs(i)
                t-=1
        ans=max(ans,m+sum(q))
print(ans)

Solution (Python):
def solve(k, koho, cho=[], syo=0):
    if syo == 2:
        lenc=len(cho)
        for i in range(min(k,lenc)):
            mn = min(cho)
            if mn < 0:
                cho.remove(mn)
            else:
                break
        return sum(cho)
    if syo == 1:
        koho = koho[::-1]
    re = []
    lenk = len(koho)
    for i in range(min(k, lenk)+1):
        re.append(solve(k - i, koho[i:], cho+koho[:i], syo + 1))
    return max(re)

n, k = map(int, input().split())
vs = list(map(int, input().split()))
print(solve(k, vs))

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

rich = 0

for k in range(1, min(K+1, N+1)):
    for rot in range(k+1):
        jewels = (V[-rot:] + V[:-rot])[:k]
        jewels.sort()
        neg = [i < 0 for i in jewels].count(True)
        
        for ret in range(min(neg+1,K-k+1)):
            rich = max(sum(jewels[ret:]), rich)

print(rich)

Solution (Python):


from sys import stdin

input = stdin.readline

N, K = list(map(int, input().split()))
VList = list(map(int, input().split()))
sumList = []

for i in range(min([K,N])+1):
    for j in range(min([K,N])-i+1):
        k = K -i -j
        myHand = [VList[n] for n in range(i)] + [VList[-m] for m in range(1,j+1)] + [0]
        myHand.sort(reverse=True)
        for _ in range(k):
            if myHand[-1] < 0:
                myHand.pop()
            else:
                break
        sumList.append(sum(myHand))


print(max(sumList))

Solution (Python):
def value(k1,k2,k,D):
    if k2 == 0:
        v0 = D[:k1] + [0]
    else:
        v0 = D[:k1] + D[-(k2):] + [0]
    v0.sort()  
    n_neg = v0.index(0)  
    k0 = k - (k1+k2)  
    if n_neg <= k0:
        ans = sum(v0[n_neg:])
    else:
        ans = sum(v0[k0:])
    
    return ans

n,k = map(int,input().split(' '))
D = list(map(int,input().split(' ')))
score = 0
k1 = 0
while k1 < min(k+1,n+1):
    k2 = 0
    while k2 + k1 < min(k+1,n+1):
        v = value(k1,k2,k,D)
        if v > score:
            score = v
            
            
        k2 += 1
    k1 += 1
print(score)

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

ans = 0
for k in range(1,K+1):
    for i in range(k+1):
        R = V[-(k-i):]
        if len(R) == N:
            R = []
        
        L = V[:i]+R+[0]
        L.sort()
        zero_index = min(L.index(0),KK-k)
        res = sum(L[zero_index:])
        ans = max(ans,res)
print(ans)

Solution (Python):
import heapq
N, K = map( int, input().split())
V = list(  map( int, input().split()))
ans = 0
for i in range(N+1):
    for j in range(N+1):
        if i + j > K or i + j > N:
            break
        cnt = i+j
        if j == 0:
            q = V[:i]
            now = sum(q)
            heapq.heapify(q)
            while q and (cnt < K):
                t = heapq.heappop(q)
                if t >= 0:
                    break
                else:
                    now += -t
                    cnt += 1
        else:
            q = V[:i] + V[-j:]
            now = sum(q)
            heapq.heapify(q)
            while q and (cnt < K):
                t = heapq.heappop(q)
                if t >= 0:
                    break
                else:
                    now += -t
                    cnt += 1
        if ans < now:
            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(N+1):
    for j in range(N+1):
        if i + j > K or i + j > N:
            break
        cnt = i+j
        if j == 0:
            q = V[:i]
            now = sum(q)
            heapq.heapify(q)
            while q and (cnt < K):
                t = heapq.heappop(q)
                if t >= 0:
                    break
                else:
                    now += -t
                    cnt += 1
        else:
            q = V[:i] + V[-j:]
            now = sum(q)
            heapq.heapify(q)
            while q and (cnt < K):
                t = heapq.heappop(q)
                if t >= 0:
                    break
                else:
                    now += -t
                    cnt += 1
        if ans < now:
            ans = now
print( ans)

Solution (Python):
from collections import deque

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

numofget=min(N,K)
tmp=[]
for i in range(numofget+1):
    A,B=i,numofget-i
    for j in range(B+1):
        q=deque(V)
        bag=[]
        for a in range(A):
            bag.append(q.popleft())
        for b in range(j):
            bag.append(q.pop())
        for _ in range(K-(A+j)):
            if len(bag)>0 and min(bag)<0:
                bag.remove(min(bag))
        tmp.append(sum(bag))
print(max(tmp))

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


main()

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

ans = 0
for num_a in range(min(N,K) + 1):
    for num_b in range(min(N-num_a,K-num_a) + 1):
        if num_b >= 1:
            temp_v = V[:num_a] + V[-num_b:]
        else:
            temp_v = V[:num_a]
        t = K - num_a - num_b
        s = 0
        for vv in sorted(temp_v):
            if vv < 0 and t > 0:
                s += -vv
                t -= 1
        ans = max(ans, sum(temp_v) + s)
        
print(ans)

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

maxsum = -10**13
for i in range(min(n+1,k+1)):
    for j in range(min(n+1-i,k+1-i)):
        su = (V[:i] + V[:n-j-1:-1])
        su.sort()
        sumsu = sum(su)
        
        if k-i-j > 0:
            for l in range(k-i-j):
                if len(su) > l and su[l] < 0: sumsu -= su[l]
        maxsum = sumsu if maxsum < sumsu else maxsum
print(maxsum)

Solution (Python):
import heapq
from collections import deque
n,k = map(int,input().split())
v=list(map(int,input().split()))

res = 0
for i in range(k+1):
    for j in range(k+1-i):
        d = deque(v)
        num = 0
        a = []

        cnt = 0
        while cnt<i and len(d):
            x = d.popleft()
            heapq.heappush(a,x)
            num += x
            cnt += 1
        cnt = 0
        while cnt<j and len(d):
            x = d.pop()
            heapq.heappush(a,x)
            num += x
            cnt += 1

        sub = k-i-j
        while sub > 0 and len(a):
            y = heapq.heappop(a)
            if y <= 0:
                num -= y
            sub -= 1
        
        res = max(res,num)
print(res)

Solution (Python):
from collections import deque

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

ans = 0 
for l in range(K+1):
    for r in range(K+1-l):
        if l+r > N: break
        ll_max = K+1-l-r
        for ll in range(ll_max):
            rr = K-l-r-ll
            if not all([0<=val<=N for val in [l,r]]):
                break
            
            if ll>N: ll=N
            if rr>N: rr=N
            l_vs = sorted(vs[:l])
            r_vs = sorted(vs[N-r:])
            ans = max(ans, sum(l_vs)+ sum(r_vs))

            l_remain = l_vs[ll:]
            r_remain = r_vs[rr:]
            
            
            ans = max(ans, sum(l_remain) + sum(r_remain))

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-A+1):
        C=K-(A+B)
        have=[]
        have.extend(v[:A])
        have.extend(v[N-B:])
        have.sort()
        for c in range(C):
            if len(have)>0:
                if have[0]<0:
                    have.pop(0)
                else:
                    break
        ans=max(ans,sum(have))

print(ans)

Solution (Python):
N, K = 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-A+1):
    C = K-(A+B)
    have = []
    have.extend(v[:A])
    have.extend(v[N-B:])
    have.sort()
    for c in range(C):
      if len(have) > 0:
        if have[0] < 0:
          have.pop(0)
        else:
          break
    ans = max(ans, sum(have))

print(ans)

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

ans = 0
cnt = 0
for i in range(n+1):
    for j in range(n+1):
        if i + j > n or i + j > k:
            continue
        if j == 0:
            a = v[:i]
        else:
            a = v[:i] + v[-j:]
        tmp = sum(a)
        a.sort()
        for x in range(min([k-(i+j), i+j])):
            if a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])
        cnt += 1

print(ans)

Solution (Python):
def take(n, lis):
    if n >= len(lis):
        return lis
    if n <= 0:
        return []
    else:
        return lis[:n]


n, k = list(map(int, input().split(" ")))
vs = list(map(int, input().split(" ")))
if n == 1:
    print(max(0, vs[0]))
    exit()
if k == 1:
    print(max(0, vs[0], vs[-1]))
    exit()
scores = []
score = 0
length = min(n, k)
for gets in range(length + 1):
    for left in range(gets + 1):
        right = gets - left
        retnum = k - gets
        get_list = take(left, vs) + take(right, vs[::-1])
        minus = sorted([get for get in get_list if get <= 0])
        score = sum(get_list) - sum(take(retnum, minus))
        scores.append(score)
print(max(0, *scores))

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



ans=0
for L in range(min(n,k)+1):
    for R in range(min(n,k)-L+1):
        v_get=v[0:L]+v[n-R:n]
        v_get.sort()
        D=0
        for i in v_get:
            if D==k-L-R:
                break
            if i>=0:
                break
            D+=1
        if D==0:
            total=sum(v_get)
        else:
            total=sum(v_get[D:])
        if total>ans:
            ans=total
print(ans)

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

ans = 0

for left in range(K + 1):
    for right in range(K - left + 1):
        if left + right > N:
            break
        items = list(D[0:left])
        items.extend(D[::-1][0:right])
        dis = K - (left + right)
        i = 0
        items = sorted(items, reverse=True)
        while True:
            ans = max(ans, sum(items))
            if i < dis and len(items) > 0:
                i += 1
                items.pop()
                continue
            else:
                break

print(ans)

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

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

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

maxans = 0
for l in range(K+1):
    for r in range(K-l+1):
        d = K - l- r
        if l+r > N:
            break
        hand = []
        hand.extend(V[:l])
        if r != 0:
            hand.extend(V[-r:])
        hand.sort()
        ans = sum(hand)

        for i in range(d):
            if len(hand) <= i+1:
                break
            if hand[i] < 0:
                ans -= hand[i]
        
        if ans > maxans:
            maxans = ans

print(maxans)

Solution (Python):
n,k,*v=map(int,open(0).read().split());print(max(sum(v[:i-j]+v[n-j:])-sum(sorted(x*(x<0)for x in v[:i-j]+v[n-j:])[:k-i])for i in range(min(n,k)+1)for j in range(i)))

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

for n_get in range(1, min(N, K)+1):
    for a in range(n_get+1):
        b = n_get - a
        temp = V[:a] + V[N-b:]
        temp = sorted(temp)
        rem = max(0, K - n_get)
        i = 0
        while rem > 0 and i < n_get:
            if temp[i] >= 0:
                break
            rem -= 1
            i += 1
        ans = max(ans, sum(temp[i:]))

print(ans)

Solution (Python):
N,K=list(map(int,input().split()))
V=list(map(int,input().split()))
max_score=0
for k in range(min(N,K)+1):
    for k_l in range(k+1):
        left=V[:k_l]
        k_r = k-k_l
        right=V[-k_r:]if k_r>0 else []
        sterable=sorted(left+right)[:K-k]
        stesum=0
        for ss in sterable:
            if ss<0:
                stesum+=ss
            else:break
        score = sum(left)+sum(right) - stesum
        max_score=max(score,max_score)
print(max_score)

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

for i in range(min(n, k) + 1):
  for j in range(min(i + 1, k - i + 1)):
    for m in range(i + 1):
      out = list(v[0:m]) + list(v[:-(i-m+1):-1])
      out.sort()
      best = max(best, sum(out[j::]))
 
print(best)

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

max_price = 0
for pick in range(1, min(K, N) + 1):
    for left_pick_num in range(pick + 1):
        right_pick_num = max(pick - left_pick_num, 0)
        if not left_pick_num and not right_pick_num: continue

        picked = gems[:left_pick_num]
        if right_pick_num: picked += gems[-right_pick_num:]


        picked.sort()
        num_throw = min(pick, K - pick)
        j = 0
        while j < num_throw and picked[j] < 0:
            j += 1

        max_price = max(max_price, sum(picked[j:]))
print(max_price)

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
res = 0
for i in range(1,min(N+1,K+1)): 
  for j in range(i+1):
    have = []
    trash_value = 0
    a = j
    b = i - j
    have.extend(V[:a])
    if not b ==0:
      have.extend(V[((-1)*b):])
    have.sort()
    trash = K - i 
    for p in range(min(trash,len(have))):
      if have[p] < 0:
        trash_value += have[p]
      else:
        break
    if res < (sum(have)-trash_value):
      res = (sum(have)-trash_value)
print(res)

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

for a in range(lim+1):
    for b in range(lim-a+1):
        hand = sorted(v[:a]+v[n-b:])
        hs = sum(hand)
        for i in range(min(k-a-b, a+b)):
            test = hand[i]
            if test < 0:
                hs -= test
        ans.append(hs)
print(max(ans))

Solution (Python):





from sys import stdin
import itertools
import time




import numpy as np

temp = stdin.readline().split()
N = int(temp[0])
K = int(temp[1])

data = []
write = stdin.readline().split()
for a in write:
    data.append(int(a))

steps = min(N, K)




An = range(K+1)

max = 0

for A in An:
    for B in range(steps-A+1):       
        small = K - A - B

        left = data[0:A]
        right = []
        if not B==0:
            right = data[-B:]
        left.extend(right)
        left = sorted(left)

        
        for k in range(small):

            try:
                if left[k] <= 0:
                    left[k] = 0
            except:
                continue
        
        add = sum(left)

        if max < add:
            max = add
print(max)

Solution (Python):
N, K = map(int, input().split())
Vs = list(map(int, input().split()))
max_v = 0
R = min(N, K)
for r in range(R+1):
  for a in range(r+1):
    b = r - a
    inh = sorted(Vs[:a] + Vs[N-b:])
    for d in range(min(r, K-r)+1):
      max_v = max(max_v, sum(inh[d:]))

print(max_v)

Solution (Python):
import sys
from collections import deque
import copy
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsm = lambda: map(str, sys.stdin.readline().split())

n, k = nm()
v = nl()
d = deque(v)
ans = 0
for m in range(k + 1):
    for p in range(m + 1):
        lis = copy.copy(d)
        neg = []
        summ = 0
        for i in range(p):
            if len(lis) == 0:
                break
            l = lis.popleft()
            summ += l
            if l < 0:
                neg.append(l)
        for j in range(m - p):
            if len(lis) == 0:
                break
            l = lis.pop()
            summ += l
            if l < 0:
                neg.append(l)
        neg.sort()
        for o in range(k - m):
            if o < len(neg):
                summ -= neg[o]
            else:
                break
        ans = max(ans, summ)
print(ans)

Solution (Python):

n,k = map(int, input().split())
v = [int(z) for z in input().split()]
ans = []
m = min(n,k)

for a in range(0,min(n,k)+1):
    for b in range(0,min(n,k)-a+1):
        vm = []
        cd = k-a-b
        vm.extend(v[0:a])
        vm.extend(v[n-b:n])
        vm.sort()
        for i in range(0,min(cd,len(vm))):
            if vm[i] < 0:
                vm[i] = 0
        ans.append(sum(vm))

print(max(ans))

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

dp = [[0 for _ in range(N+1)]for _ in range(N+1)]
ans = -10**10
for i in range(N+1):
    for j in range(N+1):
        if(i+j>min(K,N)): continue
        li = []
        if(j!=0):
            li = V[:i]+V[-j:]
        else:
            li = V[:i]
        cnt = 0
        li.sort()
        for x in li:
            if(x<0):
                cnt+=1
            else:
                break
        if(min(cnt,K-(i+j))<0):
            print('aaa')
        dp[i][j] = sum(li[min(cnt,K-(i+j)):])
        ans = max(ans,sum(li[min(cnt,K-(i+j)):]))

print(ans)

Solution (Python):
import sys

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')
N, K = list(map(int, input().split()))
V = np.array(list(map(int, input().split())))
Vminus = np.minimum(V, 0)

ans = 0
for take in range(min(N, K) + 1):
    discards = min(K - take, take)
    for t in range(take + 1):
        v = sum(V[:take - t]) + sum(V[N - t:])
        minuses = list(sorted(np.concatenate([Vminus[:take - t], Vminus[N - t:]])))
        ans = max(ans, v - sum(minuses[:discards]))
print(ans)

Solution (Python):
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]

def main():
    デックの長さ, 操作可能回数 = LI()
    デック = LI()

    最大値 = 0
    for 取る数 in range(min(操作可能回数+1, デックの長さ+1)):
        捨てることができる数 = 操作可能回数 - 取る数
        for 左から取る数 in range(取る数+1):
            右から取る数 = 取る数 - 左から取る数
            取ったリスト = デック[:左から取る数] + デック[デックの長さ-右から取る数:]
            取ったリスト = sorted(取ったリスト)
            for _ in range(捨てることができる数):
                if 取ったリスト and 取ったリスト[0] < 0:
                    取ったリスト.pop(0)
                else:
                    break
            最大値 = max(最大値, sum(取ったリスト))

    print(最大値)

main()

Solution (Python):

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



ans = max(
    sum(sorted(V[:a] + V[(N - b):])[c:])
    for a in range(min(N, K) + 1)
    for b in range(min(N, K) - a + 1)
    for c in range(min(K - a - b, a + b) + 1)
)


print(ans)

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(min(n, k) + 1 - i):
        if i == 0 and j == 0:
            continue
        if j == 0:
            vv = v[:i]
        else:
            vv = v[:i] + v[-j:]
        num = sum(vv)
        vv.sort()
        for l in range(min(len(vv), k - i - j)):
            if vv[l] < 0:
                num -= vv[l]
            else:
                break
        ans = max(ans, num)
print(ans)

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


motimono = []
ans = []

if K >= N: 
    K -= N
    heapq.heapify(V)
    ans.append(sum(V))
    for i in range(K):
        if len(V)>0:
            heapq.heappop(V)
        ans.append(sum(V))
    print(max(ans))

else:
    V *= 2 
    
    for j in range(1,K+1): 
        for i in range(N-j, N+1): 
            motimono = V[i:i+j]
            
            ans.append(sum(motimono))
            
            heapq.heapify(motimono)
            for i in range(K-j): 
                if len(motimono)>0:
                    heapq.heappop(motimono)
                ans.append(sum(motimono))
    print(max(ans))

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

ans = -pow(10, 9)
for left in range(0, K + 1):
  for right in range(0, K - left + 1):
    if left + right < N:
      all_get = V[:left] + V[::-1][:right]
    else:
      all_get = V[:]
    all_get.sort(reverse=True)
    num = 0
    while True:
      if num >= K - (left + right) or len(all_get) == 0:
        break
      if all_get[-1] >= 0:
        break
      all_get.pop(-1)
      num += 1
    now = sum(all_get)
    ans = max(ans, now)
print(ans)

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




INT_MIN = -1 * (10**10)

ans = INT_MIN

NK = min(N, K)
for left in range(0,NK+1):
    for right in range(0, NK+1-left):
        
        
        
        
        rest_ope = K-left-right
        
        my_v = V[:left]+V[N-right:]
        sum_v = 0
        for i, x in enumerate(sorted(my_v)):
            if x < 0 and i < rest_ope:
                continue
            else:
                sum_v += x
        
        ans = max(ans, sum_v)
        
print(ans)