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:
n,k=map(int,input().split())
a=list(map(int,input().split()))
g=-float("INF")
for i in range(0,min(n,k)):
    for j in range(0,min(k-i,n-i)):
        s=sorted(a[:i]+(a[-j:] if j!=0 else []))
        p=sum(s)
        t=i+j
        c=0
        if len(s)>0:
            while s[min(c,len(s)-1)]<0 and len(s)>=c+1 and t+1<=k:
                p-=s[c]
                c+=1
                t+=1
        g=max(g,p)
print(g)

Submission status: Wrong Answer

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

import heapq
import copy
def main(): 
    N,K = map(int,input().split())
    v=list(map(int, input().split()))
    l_count = []
    heapq.heapify(l_count)
    ans = 0
    for l in range(N//2+1):
        if l!=0: heapq.heappush(l_count,v[l-1])
        r_count = copy.deepcopy(l_count)
        for r in range(N-l+1):
            if r!=0: heapq.heappush(r_count,v[N-r])
            d_count = copy.deepcopy(r_count)
            for d in range(K-l-r+1):
                if not(len(d_count) == 0 or d==0):
                    heapq.heappop(d_count)
                sum_d_count = sum(d_count)
                if sum_d_count > ans:
                    ans = sum_d_count
    print(ans)
            
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for n in range(k+1):
        if n >= N or k >= N:
            break
        A = V[:n]
        A.extend(V[N-k+n:])
        A.sort()
        
        c = sum(A)
        for i in range(min(K-k, k)):
            if A[i] <= 0:
                c -= A[i]
        #print(k, n, c)
        ans = max(ans, c)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
Gem = list(map(int, input().split()))
gem = []
Max = Gem[0]
r = min(n,k)
for i in range(r + 1):
    gem = []
    for j in range(r - i + 1):
        gem = Gem[0:i]
        gem += Gem[n - j:n]
        dif = r - (i + j)
        for k in range(dif + 1):
            if len(gem) == 0:
                break
            M = gem.index(min(gem))
            if gem[M] < 0:
                del gem[M]
            else:
                break
        if len(gem) == 0:
            Max = max(Max,0)
        elif Max < sum(gem):
            Max = sum(gem)
print(Max)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
Gem = list(map(int, input().split()))
gem = []
Max = -10 ** 7
r = min(n,k)
for i in range(r + 1):
    gem = []
    for j in range(r - i + 1):
        gem = Gem[0:i]
        gem += Gem[n - j:n]
        dif = r - (i + j)
        for k in range(dif):
            if len(gem) == 0:
                break
            M = gem.index(min(gem))
            if gem[M] < 0:
                del gem[M]
            else:
                break
        if len(gem) == 0:
            Max = max(Max,0)
        elif Max < sum(gem):
            Max = sum(gem)
print(Max)

Submission status: Wrong Answer

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

items = []
ans = 0
for l in range(K):
    for m in range(K - l + 1):
        if l + m > N: break
        litems = deque[0:l]
        mitems = deque[N-m:]
        arr = litems + mitems
        arr.sort()
        items.append(arr)

        d = K - l - m
        for n in range(min(len(arr), d+1)):
            ans = max(ans, sum(arr[n:]))

print(ans)

Submission status: Wrong Answer

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

items = []
for l in range(K):
    for m in range(K - l + 1):
        if l + m > N: break
        litems = deque[0:l]
        mitems = deque[N-m:]
        arr = litems + mitems
        arr.sort()
        items.append(arr)

ans = 0
for i in range(len(items)):
    item = items[i]
    if len(item) == 0: continue
    d = K - len(item)
    total = sum(item)
    for j in range(d):
        if len(item) <= j: break
        if item[0] >= 0: break
        if item[j] < 0:
            total -= item[j]
            if total > ans:
                ans = total

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0

for i in range(min(K,N+1)):
  for l in range(i+1):
    H = []
    r = i-l
    H.extend(V[:l])
    if r > 0:
      H.extend(V[-r:])
    heapify(H)
    for j in range(min(K-i,i)):
      if H[0] >= 0:
        break
      heappop(H)
    ans = max(ans,sum(H))
#  print(H)
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0

for i in range(min(K,N)):
  for l in range(i+1):
    H = []
    r = i-l
    H.extend(V[:l])
    if r > 0:
      H.extend(V[-r:])
    heapify(H)
    for j in range(min(K-i,i)):
      if H[0] >= 0:
        break
      heappop(H)
    ans = max(ans,sum(H))
#  print(H)
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0

for i in range(min(K,N)):
  for l in range(i+1):
    H = []
    r = i-l
    H.extend(V[:l])
    if r > 0:
      H.extend(V[-r:])
    heapify(H)
    for j in range(min(K-i,i)):
      heappop(H)
    ans = max(ans,sum(H))
#  print(H)
print(ans)

Submission status: Wrong Answer

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

ans = 0
num = min(N,K)
for i in range(num+1): # 左端から取り出す回数
    for j in range(num+1-i): # 右端から取り出す回数
        L = V[:i]
        if j == 0:
            R = []
        else:
            R = V[-j:]
        LR = L + R
        num_drop = num-i-j
        LR.sort()
        total = sum(LR)
        idx = 0
        while idx < num_drop and idx < len(LR):
            if LR[idx] < 0:
                total -= LR[idx]
                idx += 1
            else:
                break
        ans = max(ans, total)


print(ans)

Submission status: Wrong Answer

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

ans = 0
num = min(N,K)
for i in range(num): # 左端から取り出す回数
    for j in range(num-i): # 右端から取り出す回数
        L = V[:i]
        if j == 0:
            R = []
        else:
            R = V[-j:]
        LR = L + R
        num_drop = num-i-j
        LR.sort()
        total = sum(LR)
        idx = 0
        while idx < num_drop and idx < len(LR):
            if LR[idx] < 0:
                total -= LR[idx]
                idx += 1
            else:
                break
        ans = max(ans, total)


print(ans)

Submission status: Wrong Answer

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

ans = 0
nums = min(N, K)
for i in range(nums+1): # 左端から取り出す回数
    for j in range(nums+1-i): # 右端から取り出す回数
        for k in range(nums+1-i-j): # Dに戻す個数
            L = V[:i]
            R = V[N-j:]
            #print(L, R, i, j, k)

            LR = L + R
            LR.sort()
            total = sum(LR)
            idx = 0
            while idx < k and idx < len(LR):
                if LR[idx] < 0:
                    total -= LR[idx]
                    idx += 1
                else:
                    break
            ans = max(ans, total)
            

print(ans)

Submission status: Wrong Answer

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

ans = 0
nums = min(N, K)
for i in range(nums+1): # 左端から取り出す回数
    for j in range(nums+1-i): # 右端から取り出す回数
        for k in range(nums+1-i-j): # Dに戻す個数
            L = V[:i]
            R = V[N-j:]
            print(L, R, i, j, k)

            LR = L + R
            LR.sort()
            total = sum(LR)
            idx = 0
            while idx < k and idx < len(LR):
                if LR[idx] < 0:
                    total -= LR[idx]
                    idx += 1
                else:
                    break
            ans = max(ans, total)
            

print(ans)

Submission status: Wrong Answer

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

ans = 0
nums = min(N, K)
for i in range(nums+1): # 左端から取り出す回数
    for j in range(nums+1-i): # 右端から取り出す回数
        for k in range(nums+1-i-j): # Dに戻す個数
            L = V[:i]
            R = V[N-j:]
            #print(L, R, i, j, k)
            if L == []: L = [0]
            if R == []: R = [0]
            LR = L + R
            LR.sort()
            total = sum(LR)
            idx = 0
            while idx < k and idx < len(LR):
                if LR[idx] < 0:
                    total -= LR[idx]
                    idx += 1
                else:
                    break
            ans = max(ans, total)
            

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
res = []
for i in range(min(k, len(lst))):
  throwaways = i
  take = min(k - throwaways, len(lst))
  # throwaways cannot be larger than take
  if throwaways > take:
    break
  for j in range(take+1):
    right = lst[:j]
    if len(right) < take:
      l = take - len(right)
      left = lst[l*-1:]
    else:
      left = []
    m_lst = sorted(right + left)
    for t in range(throwaways):
      del m_lst[0]
    res.append(m_lst)
max_num = max([sum(e) for e in res])
if max_num <= 0:
  print(0)
else:
  print(max_num)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
res = []
for i in range(min(k, len(lst))):
  throwaways = i
  take = k - throwaways
  # throwaways cannot be larger than take
  if throwaways > take:
    break
  for j in range(take+1):
    right = lst[:j]
    if len(right) < take:
      l = take - len(right)
      left = lst[l*-1:]
    else:
      left = []
    m_lst = sorted(right + left)
    for t in range(throwaways):
      del m_lst[0]
    res.append(m_lst)
max_num = max([sum(e) for e in res])
if max_num <= 0:
  print(0)
else:
  print(max_num)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
for i in range(K+1):
    for j in range(K+1):
        if i+j>K:
            continue
        tmp=V[:i]+V[N-j:]
        tmp.sort()
        count_tmp=0
        ans_tmp=sum(tmp)
        for k in tmp:
            if k < 0 and count_tmp < K-i-j:
                ans_tmp-=k
                count_tmp+=1
            ans=max(ans,ans_tmp)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        jewel = []
        if a != 0:
            jewel += lst[:a]
        if b != 0:
            jewel += lst[-b:]
        jewel.sort()
        
        if k > a+b and len(jewel) != 0:
            for c in range(min(len(jewel),k+1-a-b)):
                if jewel[c] < 0:
                    jewel[c] = 0
                else:
                    break
            
        tmp = sum(jewel)
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        jewel = []
        if a != 0:
            jewel += lst[:a]
        if b != 0:
            jewel += lst[-b:]
        jewel.sort()
        
        if k > a+b and len(jewel) != 0:
            for c in range(min(len(jewel),k-a-b)):
                if jewel[c] < 0:
                    jewel[c] = 0
                else:
                    break
            
        tmp = sum(jewel)
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lst = list(map(int,input().split()))
ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        jewel = []
        if a != 0:
            jewel += lst[:a]
        if b != 0:
            jewel += lst[-b:]
        jewel.sort()
        for c in range(min(len(jewel),k-a-b)):
            if jewel[c] < 0:
                jewel[c] = 0
            else:
                break
        tmp = sum(jewel)
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        jewel = []
        if a != 0:
            jewel += lst[:a]
        if b != 0:
            jewel += lst[-b:]
        jewel.sort()
        for c in range(min(len(jewel),k-a-b)):
            if jewel[c] < 0:
                jewel[c] = 0
        tmp = sum(jewel)
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        jewel = []
        if a != 0:
            jewel += lst[:a]
        if b != 0:
            jewel += lst[-b:]
        jewel.sort()

        if len(jewel) != 0:
            for c in range(min(len(jewel),k-a-b)):
                if jewel[c] < 0:
                    jewel[c] = 0
        
        tmp = sum(jewel)
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

res = 0
max_num = min(n, k)
for i in range(max_num+1):
    for j in range(max_num+1 - i):
        my_lst = []
        my_lst += lst[:i]
        if j != 0:
            my_lst += lst[-j:]
        
        s = sum(my_lst)
        sorted_my_lst = sorted(my_lst)
        #print('my_lst :', my_lst)
        for l in range(max_num- i - j):
            if len(my_lst) > l:
                if sorted_my_lst[l] < 0:
                    s -= sorted_my_lst[l]
        res = max(res, s)

print(res)

Submission status: Wrong Answer

Solution code:
#デックをインポート
from collections import deque

#前準備
n,k=[int(i) for i in input().split()]

#デックを用意
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)   
    
#答え
maxnum=0

#繰り返す単位
times=min(n,k)

#aとbの操作の合計のlimitを設定
for limit in range(times+1):
#     print(D)
    
    #aの操作を決定
    for a in range(limit+1):
        get=[]
        Dcopy=deque()
        for i in range(n):
            Dcopy.append(D[i])
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(k-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
            
        print(a,b,Dcopy,get,sumnum)
print(maxnum)

Submission status: Wrong Answer

Solution code:
#デックをインポート
from collections import deque

#前準備
n,k=[int(i) for i in input().split()]

#デックを用意
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)   
    
#答え
maxnum=0

#繰り返す単位
times=min(n,k)

#aとbの操作の合計のlimitを設定
for limit in range(times+1):
#     print(D)
    
    #aの操作を決定
    for a in range(limit+1):
        get=[]
        Dcopy=deque()
        for i in range(n):
            Dcopy.append(D[i])
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(times-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
print(maxnum)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)   
maxnum=0
times=min(n,k)
for limit in range(times):
    for a in range(limit+1):
        get=[]
        Dcopy=deque()
        for i in range(n):
            Dcopy.append(D[i])
        b=limit-a
        for i in range(a):
            get.append(Dcopy.popleft())
        for i in range(b):
            get.append(Dcopy.pop())
        get.sort(reverse=True)
#         print(get)
        for i in range(times-limit):
            if len(get)==0:
                break
            if get[-1]>=0:
                break
            get.pop()
        sumnum=0
        for i in get:
            sumnum+=i
        if sumnum>maxnum:
            maxnum=sumnum
print(maxnum)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]
for i in h:
    D.append(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
D=deque()
h=[int(i) for i in input().split()]

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]

Submission status: Wrong Answer

Solution code:
from bisect import insort_right as ir

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = [0]

def right(x, k, l, lim):
    if lim > k:
        lim = k
    for vr in V[::-1][:lim]:
        k -= 1
        ir(l, vr)
        if vr <= 0:
            x += 1
        ans.append(sum(l[min(k, x):]))    

x1, k1, l1, lim1 = 0, K, [], N
if lim1 > K:
    lim1 = K
right(0, K, [], N - 1)
for vl in V[:lim1]:
    k1 -= 1
    ir(l1, vl)
    if vl <= 0:
        x1 += 1
    ans.append(sum(l1[min(k1, x1):]))
    x2, k2, l2, lim2 = x1, k1, l1[:], N + k1 - K - 1
    right(x2, k2, l2, lim2)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(e) for e in input().split()]
m = min(n, k)
result = 0
for i in range(m + 1):
  for j in range(i + 1):
    t = v[:j]
    t.extend(v[n - (i - j):])
    t.sort()
    k = m - i
    while len(t) > 0 and k > 0 and t[0] < 0:
      t = t[1:]
      k -= 1
    l = sum(t)
    if l > result:
      result = l
print(result)

Submission status: Wrong Answer

Solution code:
import heapq

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = [0]

def right(x, k, l, lim):
    if lim > k:
        lim = k + 1
    for vr in V[:-lim:-1]:
        k -= 1
        heapq.heappush(l, vr)
        if vr <= 0:
            x += 1
        ans.append(sum(l[min(k, x):]))    

x1, k1, l1 = 0, K, []
right(0, K, [], N - 1)
for vl in V[:K]:
    k1 -= 1
    heapq.heappush(l1, vl)
    if vl <= 0:
        x1 += 1
    ans.append(sum(l1[min(k1, x1):]))
    x2, k2, l2, lim2 = x1, k1, l1[:], N + k1 - K
    right(x2, k2, l2, lim2)
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq
from bisect import bisect_right as br

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

ans = []
lim1 = N
if lim1 > K:
    lim1 = K
lim1 += 1
for i in range(lim1):
    l = V[:i]
    l.sort()
    k = K - i
    x = br(l, 0)
    if x > k:
        ans.append(sum(l[k:]))
    else:
        ans.append(sum(l[x:]))
    lim2 = N - i
    if lim2 > k:
        lim2 = k + 1
    for j in range(1, lim2):
        k -= 1
        if V[-j] < 0:
            x += 1
        heapq.heappush(l, V[-j])
        if x > k:
            ans.append(sum(l[k:]))
        else:
            ans.append(sum(l[x:]))        
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq
from bisect import bisect_left as bl

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

ans = []
lim1 = N
if lim1 > K:
    lim1 = K
lim1 += 1
for i in range(lim1):
    l = V[:i]
    l.sort()
    k = K - i
    x = bl(l, 0)
    if x > k:
        ans.append(sum(l[k:]))
    else:
        ans.append(sum(l[x:]))
    lim2 = N - i
    if lim2 > k:
        lim2 = k + 1
    for j in range(1, lim2):
        k -= 1
        if V[-j] < 0:
            x += 1
        heapq.heappush(l, V[-j])
        if x > k:
            ans.append(sum(l[k:]))
        else:
            ans.append(sum(l[x:]))        
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq
from bisect import bisect_left as bl

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

ans = []
lim1 = N
if lim1 > K:
    lim1 = K
lim1 += 1
for i in range(0, lim1):
    l = V[:i]
    l.sort()
    k = K - i
    x = bl(l, 0)
    if x > k:
        ans.append(sum(l[k:]))
    else:
        ans.append(sum(l[x:]))
    lim2 = N - i
    if lim2 > k:
        lim2 = k
    lim2 += 1
    for j in range(1, lim2):
        k -= 1
        if V[-j] < 0:
            x += 1
        heapq.heappush(l, V[-j])
        if x > k:
            ans.append(sum(l[k:]))
        else:
            ans.append(sum(l[x:]))        
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq
from bisect import bisect_left as bl

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

ans = []
lim1 = N
if lim1 > K:
    lim1 = K
lim1 += 1
for i in range(0, lim1):
    l = V[:i]
    l.sort()
    k = K - i
    x = bl(l, 0)
    if x > k:
        ans.append(sum(l[k:]))
    else:
        ans.append(sum(l[x:]))
    lim2 = N - i
    if lim2 > k:
        lim2 = k
    for j in range(1, lim2):
        k -= 1
        if V[-j] < 0:
            x += 1
        heapq.heappush(l, V[-j])
        if x > k:
            ans.append(sum(l[k:]))
        else:
            ans.append(sum(l[x:]))        
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq
from bisect import bisect_left as bl

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

ans = []
lim1 = N
if lim1 > K:
    lim1 = K
x = 0
for i in range(0, lim1):
    l = V[:i]
    l.sort()
    k = K - i
    x = bl(l, 0)
    if x > k:
        ans.append(sum(l[k:]))
    else:
        ans.append(sum(l[x:]))
    lim2 = N - i
    if lim2 > k:
        lim2 = k
    for j in range(1, lim2):
        k -= 1
        if V[-j] < 0:
            x += 1
        heapq.heappush(l, V[-j])
        if x > k:
            ans.append(sum(l[k:]))
        else:
            ans.append(sum(l[x:]))        
print(max(ans))

Submission status: Wrong Answer

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

res=[]
for x in range(min(n,k)+1):
	for a in range(x+1):
		b=x-a
		l=v[:a]
		r=v[n-b:]
		z=[]
		for t in l:
			if t<0:
				z.append(-t)
		for t in r:
			if t<0:
				z.append(-t)
		z.sort()
		z=z[::-1]
		res.append(sum(l)+sum(r)+sum(z[:k-x]))
		print(l,r,z)
print(max(res))

Submission status: Wrong Answer

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

res=[]
for x in range(min(n,k)):
	for a in range(x+1):
		b=x-a
		l=v[:a]
		r=v[n-b:]
		z=[]
		for t in l:
			if t<0:
				z.append(-t)
		for t in r:
			if t<0:
				z.append(-t)
		z.sort()
		z=z[::-1]
		res.append(sum(l)+sum(r)+sum(z[:k-x]))
		#print(l,r,z)
print(max(res))

Submission status: Wrong Answer

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

res=[]
for x in range(min(n,k)):
	for a in range(x+1):
		b=x-a
		l=v[:a]
		r=v[n-b:]
		z=[]
		for t in l:
			if t<0:
				z.append(-t)
		for t in r:
			if t<0:
				z.append(-t)
		z.sort()
		z=z[::-1]
		res.append(sum(l)+sum(r)+sum(z[:k-x]))
		#print(l,r,z)
	print(max(res))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
lim=min(n,k)
V=len(v)
ans=-1*float("inf")
for a in range(0,lim+1):
    for b in range(0,lim+1):
        x=[]
        m=k-a-b
        if a+b>lim:
            break
        for i in range(a):
            x.append(v[i])
        for i in range(V-1,V-b-1,-1):
            x.append(v[i])
        x.sort()
        for i in range(m):
            if x:
                X=x.pop(0)
        ans=max(ans,sum(x))
print(ans)

Submission status: Wrong Answer

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

ans = []
for i in range(min(N,K)):
  for j in range(min(N,K)-i):
    Vc = deque(V.copy())
    cal = deque([0])
    for k in range(i):
      cal.append(Vc.popleft())
    for l in range(j):
      cal.append(Vc.pop())
    cal = deque(sorted(cal))
    for m in range(K-i-j):
      if cal[0] < 0:
        cal.popleft()
      else:
        break
    ans.append(sum(cal))

print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*V, = map(int, input().split())
M = [v if v < 0 else 0 for v in V]
S = [0, V[0], V[-1]]
for i in range(min(k, n)):
    for j in range(i+1):
        s = sum(V[:i-j])+sum(V[n-j:])
        M_sub = sorted(M[:i-j]+M[n-j:])
        s -= sum(M_sub[:k-i])
        S += [s]
print(max(S))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*V, = map(int, input().split())
M = [v if v < 0 else 0 for v in V]
S = [0, V[0]]
for i in range(min(k, n)):
    for j in range(i+1):
        s = sum(V[:i-j])+sum(V[n-j:])
        M_sub = sorted(M[:i-j]+M[n-j:])
        s -= sum(M_sub[:k-i])
        S += [s]
print(max(S))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*V, = map(int, input().split())
M = [v if v < 0 else 0 for v in V]
S = [0, V[0]]
for i in range(min(k, n)):
    for j in range(i):
        s = sum(V[:i-j])+sum(V[n-j:])
        M_sub = sorted(M[:i-j]+M[n-j:])
        s -= sum(M_sub[:k-i])
        S += [s]
print(max(S))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*V, = map(int, input().split())
M = [v if v < 0 else 0 for v in V]
S = []
for i in range(min(k, n)):
    for j in range(i):
        s = sum(V[:i-j])+sum(V[n-j:])
        M_sub = sorted(M[:i-j]+M[n-j:])
        s -= sum(M_sub[:k-i])
        S += [s]
print(*[max(S) if S else 0])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
V = list(map(int, input().split(" ")))
V_rev = list(reversed(V))
val = [[0] * (N + 1) for _ in range(N + 1)]

for i in range(N):
    for j in range(N):
        val[i + 1][j] = val[i][j] + V[i]
        val[i][j + 1] = val[i][j] + V_rev[j]

val[N][N] = val[N][N - 1] + V_rev[N - 1]

ans = 0
for l in range(N + 1):
    for r in range(N + 1):
        num = l + r
        if num > K or num > N:
            continue
        rm = []
        can = (K - num)
        for li in range(l):
            if V[li] < 0:
                rm.append(V[li])
        for ri in range(r):
            if V_rev[ri] < 0:
                rm.append(V_rev[ri])

        rm.sort()
        print(l, r, val[l][r], sum(rm[:can]))
        ans = max(ans, val[l][r] - sum(rm[:can]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
V = list(map(int, input().split(" ")))
val = [[0] * (N + 1) for _ in range(N + 1)]

for i in range(N):
    for j in range(N):
        val[i + 1][j] = val[i][j] + V[i]
        val[i][j + 1] = val[i][j] + V[-1 - j]

val[N][N] = val[N][N - 1] + V[0]

ans = 0
for l in range(N + 1):
    for r in range(N + 1):
        num = l + r
        if num > K:
            continue
        rm = []
        can = (K - num)
        for li in range(l):
            if V[li] < 0:
                rm.append(V[li])
        for ri in range(r):
            if V[-1 - ri] < 0:
                rm.append(V[-1 - ri])

        rm.sort()
        ans = max(ans, val[l][r] - sum(rm[:can]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
V = list(map(int, input().split(" ")))
val = [[0] * (N + 1) for _ in range(N + 1)]

for i in range(N):
    for j in range(N):
        val[i + 1][j] = val[i][j] + V[i]
        val[i][j + 1] = val[i][j] + V[-1 - j]

val[N][N] = val[N][N - 1] + V[-1]

ans = 0
for l in range(N + 1):
    for r in range(N + 1):
        num = l + r
        if num > K:
            continue
        rm = []
        can = (K - num)
        for li in range(l):
            if V[li] < 0:
                rm.append(V[li])
        for ri in range(r):
            if V[-1 - ri] < 0:
                rm.append(V[-1 - ri])

        rm.sort()
        ans = max(ans, val[l][r] - sum(rm[:can]))

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
from collections import deque
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
Q = deque()
ans = 0
R = min(N,K)+1
for l in range(R):
  H = []
  for r in range(R-l):
    if l >= 1:
      for i in range(l):
        heappush(H,V[i])
    if r >= 1:
      for i in range(r):
        heappush(H,V[N-1-i])
    ans = max(ans,sum(H))
#    print(H,sum(H),l,r)
    for i in range(min(len(H),K-l-r)):
      p = heappop(H)
      if p <= 0:
        ans = max(ans,sum(H))
      else:
        break
#      print(H,sum(H))
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
from collections import deque
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
Q = deque()
ans = 0
R = min(N,K)
for l in range(R):
  H = []
  for r in range(R-l):
    if l >= 1:
      for i in range(l):
        heappush(H,V[i])
    if r >= 1:
      for i in range(r):
        heappush(H,V[N-1-i])
    ans = max(ans,sum(H))
    for i in range(min(len(H),K-l-r)):
      p = heappop(H)
      if p <= 0:
        ans = max(ans,sum(H))
      else:
        break
print(ans)