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:
n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): # t個のうち前からs個
    return sorted(v[:s]+v[n-(t-s):])

def f(t):
    res=0
    for i in range(t+1):
        a=get(min(t,n),min(i,n))
        tmp=sum(a)
        res=max(res,tmp)
        for j in range(min(k-t,t,n)):
            tmp-=a[j]
            res=max(res,tmp)
    return res

ans=0
for i in range(1,k+1):
    ans=max(ans,f(i))
print(ans)

Submission status: Accepted

Solution code:
# D - equeue
def main():
    N, K, *V = map(int, open(0).read().split())
    lim = min(N, K)
    available_choices = set()

    for l in range(lim + 1):
        for r in range(lim + 1):
            if l + r > lim:
                break

            choice = V[:l] + V[-r:] if r else V[:l]
            choice.sort()
            cnt_negatives, cnt_rest = sum(i < 0 for i in choice), K - (l + r)

            if cnt_negatives > cnt_rest:
                available_choices.add(sum(choice[cnt_rest:]))
            else:
                available_choices.add(sum(choice[cnt_negatives:]))

    print(max(available_choices))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# D - equeue
def main():
    N, K, *V = map(int, open(0).read().split())
    lim = min(N, K)
    available_choices = set()

    for l in range(lim + 1):
        for r in range(lim + 1):
            if l + r > lim:
                break

            choice = V[:l] + V[-r:] if r else V[:l]
            cnt_negatives = sum(i < 0 for i in choice)
            cnt_rest = K - (l + r)

            if cnt_negatives > cnt_rest:
                choice.sort()
                available_choices.add(sum(choice[cnt_rest:]))
            else:
                available_choices.add(sum(i for i in choice if i > 0))

    print(max(available_choices))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
rev_V = V[::-1]

for L in range(0, K+1):
        for R in range(0, K+1):
            if L + R > min(K,N):
                break
            
            tmp = V[0:L] + rev_V[0:R]
            tmp.sort()
            tmp = deque(tmp)
            num = K - (L+R)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= num or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                if stone >= 0:
                    tmp.append(stone)
                    break
                cnt += 1
            ans = max(ans, sum(tmp))

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
vs = [int(i) for i in input().split()]


def getv(takel, taker, drop):
    g = vs[:takel] + vs[len(vs) - taker:] + [0] * drop
    return sum(sorted(g, reverse=True)[:takel + taker])


def getm(take):
    if take >= n:
        return getv(n, 0, k - take)
    else:
        m = 0
        for takel in range(take + 1):
            m = max([m, getv(takel, take - takel, k - take)])
        return m


def solve():
    return max([getm(take) for take in range(k + 1)])


print(solve())

Submission status: Accepted

Solution code:
import bisect

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

ans = -10**20
for l in range(min(K+1, N+1)):
    for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:]))

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
max_cnt = -float('inf')
for k in range(min(N,K)+1):
  for i in range(k+1):
    j = k - i
    le = V[:i]
    ri = V[-j:] if j > 0 else []
    ne = [i for i in le if i < 0]+[j for j in ri if j < 0]
    ne.sort()
    max_cnt = max(sum(le)+sum(ri) - sum(ne[:K-k]),max_cnt)
    
print(max_cnt)

Submission status: Accepted

Solution code:
from collections import deque
import heapq
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
  
for i in range(0,min(N,K)+1):
  for a in range(i+1):
    b=i-a
    
    l=0
    d=deque(V)
    minus=[]
    
    for j in range(a):
      p=d.popleft()
      l+=p
      if p<0:
        minus.append(p)
    for j in range(b):
      p=d.pop()
      l+=p
      if p<0:
        minus.append(p)
    heapq.heapify(minus)
    for j in range(min(len(minus),K-i)):
      k=heapq.heappop(minus)
      l-=k
    #print(a,b,l)
    ans=max(ans,l)
    
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
v = [0]+[int(i) for i in input().split()]+[0]
ans = 0
cnt1 = 0
m1 = []
for i in range(n+1):
    cnt1 += v[i]
    if v[i] < 0: m1.append(v[i])
    cnt2 = cnt1
    m2 = m1[:]
    for j in range(n-i+1):
        if i + j > k: break
        r = k - i - j
        cnt2 += v[n+1-j]
        if v[n+1-j] < 0: m2.append(v[n+1-j])
        m2.sort()
        s = cnt2
        s -= sum(m2[:r])
        ans = max(s,ans)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
v = [0]+[int(i) for i in input().split()]+[0]
ans = 0
cnt1 = 0
m1 = []
for i in range(n+1):
    cnt1 += v[i]
    if v[i] < 0: m1.append(v[i])
    cnt2 = cnt1
    m2 = m1[:]
    for j in range(n-i+1):
        if i + j > k: break
        r = k - i - j
        cnt2 += v[n+1-j]
        tmp = cnt2
        if v[n+1-j] < 0: m2.append(v[n+1-j])
        m2.sort()
        tmp -= sum(m2[:r])
        ans = max(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
vmax = -10**9
for n1 in range(K+1):
    A = V[:n1]
    ind = min(n1,N)
    m = N-ind
    for n2 in range(min(K-n1,m)+1):
        B = V[N-n2:]
        C = A+B
        C = sorted(C,reverse=True)
        k = K-n1-n2
        for i in range(k):
            if len(C)==0 or C[-1]>=0:break
            C.pop()
        vmax = max(vmax,sum(C))
print(vmax)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

ans = 0

for l in range(min(N, K) + 1): #左側から取る個数
    left = N - l #筒に残っている数
    for r in range(min(left, K - l) + 1): #右側から取る個数
        k = K - l - r #捨てる個数
        tmp = V[:l] + V[N - r:]
        tmp.sort(reverse = False)
        t = sum(tmp)
        for i in range(min(k, l + r)): #最大捨てることができる個数
            if tmp[i] < 0:
                t -= tmp[i]
            else:
                break
        
        ans = max(ans, t)

print (ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bil
N, K, *V = map(int, open(0).read().split())


k = min(N, K)
ans = 0
for i in range(k+1):
    for j in range(k-i+1):
        U = V[:i]+V[-j:] if j != 0 else V[:i]
        U.sort()
        z = min(bil(U, 0), K-i-j)
        sm = sum(U[z:])

        if ans < sm:
            ans = sm

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(N + 1):
    for right in range(N + 1):
        if left + right > N or left + right > K:
            continue
        L = V[:left]
        R = V[::-1][:right]

        A = L + R
        A.sort(reverse=True)
        k = K - (left + right)
        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())
a=list(map(int,input().split()))
ans=0
min_nk=min(n,k)
for i in range(min_nk+1):#左
    for j in range(n-min_nk+i,n+1):#右
        count=a[0:i]+a[j:n]
        count.sort()
       # print(count)
        ans=max(ans,sum(count))
        for x in range(1,k-len(count)+1):#消せれば消す
           # print(sum(count[x:]))
            ans=max(ans,sum(count[x:]))
print(ans)

Submission status: Accepted

Solution code:
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):#右から何個とるか
        x = sorted(v[:i]+v[n-j:])
        for l in range(k-i-j+1):
            ans = max(ans,sum(x[l:]))
print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)+1
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = k-l-r
        data = have[0]+have[1]
        data.sort()
        for i in range(len(data)):
            if i < a and data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Submission status: Accepted

Solution code:
import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
n,k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for left in range(n+1):
    for right in range(left, n+1):
        have = []
        heapq.heapify(have)
        i = 0
        while i < left:
            heapq.heappush(have, v[i])
            i += 1
        cost = left + (n - right)
        if cost > k:
            continue
        i = n-1
        while i >= right:
            heapq.heappush(have, v[i])
            i -= 1
        while k - cost > 0 and len(have) > 0:
            value = heapq.heappop(have)
            if  value >= 0:
                heapq.heappush(have,value)
                break
            cost += 1
        #print("left:{} right:{} ans:{}".format(left, right, sum(have)))
        ans = max(ans, sum(have))
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,k=NM()
l=L()
ans=0
for a in range(n+1):
    for b in range(n-a+1):
        for c in range(a+1):
            for d in range(b+1):
                if a+b+c+d>k:
                    continue
                x=l[:a]
                y=l[-1:-b-1:-1]
                x.sort()
                y.sort()
                ans=max(ans,sum(x[c:])+sum(y[d:]))
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a[::-1]
ans = 0
for i in range(1,min(n+1, k+1)):
    for j in range(i+1):
        m = i - j
        l = a[:m] + b[:j]
        ml = sorted([i for i in l if i < 0])
        pl = [i for i in l if i >= 0]
        ml = ml[min(k-i, len(ml)):]
        if ml:
            minus = sum(ml)
        else:
            minus = 0
        if pl:
            plus = sum(pl)
        else:
            plus = 0
        ans = max(ans, minus + plus)        
print(ans)

Submission status: Accepted

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

result=0

for a in range(min(n,k)+1):
    for b in range(min(n,k)+1-a):
        tmp=v[:a]+v[n-b:]
        tmp.sort()

        lost=k-a-b

        ttt=[]
        cnt=0
        for item in tmp:
            if item<0 and cnt<lost:
                ttt.append(item)
                cnt+=1
            else:
                break
        result=max(result,sum(tmp)-sum(ttt))

print(result)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
from collections import deque
au = list(map(int,input().split()))
left = [0 for i in range(n)]
right = [0 for i in range(n)]
left[0] = au[0]
right[0] = au[-1]
ans = 0
for i in range(n-1):
    left[i+1] = left[i] + au[i+1]
    right[i+1] = right[i] + au[n-i-2]
p = min(n,k)
for a in range(p+1):
    for b in range(p+1-a):
        d = deque(au)
        x = []
        y = 0
        for i in range(a):
            u = d.popleft()
            x.append(u)
            y += u
        for j in range(b):
            v = d.pop()
            x.append(v)
            y += v
        x.sort(reverse = True)
        for ue in range(k-a-b):
            if len(x) > 0 and x[-1] < 0:
                y -= x.pop()
            else:
                break
        ans = max(ans,y)
print(ans)

Submission status: Accepted

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

c = min(n, k)
ans = 0
for a in range(c + 1):
    for b in range(c + 1 - a):
        w = v[:a] + v[n-b:]  # 手持ち
        w.sort()
        for i in range(k - (a + b)):
            if len(w) > 0 and w[0] < 0:
                del w[0]
            else:
                break
        ans = max(ans, sum(w))
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappop,heappush


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


    res=0

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

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

            res=max(res,sum(hands))

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

mini = min(n,k)
mx = 0

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

Submission status: Accepted

Solution code:
# AtCoder
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for l in range(K+1):
    for r in range(K-l+1):
        if l+r > N:
            break
        d = K-(l+r)

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

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

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

Submission status: Accepted

Solution code:
#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
input_list = lambda : list(map(int,input().split()))

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for pick in range(min(K+1, N+1)):
    for right in range(pick+1):
        left = pick - right
        pick_list = V[:right] + V[N-left:]

        pick_list.sort()
        tmp_ans = sum(pick_list)
        rem = min(K - pick, len(pick_list))
        for i in range(rem):
            if pick_list[i] < 0:
                tmp_ans -= pick_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for pick in range(min(K+1, N+1)):
    for right in range(pick+1):
        left = pick-right
        pick_list = V[:right]+V[N-left:]

        pick_list = sorted(pick_list)
        tmp_ans = sum(pick_list)
        rem = min(K-pick, len(pick_list))
        for i in range(rem):
            if pick_list[i] < 0:
                tmp_ans -= pick_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from heapq import heapify, heappush, heappop

def main():
    N, K = map(int, readline().rstrip().split())
    V = list(map(int, readline().rstrip().split()))
    V_rev = V[::-1]
    res = 0
    for x in range(min(N, K)+1):
        for y in range(min(N, K)-x+1):
            z = K - x - y
            temp = V[:x] + V_rev[:y]
            heapify(temp)
            for _ in range(z):
                if temp:
                    m = heappop(temp)
                    if m >= 0:
                        heappush(temp, m)
                        break
                else:
                    break

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from heapq import heapify,heappush,heappop
import math

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

ans = 0
sum_d = 0
d = []
heapify(d)
# i = 操作回数
# j = 取る回数 (i-j = 捨てる回数)
# k = 左から取る回数 (j-k = 右から取る回数)
for i in range(1,K+1):
	for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d)
print(ans)

Submission status: Accepted

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

ans = 0  # 何もしないと0なので、ansの初期値は0


# pick->取り出す操作をする回数(listより多く取り出さないように注意)
for pick in range(min(K+1, N+1)):
    # 右から取り出す数
    for right in range(pick+1):
        left = pick-right  # 左から取り出す数
        pick_list = V[:right]+V[N-left:]

        # queueに戻す操作
        pick_list = sorted(pick_list)
        tmp_ans = sum(pick_list)  # 戻す前の合計値
        rem = min(K-pick, len(pick_list))  # 戻す操作をできる回数
        for i in range(rem):  # 負の値のものをできるだけ戻す
            if pick_list[i] < 0:
                tmp_ans -= pick_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Accepted

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

#a:左からとる b:右からとる K-a-b:捨てる
score=0
torikata=[]
for d in range(101):
    for a in range(101):
        for b in range(101):
            if d+a+b<=K and a+b<=N:
                torikata.append((a,b,d))

for i in range(len(torikata)):
    A,B,D=torikata[i][0],torikata[i][1],torikata[i][2]
    H=[]
    for i in range(A):
        H.append(V[i])
    for i in range(B):
        H.append(V[-(i+1)])
    heapq.heapify(H)
    for i in range(min(D,A+B)):
        heapq.heappop(H)
    score=max(score,sum(H))

print(score)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

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

Submission status: Accepted

Solution code:
import heapq

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

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

print(ans)

Submission status: Accepted

Solution code:
import heapq,copy

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

Submission status: Accepted

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

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


    print(ans)




        

if __name__ == "__main__":
    main()