Problem:
 Score : 400 points    Problem Statement Your friend gave you a dequeue D as a birthday present.  D is a horizontal cylinder that contains a row of N jewels.  The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.  In the beginning, you have no jewel in your hands.  You can perform at most K operations on D , chosen from the following, at most K times (possibly zero):    Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.    Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.    Operation C: Choose a jewel in your hands and insert it to the left end of D . You cannot do this operation when you have no jewel in your hand.    Operation D: Choose a jewel in your hands and insert it to the right end of D . You cannot do this operation when you have no jewel in your hand.    Find the maximum possible sum of the values of jewels in your hands after the operations.      Constraints  All values in input are integers.  1 \leq N \leq 50  1 \leq K \leq 100  -10^7 \leq V_i \leq 10^7         Input Input is given from Standard Input in the following format:  N  K  V_1  V_2  ...  V_N       Output Print the maximum possible sum of the values of jewels in your hands after the operations.        Sample Input 1 6 4
-10 8 2 1 2 6      Sample Output 1 14  After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14 , which is the maximum result.   Do operation A. You take out the jewel of value -10 from the left end of D .  Do operation B. You take out the jewel of value 6 from the right end of D .  Do operation A. You take out the jewel of value 8 from the left end of D .  Do operation D. You insert the jewel of value -10 to the right end of D .        Sample Input 2 6 4
-6 -100 50 -2 -5 -3      Sample Output 2 44       Sample Input 3 6 3
-6 -100 50 -2 -5 -3      Sample Output 3 0  It is optimal to do no operation.

Solution (Python):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N, K = map(int, line_input())
    V = [int(v) for v in line_input()]
    maxpoint = 0
    for i in range(N + 1):
        for j in range(N - i + 1):
            if i + j <= min(N, K):
                trash = []
                heapq.heapify(trash)
                point = sum(V[:i])
                for k in range(j):
                    point += V[-1 - k]
                    heapq.heappush(trash, V[-1-k])
                for a in range(i):
                    heapq.heappush(trash, V[a])
                count = 0
                while trash and count < K - (i + j):
                    bye = heapq.heappop(trash)
                    count += 1
                    if bye > 0: break
                    else: 
                        point -= bye
                maxpoint = max(maxpoint, point)
    print(maxpoint)
    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):
from collections import deque
N,K = map(int, raw_input().split())
V = map(int, raw_input().split())
q = deque([])
nl = []
for i in range(min(K+1,N+1)):
	for j in range(i+1):
		q.append(V[:j]+V[N-i+j:])
	while q:
		q2 = q.popleft()
		q2.sort()
		qlen = len(q2)
		np = sum(q2)
		for j in range(min(qlen,K-i)):
			if(q2[j] < 0):
				np -=q2[j]
		nl.append(np)
print(max(nl))

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 

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





ans = 0
for i in range(K): 
    for j in range(K-i+1):
        if j <= N-(K-i-j):
            ans_list = V[:j]+V[N-(K-i-j):]
            ans_list.sort()
            tmp = sum(ans_list)
            for k in range(min(len(ans_list),i)):
                if ans_list[k] < 0:
                    tmp -= ans_list[k]
            ans = max(ans,tmp)
        else:
            pass

print(max(ans,0))

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

vmax = 0
for l in range(0, K + 1):
	for r in range(0, K - l + 1):
		if N < l + r:
			break
		tv = V[:l] + V[N - r:]
		
		tv.sort()
		for n in range(min(len(tv), K - (l + r))):
			if tv[n] < 0:
				tv[n] = 0
		vmax = max(vmax, sum(tv))

print(vmax)

Solution (Python):

import sys
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    from itertools import accumulate
    import heapq
    N, K = MI()
    V = LI()
    left_cum = [0]+list(accumulate(V))
    right_cum = [0] + list(accumulate(V[::-1]))

    minus_left = [[0]*N]
    minus_right = [[0]*N]
    now_minuses = [0]*N

    for i, v in enumerate(V):
        heapq.heappush(now_minuses, v)
        minus_left.append(list(now_minuses))

    now_minuses = [0]*N
    for i in range(N)[::-1]:
        heapq.heappush(now_minuses, V[i])
        minus_right.append(list(now_minuses))

    ans = 0
    for left in range(N+1):
        for right in range(N+1):
            if left + right > N or left + right > K:
                break
            ans_ = left_cum[left] + right_cum[right]
            l, r = list(minus_left[left]), list(minus_right[right])
            for i in range(K - right - left):
                if l[0] > r[0]:
                    pop_item = heapq.heappop(r)
                else:
                    pop_item = heapq.heappop(l)
                if pop_item >= 0:
                    break
                else:
                    ans_ -= pop_item

            ans = max(ans, ans_)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    from itertools import accumulate
    import heapq
    N, K = MI()
    V = LI()
    left_cum = [0]+list(accumulate(V))
    right_cum = [0] + list(accumulate(V[::-1]))

    minus_left = [[0]*N]
    minus_right = [[0]*N]
    now_minuses = [0]*N

    for i, v in enumerate(V):
        heapq.heappush(now_minuses, v)
        minus_left.append(list(now_minuses))

    now_minuses = [0]*N
    for i in range(N)[::-1]:
        heapq.heappush(now_minuses, V[i])
        minus_right.append(list(now_minuses))

    
    

    ans = 0
    for left in range(N+1):
        for right in range(N+1):
            if left + right > N or left + right > K:
                break
            ans_ = left_cum[left] + right_cum[right]
            l, r = list(minus_left[left]), list(minus_right[right])
            for i in range(K - right - left):
                if l[0] > r[0]:
                    pop_item = heapq.heappop(r)
                else:
                    pop_item = heapq.heappop(l)
                if pop_item >= 0:
                    break
                else:
                    """print(left, right, left_cum[left],
                          right_cum[right], K, ans_, pop_item)"""
                    ans_ -= pop_item
            

            ans = max(ans, ans_)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
ans = 0
for i in range(1, min(n, k) + 1):
    for j in range(i):
        t = v[:i - j] + v[n - j:]
        ans = max(ans, sum(t) - sum(sorted(x for x in t if x < 0)[: k - i]))
print(ans)

Solution (Python):
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
ans = max(sum(v[:i - j] + v[n - j:]) -
          sum(sorted(x for x in v[:i - j] + v[n - j:] if x < 0)[: k - i])
          for i in range(1, min(n, k) + 1) for j in range(i))
print(ans)

Solution (Python):



def main():

    num_houseki, num_sousa = map(int, input().split())
    data = list(map(int, input().split()))

    ans = 0

    for left in range(min(num_houseki, num_sousa) + 1):
        for righ in range(min(num_sousa- left, num_houseki - left) + 1):
            data_left = data[:left]
            data_righ = data[num_houseki - righ:]
            data_now = data_left + data_righ
            data_now.sort()
            nokori = num_sousa - len(data_now)

            while nokori > 0:
                if len(data_now) == 0:
                    break
                if data_now[0] < 0:
                    data_now.pop(0)
                    nokori -= 1
                else:
                    break

            kari = sum(data_now)
            if kari > ans:
                ans = kari

    print(ans)





if __name__ == '__main__':
    main()

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

ans = 0

P = min(N,K)

for a in range(1,P+1) :
    for i in range(a+1) :
        cnt = 0
        calc = 0
        GET = V[:i] + V[N-(a-i):]
        GET = sorted(GET)
        
        for g in GET :
            if (g < 0) and (cnt < K-a) :
                cnt += 1
            else :
                calc += g
        
        ans = max(ans, calc)
print(ans)

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

R = min(N, K)
max_sum = 0
for A in range(R + 1):
        for B in range(R - A + 1):
                drop_max = min(K - (A + B), (A + B))
                if B == 0:
                        L = D[:A]
                else:
                        L = D[:A] + D[-B:]
                L.sort()
                drop_num = 0
                for i in range(A + B):
                        if drop_num < drop_max and L[i] < 0:
                                L[i] = 0
                                drop_num += 1
                        else:
                                break

                max_sum = max(max_sum, sum(L))

                

print(max_sum)

Solution (Python):



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

ans = 0
for left in range(k+1):
    for right in range(k-left+1):
        if left+right > n : continue
        for trash in range (k-left-right+1):
            x = sorted(v[0:left]+v[::-1][0:right])[trash:]
            ans = max(ans,sum(x))

print(ans)

Solution (Python):
N, K = map(int, input().split())
Vs = list(map(int, input().split()))
lV = [0]*(N+1)
rV = [0]*(N+1)
lm = [[] for _ in range(N+1)]
rm = [[] for _ in range(N+1)]
for i in range(1, N+1):
    lV[i] += Vs[i-1]+lV[i-1]
    rV[i] += Vs[-i] + rV[i-1]
    if Vs[i-1] < 0:
        for j in range(i, N+1):
            lm[j].append(Vs[i-1])
    if Vs[-i] < 0:
        for j in range(i, N+1):
            rm[j].append(Vs[-i])
ans = 0
for i in range(min(N+1, K+1)):
    for j in range(min(N+1-i, K+1-i)):
        for k in range(max(1, K+1-i-j)):
            SUM = lV[i]+rV[j]
            minus = lm[i]+rm[j]
            minus.sort()
            SUM -= sum(minus[:k])
            if ans < SUM:
                ans = SUM
print(ans)

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

maxValue = 0

for i in range(min(N,K)+1):
    for j in range(min(N,K)-i+1):
        left = D[:i]
        right = D[N-j:]
        merge = (left + right + [0])
        merge.sort()

        index = 0

        for k in range(K-i-j):
            if (merge[index] < 0):
                index += 1
            else:
                break

        val = sum(merge[index:])
        if(maxValue < val):
            maxValue = val

print(maxValue)

Solution (Python):
N, K = 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-L, K-L)+1):
        js = V[:L] + V[-R:] if R > 0 else V[:L]
        t = K - L - R
        s = 0
        for i in sorted(js):
            if i < 0 and t > 0:
                s += -i
                t -= 1
        ans = max(ans, sum(js) + s)
print(ans)

Solution (Python):



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

ans = []
for left in range(k+1):
    for right in range(k-left+1):
        if left+right > n : continue
        for trash in range (k-left-right+1):
            x = sorted(v[0:left]+v[::-1][0:right])[trash:]
            ans.append(sum(x))

print(max(ans))

Solution (Python):
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
ans = max(sum(v[:i - j] + v[n - j:]) -
          sum([x for x, m in a if m < i - j or m >= n - j][:k - i])
          for i in range(1, min(n, k) + 1) for j in range(i))
print(ans)

Solution (Python):
from itertools import chain, islice
 
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
ans = max(sum(chain(v[:i - j], v[n - j:])) -
          sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i))
          for i in range(1, min(n, k) + 1) for j in range(i))
print(ans)

Solution (Python):
from itertools import islice
 
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
ans = max(sum(v[:i - j] + v[n - j:]) -
          sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i))
          for i in range(1, min(n, k) + 1) for j in range(i))
print(ans)

Solution (Python):
from itertools import chain, islice
 
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
ans = max(sum(v[m] for m in chain(range(i - j), range(n - j, n))) -
          sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i))
          for i in range(1, min(n, k) + 1) for j in range(i))
print(ans)

Solution (Python):
from itertools import chain
 
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
r = 0
for i in range(1, min(n, k) + 1):
    for j in range(i):
        r1, r2 = i - j, n - j
        s = sum(v[m] for m in chain(range(r1), range(r2, n)))
        j = i
        for x, m in a:
            if j == k:
                break
            if m < r1 or m >= r2:
                s -= x
                j += 1
        r = max(r, s)
print(r)

Solution (Python):
from itertools import chain
 
R = lambda: map(int, input().split())
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
r = 0
for i in range(1, min(n, k) + 1):
    for j in range(i):
        r1, r2 = range(i - j), range(n - j, n)
        s = sum(v[m] for m in chain(r1, r2))
        j = i
        for x, m in a:
            if j == k:
                break
            if m in r1 or m in r2:
                s -= x
                j += 1
        r = max(r, s)
print(r)

Solution (Python):
N, K = map(int,input().split())
A = 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 j == 0:
      st = A[:i]
    else:
      st = A[:i]+ A[-j:]
    st.sort(reverse = 1)
    for suteru in range(K - i - j+1):
      ans = max(ans, sum(st[:i+j-suteru]))
    
print(ans)

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

ans = - 10**7
for i in range(min(N,K)+1):
  for j in range(min(N,K)+1-i):
    if j == 0:
      st = A[:i]
    else:
      st = A[:i]+ A[-j:]
    st.sort(reverse = 1)
    for suteru in range(K - i - j+1):
      ans = max(ans, sum(st[:i+j-suteru]))
    
print(ans)

Solution (Python):
from numpy import*
n,k,*v=int64(open(0).read().split())
m=0
for i in range(n):
 for j in range(min(k,n)-i+1):t=sort(v[:i]+v[:~j:-1]);m=max(m,sum(t)-sum(t[t<0][:k-i-j]))
print(m)

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
R=min(N,K)
L=[]
for r in range(R+1):
    for A in range(R -r + 1):
        B = R - r-A
        I = sorted((V[:A] + V[::-1][:B]))
        p=K-A-B
        for i in range(len(I)):
            if I[i] >= 0:
                p = i
                break
        for ii in range(min(p,K-A-B)):
            if len(I)>0:
                I.pop(0)
            else:break
        L.append(sum(I))
print(max(L))

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

maxv = 0
for l in range(min(K, N) + 1):
    for r in range(min(K, N) - l + 1):
        gets = V[:l + 1] + V[-1 - r:]
        d = K - l - r
        
        for i in range(d):
            if len(gets) <= 0:
                break
            v = min(gets)
            if v >= 0:
                break
            
            gets.remove(v)
        maxv = max(maxv, sum(gets))
        

print(maxv)

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

maxv = 0
for l in range(min(K, N) + 1):
    for r in range(min(K, N) - l + 1):
        gets = V[:l + 1] + V[-1 - r:]
        gets.sort()
        d = K - l - r
        
        for i in range(d):
            if len(gets) <= 0:
                break
            v = min(gets)
            if v >= 0:
                break
            
            gets.remove(v)
        maxv = max(maxv, sum(gets))
        

print(maxv)

Solution (Python):
from numpy import*
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n):
 for j in range(min(k,n)-i+1):t=sort(v[:i]+v[:~j:-1]);m=max(m,sum(t)-sum(t[t<0][:k-i-j]))
print(m)

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

ans = 0

for left in range(0, K + 1) :
    for right in range(0, K - left + 1) :
        if left + right > N :
            break

        cd = K - left - right

        res = 0
        minus = []

        for vi in V[:left] :
            if vi < 0 :
                minus.append(vi)
            res += vi

        for vi in V[-1:-1-right:-1] :
            if vi < 0 :
                minus.append(vi)
            res += vi

        if len(minus) > cd :
            minus.sort()
            res -= sum(minus[:cd])
        else :
            res -= sum(minus)

        ans = max(ans, res)

print(ans)

Solution (Python):
from heapq import heappush, heappop, heapify

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










S = 0
for take in range(min(N, K) + 1):
    for takeL in range(take + 1):
        takeR = take - takeL
        hand = []
        for i in range(takeL):
            heappush(hand, V[i])
        for i in range(takeR):
            if N - i - 1 < takeL:
                break
            heappush(hand, V[N- i - 1])
        S = max(sum(hand), S)
        for _ in range(K - take):
            if not hand or hand[0] >= 0:
                break
            heappop(hand)
            S = max(sum(hand), S)
print(S)

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

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(K + 1):
    for r in range(K + 1 - l):
        if l + r <= N:
            jotai.append([l, r, K - l - r])

for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    
    for k in range(jotai[i][2]):
        if tmp and min(tmp) < 0:
            tmp.remove(min(tmp))
    
    ans = max(sum(tmp), ans)
print(ans)

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

N, K = map(int, input().split())
V = list(map(int, input().split()))
q = deque()
for v in V:
    q.append(v)
ans = 0
jotai = []
for l in range(K + 1):
    for r in range(K + 1 - l):
        if l + r <= N and l + r  <= K:
            jotai.append([l, r, K - l - r])

for i in range(len(jotai)):
    d = deepcopy(q)
    tmp = []
    for l in range(jotai[i][0]):
        tmp.append(d.popleft())
    
    for r in range(jotai[i][1]):
        tmp.append(d.pop())
    
    for k in range(jotai[i][2]):
        if (tmp and min(tmp) < 0):
            tmp.remove(min(tmp))
    
    ans = max(sum(tmp), ans)
print(ans)

Solution (Python):


from heapq import *
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]


ans = 0
k=min(k,2*n)
for l in range(min(n,k)+1):
    for r in range(min(k-l,n-l)+1):
        for sute in range(min(k-l-r,l+r)+1):
            check = a[:l]+a[n-r:]
            heapify(check)

            c=0
            while(c < sute and check and check[0] < 0):
                heappop(check)
                c+=1
            ans = max(ans, sum(check))
print(ans)

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


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


def cal(r, k, K, V):
    l = k - r
    if l == 0:
        A = V[:r]
    elif r == 0:
        A = V[-l:]
    else:
        A = V[:r] + V[-l:]
    A.sort()
    for i, a in enumerate(A):
        if i < min(k, K-k) and a < 0:
            A[i] = 0
        else:
            break
    return sum(A)


def main():
    N, K = LI()
    V = LI()
    ans = 0
    for k in range(1, min(N, K)+1):
        for l in range(k+1):
            ans = max(ans, cal(l, k, K, V))
    print(ans)
    return


main()

Solution (Python):
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-a):
        if b==0:
            v=V[:a]
        else:
            v=V[:a]+V[-b:]
        v.sort()
        for i in range(k+1-a-b):
            if i>=len(v):
                break
            if v[i]>0:
                break
        ans=max(ans,sum(v[i:]))
print(ans)

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

ans = []
for k_l in range(K+1):
    rem = K - k_l
    for k_r in range(rem+1):
        if k_l + k_r > N:
            continue
        rem_ = rem-k_r
        v = V[:k_l]+V[N-k_r:]
        
        v.sort()
        for i in range(rem_):
            if i < len(v) and v[i] < 0:
                v[i] = 0
        
        ans.append(sum(v))


print(max(ans))

Solution (Python):
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-a):
        if b==0:
            v=V[:a]
        else:
            v=V[:a]+V[-b:]
        v.sort()
        for i in range(k+1-a-b):
            if i>=len(v):
                break
            if v[i]>=0:
                break
        ans=max(ans,sum(v[i:]))
print(ans)

Solution (Python):
import numpy as np 

N, K = map(int, input().split())
vs = np.array([int(v) for v in input().split()])
ak = min(N + 1, K + 1)
max_value = 0

for a in range(ak):
    bk = min(N - a + 1, K - a + 1)
    for b in range(bk):
        n_remove = K - a - b
        l = np.sum(vs[:a]) if a > 0 else 0
        r = np.sum(vs[-b:]) if b > 0 else 0
        
        if a > 0 and b > 0:
            pos = np.r_[vs[:a], vs[-b:]]
            order = np.argsort(pos)
            pos = pos[order]
        elif a > 0:
            pos = vs[:a]
            order = np.argsort(pos)
            pos = pos[order]
        elif b > 0:
            pos = vs[-b:]
            order = np.argsort(pos)
            pos = pos[order]
        else:
            pos = np.array([])

        n_minus = len(np.where(pos < 0)[0])
        
        value = l + r - np.sum(pos[:n_minus]) if n_minus < n_remove else l + r - np.sum(pos[:n_remove])
        
        max_value = max(value, max_value)

print(max_value)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()
V=LIST()

def calc(l, r):
    
    if l+(N-r)>K:
        return -INF
    ngtv=[]
    res=0
    
    for i in range(l):
        res+=V[i]
        if V[i]<0:
            ngtv.append(V[i])
    
    for i in range(N-1, r-1, -1):
        res+=V[i]
        if V[i]<0:
            ngtv.append(V[i])
    
    ngtv.sort()
    
    k=l+(N-r)
    i=0
    
    while k<K and i<len(ngtv):
        res-=ngtv[i]
        i+=1
        k+=1
    return res

ans=-INF

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

Solution (Python):

from bisect import bisect
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for left in range(n + 1):
    for right in range(n + 1 - left):
        if left > n - right or left + right > k:
            continue
        li = sorted(v[:left] + v[n - right:])
        rem = min(bisect(li, 0), k - left - right)
        ans = max(ans, sum(li[rem:]))
print(ans)

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

for l in range(K + 1):
	for r in range(K - l + 1):
		inHand = []
		if l + r > N:  
			break
		
		inHand = V[: l]
		inHand.extend(V[N - r:])
		tmp = sum(inHand)  
		
		inHand.sort()
		for i in range(min(K - l - r, l + r)):
			if inHand[i] >= 0:
				break
			tmp -= inHand[i]
		ans = max(ans, tmp)

print(ans)

Solution (Python):

















import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations
from bisect import bisect_left, bisect_right


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")






N, K = s_inpl()
V = l_inpl()
rV = V[::-1]
ans = 0
for l in range(K+1):
    for r in range(K+1-l):
        if l + r > N:
            continue
        d = K - l - r
        get_l = []
        current_score = 0
        for i in range(l):
            current_score += V[i]
            get_l.append(V[i])
        for i in range(r):
            current_score += rV[i]
            get_l.append(rV[i])
        get_l.sort()
        for i in range(d):
            if i >= len(get_l):
                break
            if get_l[i] > 0:
                break
            current_score -= get_l[i]

        ans = max(ans, current_score)

print(ans)

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

    m = 0 
    r = min(N, K)
    for a in range(r+1):
        for b in range(r+1-a):
            if b == 0:
                v = V[:a]
            else:
                v = V[:a] + V[-b:]
            v.sort()    
            for i in range(K+1-a-b):
                if i >= len(v):
                    break
                if  v[i] >= 0:
                    break
            s = sum(v[i:])
            if s > m:
                m = s

    print(m)


main()

Solution (Python):
import math
from functools import reduce
from collections import deque

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,k = get_nums_l()
v = get_nums_l()


left = [0]*(k+1)
leftmark = [0]*(k+1)

right = [0]*(k+1)

for i in range(1,k+1):
    score = max(left[i-1], sum(v[:i]))
    leftmark[i] = i
    for ii in range(i//2):
        ii += 1
        score_ = sum(sorted(v[:i-ii],reverse=True)[:i-(ii*2)])
        if score_ > score:
            score = score_
            leftmark[i] = i-ii
    left[i] = score

for i in range(1,k+1):
    score = max(right[i-1],sum(v[max(leftmark[k-i],(n-i)):]))
    for ii in range(i//2):
        ii += 1
        score = max(score, sum(sorted(v[max(leftmark[k-i],n-i+ii):],reverse=True)[:min(n-leftmark[k-i], i-(ii*2))]))
    right[i] = score

ans = 0

for i in range(k+1):
    ans = max(ans, left[i] + right[k-i])
print(ans)

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

for l in range(K + 1):
	for r in range(K - l + 1):
		inHand = []
		if l + r > N:  
			break

		
		inHand = V[: l]
		inHand.extend(V[N - r:])
		tmp = sum(inHand)  

		
		inHand.sort()
		for i in range(min(K - l - r, l + r)):
			if inHand[i] >= 0:
				break
			tmp -= inHand[i]

		ans = max(ans, tmp)

print(ans)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
M = min(N, K)
ans = 0
for A in range(M+1):
    for B in range(M+1-A):
        C = K - (A+B)
        p = []
        m = []
        for i in range(A):
            if V[i] >= 0:
                p.append(V[i])
            else:
                m.append(V[i])
        for i in range(B):
            if V[N-1-i] >= 0:
                p.append(V[N-1-i])
            else:
                m.append(V[N-1-i])
        m.sort()
        if C < len(m):
            m = m[C:]
        else:
            m = [0]
        tmp = sum(m)+sum(p)
        ans = max(ans, tmp)
print(ans)

Solution (Python):
def d_equeue(N, K, V):
    ans = 0
    for l in range(K + 1):
        for r in range(K - l + 1):  
            if l + r > N:
                continue

            
            get_jewelry = V[:l] + V[N - r:]
            now_value = sum(get_jewelry)

            
            d = K - l - r  
            get_jewelry.sort()
            for i in range(d):
                if i >= len(get_jewelry) or get_jewelry[i] > 0:
                    break  
                now_value -= get_jewelry[i]
            ans = max(ans, now_value)
    return ans

N, K = [int(i) for i in input().split()]
V = [int(i) for i in input().split()]
print(d_equeue(N, K, V))

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):
        hand = []
        if l+r > n:
            continue

        hand = list(v[:l])
        hand.extend(list(v[len(v)-r:]))
        d = k - l - r

        now = sum(hand)
        hand.sort()

        for i in range(min(d, len(hand))):
            if hand[i] >= 0:
                break
            now -= hand[i]

        ans = max(ans, now)

print(ans)