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: Wrong Answer

Solution code:
# ABC128 D
from collections import deque

N,K=map(int,input().split())
V=list(map(int,input().split()))
V=V[:]+V[:]
def check(k):
    res=0
    # 連続する k(K,K-1,...)個の宝石をとる
    # K-k個まで捨ててよい
    for i in range(k+1):
        if k<N:
            v=V[N-k+i:N-k+i+k]
        else:
            v=V[:N]
        v.sort(reverse=True)
        for i in range(K-k):
            if not v:
                break
            if v[-1]<0:
                v.pop()
            else:
                break
        if not k<N:
            break
        res=max(res,sum(v))
    return res

res=0
for k in range(min(N,K)+1):
    res=max(res,check(k))
print(res)

Submission status: Wrong Answer

Solution code:
# ABC128 D
from collections import deque

N,K=map(int,input().split())
V=list(map(int,input().split()))
V=V[:]+V[:]
def check(k):
    res=0
    # 連続する k(K,K-1,...)個の宝石をとる
    # K-k個まで捨ててよい
    for i in range(k+1):
        if k<N:
            v=V[N-k+i:N-k+i+k]
        else:
            v=V[:N]
        v.sort(reverse=True)
        for i in range(K-k):
            if not v:
                break
            if v[-1]<0:
                v.pop()
            else:
                break
        if not k<N:
            break
        res=max(res,sum(v))
    return res

res=0
for k in range(K+1):
    res=max(res,check(k))
print(res)

Submission status: Wrong Answer

Solution code:
# ABC128 D
from collections import deque

N,K=map(int,input().split())
V=list(map(int,input().split()))
V=V[:]+V[:]
def check(k):
    res=0
    # k(N,N-1,...)個の宝石をとる
    # K-k個まで捨ててよい
    for i in range(k+1):
        v=V[N-k+i:N-k+i+k]
        v.sort(reverse=True)
        for i in range(K-k):
            if not v:
                break
            if v[-1]<0:
                v.pop()
            else:
                break
        res=max(res,sum(v))
    return res

res=0
for k in range(K+1):
    res=max(res,check(k))
print(res)

Submission status: Wrong Answer

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):
    for b in range(r):
        jew=[]
        if a+b>r:
            break
        for i in range(a):
            jew.append(v[i])
        for i in range(n-1,n-b-1,-1):
            jew.append(v[i])
        
        if len(jew)==0:
            continue

        jew.sort(reverse=True)

        for i in range(k-(a+b)):
            if len(jew)>0:
                if jew[-1]<0:
                    jew.pop(-1)
                else:
                    break
        ans=max(ans,sum(jew))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reversed_v = [0] + v[::-1]
    v = [0] + v
    ans = -float("inf")
    for i in range(n + 1):
        for j in range(n + 1):
            if i + j > min(n, k):
                continue
            now_k = k - i - j
            if now_k < 0:
                continue
            q = []
            for l in range(1, i + 1):
                q.append(v[l])
            for l in range(1, j + 1):
                q.append(reversed_v[l])
            q.sort()
            for i in range(len(q)):
                if q[i] < 0 and now_k > 0:
                    q[i] = 0
                    now_k -= 1
            ans = max(ans, sum(q))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def calc(x,y):
    if y!=0:
        now=V[:x]+V[-y:]
    else:
        now=V[:x]
    now.sort()
    cnt=0
    lim=min(min(n-x-y,k-x-y),len(now))
    while cnt<lim and now[cnt]<0:
        cnt+=1
    print(x,y,now,sum(now[cnt:]))
    return sum(now[cnt:])

ans=0
for i in range(min(n+1,k+1)):
    for q in range(min(n+1-i,k-i+1)):
        ans=max(ans,calc(i,q))
print(ans)

Submission status: Wrong Answer

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


def calc(x,y):
    if y!=0:
        now=V[:x]+V[-y:]
    else:
        now=V[:x]
    now.sort()
    cnt=0
    lim=min(min(n-x-y,k-x-y),len(now))
    while cnt<lim and now[cnt]<0:
        cnt+=1
    #print(x,y,now,sum(now[cnt:]))
    return sum(now[cnt:])

ans=0
for i in range(min(n+1,k+1)):
    for q in range(min(n-i,k-i+1)):
        ans=max(ans,calc(i,q))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
for i in range(1,min(N,K)):#i個とる
    for j in range(-i,1):
        lis = []
        for k in range(i):
            lis.append(V[j+k])
        lis.sort(reverse=True)
        cnt = 0
        while (len(lis) != 0 and i+cnt < K and lis[-1] < 0):
            del lis[-1]
            cnt += 1
        ans = max(ans,sum(lis))
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
M=min(M,N)
for i in range(M+1):
   for j in range(M-i+1):
      mem=l[0:i]+l[N-j:]
      for k in range(min((M-i-j),len(mem))):
         if min(mem)<0:
            mem.remove(min(mem))
      ans=max(ans,sum(mem))
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
for i in range(M+1):
   for j in range(M-i+1):
      mem=l[0:i]+l[N-j:]
      for k in range(min((M-i-j),len(mem))):
         if min(mem)<0:
            mem.remove(min(mem))
      ans=max(ans,sum(mem))
print(ans)

Submission status: Wrong Answer

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

y = min(n, k)

ans = 0

for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    heap2 = heap[:]
    for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2)

print(ans)

Submission status: Wrong Answer

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

y = min(n, k)

ans = 0

for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    
    for r in range(right+1):
        index = -1
        cnt2 = cnt
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
        if r <= rest:
            ans = max(ans, cnt2)
            rest2 = rest - r
            for _ in range(rest2):
                if not heap:
                    break
                cnt2 -= heapq.heappop(heap)
                ans = max(ans, cnt2)
            
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))
ac = [0]
y = min(n, k)

ans = 0
for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    index = -1
    while rest:
        if right == 0 and not heap:
            ans = max(ans, cnt)
            break
        if right == 0:
            ans = max(ans, cnt)
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)
        elif not heap:
            ans = max(ans, cnt)
            cnt += v[index]
            ans = max(ans, cnt)
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
            right -= 1

        elif -heap[0] <= v[index]:
            ans = max(ans, cnt)
            cnt += v[index]
            ans = max(ans, cnt)
            index -= 1
            right -= 1
        else:
            ans = max(ans, cnt)
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)

        rest -= 1

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))
ac = [0]
y = min(n, k)

ans = 0
for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    index = -1
    while rest:
        if right == 0 and not heap:
            ans = max(ans, cnt)
            break
        if right == 0:
            ans = max(ans, cnt)
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)
        elif not heap:
            ans = max(ans, cnt)
            cnt += v[index]
            ans = max(ans, cnt)
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
            right -= 1

        elif -heap[0] < v[index]:
            ans = max(ans, cnt)
            cnt += v[index]
            ans = max(ans, cnt)
            index -= 1
            right -= 1
        else:
            ans = max(ans, cnt)
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)

        rest -= 1

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

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

y = min(n, k)

ans = 0
for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
        cnt += v[j]
        ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    index = -1
    while rest > 0:
        if right == 0 and not heap:
            break
        if right == 0:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)
        elif not heap:
            cnt += v[index]
            ans = max(ans, cnt)
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
            right -= 1

        elif -heap[0] < v[index]:
            cnt += v[index]
            ans = max(ans, cnt)
            index -= 1
            right -= 1
        else:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)

        rest -= 1

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

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

y = min(n, k)

ans = 0
for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
        cnt += v[j]
    ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    index = -1
    while rest:
        if right == 0 and not heap:
            break
        if right == 0:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)
        elif not heap:
            cnt += v[index]
            ans = max(ans, cnt)
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
            right -= 1

        elif -heap[0] < v[index]:
            cnt += v[index]
            ans = max(ans, cnt)
            index -= 1
            right -= 1
        else:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)

        rest -= 1

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

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

y = min(n, k)

ans = 0
for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heapq.heappush(heap, v[j])
        cnt += v[j]
    ans = max(ans, cnt)
    # print(i, cnt)
    rest = k - i - 1
    right = n - i - 1
    index = -1
    while rest:
        if right == 0 and not heap:
            break
        if right == 0:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)
        elif not heap:
            cnt += v[index]
            ans = max(ans, cnt)
            if v[index] < 0:
                heapq.heappush(heap, v[index])
            index -= 1
            right -= 1

        elif -heap[0] <= v[index]:
            cnt += v[index]
            ans = max(ans, cnt)
            index -= 1
            right -= 1
        else:
            cnt -= heapq.heappop(heap)
            ans = max(ans, cnt)

        rest -= 1

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

#n,k=6,4
#v=[-10, 8, 2, 1, 2, 6]

if n<=k:
    v.sort()
    vsum=sum(v)
    kk=k-n
    vsum=0
    for i in range(n):
        if i>kk:
            vsum+=v[i]
        elif i<=kk and v[i]>0:
            vsum+=v[i]
    print(vsum)
    sys.exit()

#elif n<k:
    
vmax=-10**10
if n>k:
    vv=[[0]*k for i in range(k+1)]
    for i in range(k):
        vv[0][i]=v[n-k+i]
    vsum=sum(vv[0])
    vmax=max(vsum,vmax)
#        vsum+=v[i]
#    print(vv[0])
#    vmax=vsum
    for i in range(k):
        vv[i+1]=vv[i].copy()
        vv[i+1].pop(0)
        vv[i+1].append(v[i])
        vsum=sum(vv[i+1])
        vmax=max(vsum,vmax)
#        vsum=vsum-v[k-1-i]+v[n-1-i]
#        vmax=max(vsum,vmax) 
#        print(vsum,vmax,v[k-1-i],v[n-1-i])
#        print(vv[i+1])
    
print(vmax)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
result = 0
for a in range(N + 1):
    for b in range(N + 1):
        if a + b >= K or a + b > N:
            break
        out = V[:a] + V[N-b:]
        out.sort()
        sum_val = sum(out)
        for i in range(min(a + b, K - a - b)):
            if out[i] >= 0:
                break
            sum_val -= out[i]
        result = max(result, sum_val);
print(result)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(k + 1):
    for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = min(n, k) - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = min(n, k) - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b + 1):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) + 1):
        if a + b > min(n, k):
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) + 1):
        # if a + b > k:
        #     break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) + 1):
        if a + b > k:
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k) - a):
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k) - a):
        if a + b > min(n, k):
            break
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k) - a):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(min(n, k) - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k) - a):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        heapq.heapify(have)

        ans = max(ans, tmp)

        for c in range(min(n, k) - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    for b in range(min(n, k) - a):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(min(n, k) - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)
    ans = max(ans, sum(have))

    for b in range(min(n, k) - a):
        for i in range(b):
            have.append(v[-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(min(n, k) - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)
    ans = max(ans, sum(have))

    for b in range(min(n, k) - a):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(min(n, k) - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k)):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(min(n, k) - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])


        heapq.heapify(have)
        remove_sum = 0
        for c in range(1, min(n, k) - a - b + 1):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))

        have = copy.deepcopy(o_have)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
o_v = copy.deepcopy(v)
ans = -10 ** 10
for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    # print(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])


        heapq.heapify(have)
        remove_sum = 0
        for c in range(1, min(n, k) - a - b + 1):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have)-remove_sum)

        have = copy.deepcopy(o_have)
        v = copy.deepcopy(o_v)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0
juwel = []
for l in range(M + 1):
    for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, M - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel))

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0
juwel = []
for l in range(M):
    for r in range(M - l):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, M - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
Ans = -float('inf')
for a in range(K+1):
    for b in range(K+1):
        if a + b > max(K, N):
            break
        c = K - a - b
        A = V[:a] + V[N - b:]
        A.sort()
        ans = sum(A)
        for x in A:
            if c == 0:
                break
            if x >= 0:
                break
            if x < 0:
                c -= 1
                ans -= x
        Ans = max(ans, Ans)
print(Ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
Ans = -float('inf')
for a in range(K+1):
    for b in range(K+1):
        if a + b > K:
            break
        c = K - a - b
        A = V[:a] + V[N - b:]
        A.sort()
        ans = sum(A)
        for x in A:
            if c == 0:
                break
            if x >= 0:
                break
            if x < 0:
                c -= 1
                ans -= x
        Ans = max(ans, Ans)
print(Ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=0
v=list(map(int,input().split()))
for i in range(min(n,k+1)):
    for j in range(min(n-i,k-i)):
        if j!=0:
            tmp=v[:i]+v[-j:]
        else:
            tmp=v[:i]
        tmp.sort()
        t=sum(tmp)
        for d in range(min(k-i-j,len(tmp))):
            if tmp[d]<0:
                t-=tmp[d]
        if t>ans:
            ans=t
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=0
v=list(map(int,input().split()))
for i in range(min(n,k+1)):
    for j in range(min(n-i,k-i+1)):
        if j!=0:
            tmp=v[:i]+v[-j:]
        else:
            tmp=v[:i]
        tmp.sort()
        t=sum(tmp)
        for d in range(min(k-i-j,len(tmp))):
            if tmp[d]<0:
                t-=tmp[d]
        if t>ans:
            ans=t
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=0
v=list(map(int,input().split()))
for i in range(min(n,k)):
    for j in range(min(n-i,k-i)):
        if j!=0:
            tmp=v[:i]+v[-j:]
        else:
            tmp=v[:i]
        tmp.sort()
        t=sum(tmp)
        for d in range(min(k-i-j,len(tmp))):
            if tmp[d]<0:
                t-=tmp[d]
        if t>ans:
            ans=t
print(ans)