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.



Sample input:
6 4
-10 8 2 1 2 6


Sample output:
14

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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


def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans


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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
from collections import deque


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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# import itertools
# import pprint
import copy
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r>ope or l+r>gem:
      break

    x   = ope-l-r
    now = []
    deq = copy.copy(vlist) 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq 
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) #KがNより大きい場合を防ぐ
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A))
print(M)

Submission status: Accepted

Solution code:
import heapq 

N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) #KがNより大きい場合を防ぐ
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i]
        A3 = A[N-j:]
        new_A = A1+A3
        heapq.heapify(new_A)
        h = K - len(new_A)
        while new_A  and h >0:
            q = heapq.heappop(new_A)
            h-=1
            if q > 0:
                heapq.heappush(new_A,q)
                break
        M = max(M,sum(new_A))
print(M)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

#とるか戻すか
ruil=[0]*N
ruil[0]=V[0]

ruir=[0]*N
ruir[0]=V[-1]
for i in range(1,N):
    ruil[i]=ruil[i-1]+V[i]
    ruir[-(i+1)]=ruir[-i]+V[-(i+1)]
#操作何回やるか 0~K
#K回のうちX回とる　K-X回戻す 0~K
#左右の割合　0~X
ans=0
for i in range(K+1):
    if i==0:
        continue
    for j in range(0,i+1):
        if i/2<=j:
            continue
        #j: 戻す回数
        #i-j取る回数
        for k in range(i-j+1):
            if i-j>=N:
                get=V
            else:
                if i-j-k==0:
                    get=V[:k]
                else:
                    get=V[:k]+V[-(i-j-k):]
            get=sorted(get)
            #print(get,i)
            score=sum(get)-sum(get[:j])
            ans=max(ans, score)
            #print(score, get,i-j,j,k)

print(ans)

Submission status: Accepted

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

    m = min(N, K)

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


if __name__ == '__main__':
    main()

# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for L in range(k+1):
    for R in range(k-L+1):
        if n < L+R:
            break
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:k-L-R])
        if ans < val:
            ans = val
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import heapq

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


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

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

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

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

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

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


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


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


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


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


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations, combinations_with_replacement
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc128/tasks/abc128_d
N, K = read_ints()
V = read_ints()

ans = 0
for i, j in combinations_with_replacement(range(N + 1), r=2):
    # V[0:i]とV[j:]はとったことにしてみる
    k = i + (N - j)
    if k > K:
        continue
    Vsub = V[:i] + V[j:]
    # print(k)
    # print(Vsub)
    Vsub.sort(reverse=True)
    while Vsub and k < K and Vsub[-1] < 0:
        k += 1
        Vsub.pop()
    # print(Vsub)
    ans = max(sum(Vsub), ans)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = inpl()
res = 0
for i in range(k+1):
    pick = i; back = k-i
    for j in range(pick+1):
        l = j; r = pick-j
        q = deque(a)
        # print(l,r)
        now = []
        while q and l > 0:
            now.append(q.popleft())
            l -= 1
        while q and r > 0:
            now.append(q.pop())
            r -= 1
        now.sort(); ln = len(now)
        ans = sum(now)
        res = max(res, ans)
        for i in range(back):
            if i >= ln: continue
            ans -= now[i]
            res = max(res, ans)
print(res)

Submission status: Accepted

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

ans = 0
for l in range(N):
    if l > K: break
    for r in range(N-l+1):
        if l+r > K: break
        vs = V[:l] + (V[-r:] if r>0 else [])
        vs.sort()
        k = K-l-r
        t = 0
        for v in vs:
            if v < 0 and k > 0:
                k -= 1
                continue
            t += v
        ans = max(ans,t)
print(ans)

Submission status: Accepted

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

ans = 0
for l in range(N + 1):
    for r in range(N + 1):
        if l + r > N:
            continue
        k = K - (l + r)
        if k < 0:
            continue

        A = V[:l] + V[N - r:]
        A.sort(reverse=True)
        while k > 0 and A and A[-1] < 0:
            A.pop()
            k -= 1
        ans = max(ans, sum(A))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for left in range(min(N, K) + 1):
    for right in range(min(N - left, K - left) + 1):
        tmp = V[:left] + V[N - right:]
        tmp.sort()
        score = 0
        count = K - left - right
        for i in tmp:
            if count <= 0:
                score += i
            else:
                if i < 0:
                    count -= 1
                else:
                    score += i
        # print (left, right, score, tmp)
        ans = max(ans, score)

print (ans)

Submission status: Accepted

Solution code:
import bisect


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

res = 0
for toru in range(0, min(k, n) + 1):
    tumeru = k - toru
    for migi in range(0, toru + 1):
        hidari = toru - migi
        ans = v[0:hidari] + v[n - migi:n]        
        ans = sorted(ans)
        ind = bisect.bisect_left(ans, 0)
        res = max((sum(ans) - sum(ans[0:min(ind, tumeru)])), res)
print(res)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
for i in range(n):
    for j in range(n-i+1):
        num = lis[:i]
        if j > 0:
            num += lis[-j:]
        if len(num) <= k:
            cnt = min(len(num),k-len(num))
            num.sort()
            for h in range(cnt):
                num[h] = max(0,num[h])
            ans = max(ans,sum(num))
print(ans)

Submission status: Accepted

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

A = []
ans = 0

#左からi個、右からj個除いた時の宝石の価値のmax

for i in range(m+1):
    if i == 0:
        pass
    else:
        A.append(V[i])
    B = []
    for j in range(m-i+1):
        if j == 0:
            pass
        else:
            B.append(V[N-j+1])
        C = A+B
        C.sort()
        a = sum(C)
        for k in range(len(C)):
            if C[k] < 0 and k < K-(i+j):
                a -= C[k]
        ans = max(ans,a)

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heapify, heappop, heappush 
def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
N,K=map(int,input().split())
V=list(map(int,input().split()))
res=0
for i in range(min(K,N)+1):
    for L in range(i+1):
        R=i-L
        li=[]
        for j in range(L):
            li.append(V[j])
        for j in range(R):
            li.append(V[N-j-1])
        li.sort()
        s=sum(li)
        for k in range(min(K-i,len(li))):
            if(li[k]<0):
                s-=li[k]
            else:
                break
        res=max(s,res)
print(res)

Submission status: Accepted

Solution code:
n,k,*l=map(int,open(0).read().split())
v,m=0,min(n,k)+1
from bisect import*
for i in range(m):
  for j in range(m-i): s=sorted(l[:i]+l[n-j:]); v=max(v,sum(s[min(bisect(s,-1),k-i-j):]))
print(v)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,k=f()
l=list(f())
v=0
m=min(n,k)
from bisect import *
for i in range(m+1):
  for j in range(m+1-i):
    s=sorted(l[:i]+l[n-j:])
    v=max(v,sum(s[min(bisect_left(s,0),k-i-j):]))
print(v)

Submission status: Accepted

Solution code:
import bisect
N,K= map(int,input().split())
V = list(map(int,input().split()))
sortedV = sorted(V)
rV = V[::-1]

ans = -10**10
for nout in range(K+1):
    nin = K-nout
    if nout >= N:
        cand = sortedV
        ind = bisect.bisect_left(cand,0)
        ind = min(ind,nin)
        ans = max(ans, sum(cand[ind:]))
    else:
        for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:]))
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
 
ans = 0
for a_plus_b in range(min(N, K) + 1): # a, bの全組み合わせで実施
    for a in range(a_plus_b + 1):
        b = a_plus_b - a
        gems = V[:a] # 左からa番目までを取得
        if b > 0:
            gems += V[-b:] # 右からb番目までを取得
        gems.sort()
        negative_gems = [g for g in gems if g < 0]
        ans = max(ans, sum(gems) - sum(negative_gems[:K - a_plus_b])) # K-(a+b)までネガティブのものを捨てられる 
print(ans)

Submission status: Accepted

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

ans = 0
for a_plus_b in range(min(N, K) + 1):
    for a in range(a_plus_b + 1):
        b = a_plus_b - a
        gems = V[:a]
        if b > 0:
            gems += V[-b:]
        gems.sort()
        negative_gems = [g for g in gems if g < 0]
        ans = max(ans, sum(gems) - sum(negative_gems[:K - a_plus_b]))
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
v = list(map(int, readline().split()))
ans = -float('inf')
nk = min(n, k)
for a in range(nk + 1):
    for b in range(nk - a + 1):
        vv = v[:a] + v[n - b:]
        if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt)
print(ans)

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from copy import deepcopy

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

dq = deque()
dq.extend(V)
INF = 10**10

ans = -INF
for i in range(k+1):
  for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    
    if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
        
if ans != -INF:          
  print(ans)
else:
  print(0)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
lst = []
take = min(N, K)
ans = 0

for i in range(take+1):
    for j in range(take - i +1):
        lst = V[:i] + V[N - j:]
        lst.sort()
        cnt = 0
        for t in lst:
            if t < 0:
                cnt += 1
        d = min(cnt, K - ( i + j))
        del lst[:d]
        ans = max(ans, sum(lst))

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

# 累積和を計算しとく
sum_list = [V[0]]
for i in range(1, N):
    add = V[i] + sum_list[-1]
    sum_list.append(add)

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
        ans=max(ans,total_sum)
            
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        #if A > 0 and B > 0:
        list = V[:A] + V[N - B:]
        total_sum = sum(list) 
        list.sort()
        n = bisect.bisect_left(list, 0)
        if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
        '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue'''
        
        if total_sum > ans:
            ans = total_sum
            
print(ans)