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: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-1, N-1-r, -1):
        hold.append(V[i])
    hold.sort()
    return 0 if d >= len(hold) else sum(hold[d:]) 

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = max(K - l - r, 0)
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-1, N-1-r, -1):
        hold.append(V[i])
    hold.sort()
    return sum(hold[d:])

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = max(K - l - r, 0)
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-1, N-1-r, -1):
        hold.append(V[i])
    hold.sort()
    return sum(hold[d:])

ret = 0
for l in range(K+1):
    for r in range(K+1-l):
        d = K - l - r
        if d > l + r:
            continue
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Runtime Error

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

def calc(l, r, d):
    hold = []
    for i in range(l):
        hold.append(V[i])
    for i in range(N-1, N-1-r, -1):
        hold.append(V[i])
    hold.sort()
    return sum(hold[d:])

ret = 0
for l in range(K):
    for r in range(K-l):
        d = K - l - r
        if d > l + r:
            continue
        ret = max(ret, calc(l, r, d))
print(ret)

Submission status: Runtime Error

Solution code:
from itertools import product

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

# 持ってる宝石の価値の最大値
max = 0

# K->取る宝石の個数 , K <=100
for left,right in product(range(K), repeat=2):
    jewelry = 0
    minus = [0]*N

    if left + right > K:
        continue

    # 左側から取る
    for l in range(left):
        jewelry +=  V[l]
        if V[l] < 0:
            minus[l] = V[l]

    # 右側から取る
    for r in range(N-right,N):
        jewelry +=  V[r]
        if V[r] < 0:
            minus[r] = V[r]

    # マイナスの石を削除
    minus.sort()
    for j in range(K - (left + right)):
        if minus[j] != 0:
            jewelry -= minus[j]
        else:
            break
    
    # maxかを判定
    if max < jewelry:
        max = jewelry

print(max)

Submission status: Runtime Error

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

ans=0

getl_sum=0
for lc in range(min(51,k+1)):
    getl_sum += v[lc]
    getr_sum =0
    for rc in range(min(51-lc,k-lc+1)):
        getr_sum += v[-rc-1]
        get = v[:lc] + v[-rc-1:]
        get.sort()
        get_sum = getl_sum + getr_sum
        ans = max(ans,get_sum)
        for bc in range(min(lc+rc,k-lc-rc)):
            get_sum-=get[bc]
            ans = max(ans,get_sum)
print(ans)

Submission status: Runtime Error

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

maxi = 0
for i in range(N):
    for o in range(i):
        fs = 0
        bs = 0
        hand = []
        for p in range(o):
            fs += V[p]
            hand.append(V[p])
        for p in range(i-o, N):
            bs += V[p]
            hand.append(V[p])
        ts = fs + bs
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
            for j in range(K-i):
                if hand[0] <= 0:
                    del hand[0]
                else:
                    break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

Submission status: Runtime Error

Solution code:
maxi = 0
for i in range(N):
    for o in range(i):
        fs = 0
        bs = 0
        hand = []
        for p in range(o):
            fs += V[p]
            hand.append(V[p])
        for p in range(i-o, N):
            bs += V[p]
            hand.append(V[p])
        ts = fs + bs
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
        for j in range(K-i):
            if hand[0] <= 0:
                del hand[0]
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

Submission status: Runtime Error

Solution code:
maximum = 0
for u in range(K):
    for i in u:
        frontSum = 0
        frontList = []
        backSum = 0
        backList = []
        for o in range(i):
            frontSum += V[o]
            frontList.append(V[o])
        for o in range(len(V)-(K-i), len(V)):
            backSum += V[o]
            backList.append(V[o])
        total = frontSum + backSum
        if total > maximum:
            maximum = total
        frontList.sort()
        backList.sort()
        for p in range(K-u):
            frontList = frontLIst[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
            for j in range(K-u-p):
                backList = backList[p:]
                newBackSum = 0
                for k in backList:
                    nweBackSum += k
                if newBackSum > backSum:
                    backSum = newBackSum
        newTotal = backSum + frontSum
        if newTotal > maximum:
            maximum = newTotal
print(maximum)

Submission status: Runtime Error

Solution code:
maximum = float("-inf")
for i in range(K):  # gets the sum of first i numbers + last K-i numbers
    frontSum = 0
    frontList = []
    backSum = 0
    backList = []
    for o in range(i):
        frontSum += V[o]
        frontList.append(V[o])
    for o in range(len(V)-(K-i), len(V)):
        backSum += V[o]
        backList.append(V[o])
    total = frontSum + backSum
    if total > maximum:
        maximum = total
    frontList.sort()
    backList.sort()
    for p in range(2, len(frontList), 2):
        for j in range(p):
            frontList = frontList[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
    for p in range(2, len(backList), 2):
        for j in range(p):
            backList = backList[p:]
            newBackSum = 0
            for k in backList:
                newBackSum += k
            if newBackSum > backSum:
                backSum = newBackSum
    newTotal = backSum + frontSum
    if newTotal > maximum:
        maximum = newTotal
print(maximum)

Submission status: Runtime Error

Solution code:
maximum = float("-inf")
for i in range(K): # gets the sum of first i numbers + last K-i numbers
    frontSum = 0
    frontList = []
    backSum = 0
    backList = []
    for o in range(i):
        frontSum += V[o]
        frontList.append(V[o])
    for o in range(len(V)-(K-i-1), len(V)):
        backSum += V[o]
        backList.append(V[o])
    total = frontSum + backSum
    if total > maximum:
        maximum = total
    frontList.sort()
    backList.sort()
    for p in range(2, i, 2):
        for j in range(p):
            frontList = frontList[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
    for p in range(2, K-i, 2):
        for j in range(p):
            backList = backList[p:]
            newBackSum = 0
            for k in backList:
                newBackSum += k
            if newBackSum > backSum:
                backSum = newBackSum
    newTotal = backSum + frontSum
    if newTotal > maximum:
        maximum = newTotal
print(maximum)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > K: break
            if l+r == 0: continue
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort(reverse=True)

            restK = K - (l+r)

            for i in range(restK):
                if having[-1] < 0:
                    having.pop()
                    continue
                break

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

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
jewels = [0] + list(map(int, input().split())) + [0]
ans = 0
for get in range(min(N, K)+1):
  put = get
  for left in range(get+1):
    right = get - left
    values = jewels[left:] + jewels[:N+1-right]
    sum_v = sum(values)
    minus_values = values.filter(lambda x: x<0)
    minus_values.sort()
    cnt = 0
    for v in minus_values:
      sum_v -= v
    ans = max(ans, sum_v)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store = deque(sorted(store))
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                continue
            else:
                max_value.appendleft(e)
                break
        else:
            break
    max_value = sum(store)
    return max_value

    
for k in range(K+1):
    for l in range(K+1):
        for r in range(K+1-l):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = deque([])
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store.sort()
    max_value = sum(store)
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                max_value -= e
            else:
                break
        else:
            break
    return max_value

    
for k in range(K+1):
    for l in range(K+1):
        for r in range(K+1-l):
            max_value = max(max_value, calc_max_value(copy(V), k, l, r))

print(max_value)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from copy import copy

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

max_value = 0

def calc_max_value(V, k, l, r):
    store = []
    for i in range(l):
        if len(V)>0:
            store.append(V.popleft())
            continue
        else:
            break
    for i in range(r):
        if len(V)>0:
            store.append(V.pop())
            continue
        else:
            break
    store.sort()
    store = deque(store)
    max_value = sum(store)
    for i in range(k-l-r):
        if len(store)>0:
            e = store.popleft()
            if e < 0:
                max_value -= e
            else:
                break
        else:
            break
    return max_value

    
for k in range(K+1):
    for l in range(K+1):
        for r in range(K+1-l):
            max_value = max(max_value, calc_max_value(V.copy(), k, l, r))

print(max_value)

Submission status: Runtime Error

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

ans = 0
for get in range(K+1):
    put = K - get
    for right in range(get+1):
        left = get - right
        jewels = A[:right] + A[N-left:]
        jewels.sort()
        value = sum(jewels)
        for i in range(get):
            if i + 1 > put:
                break
            if jewels[i] < 0:
                value += abs(jewels[i])
            else:
                break
        ans = max(ans, value)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0

ans = 0
for i in range(n):
    temp += arr[i]
    s[i] = temp

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,i+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        if k-i > 0:
            up = min(k-i,i)
            for l in range(up):
                if a[l] < 0:
                    v -= a[l]
                else:
                    break
        if v > ans:
            ans = v

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = map(int,input().split())

arr = list(map(int,input().split()))
s = [0 for i in range(n)]
temp = 0

ans = 0
for i in range(n):
    temp += arr[i]
    s[i] = temp

for i in range(1,k+1):
    upper = min(n,i)
    for j in range(upper+1):
        v = 0
        a = []
        for l in range(j):
            v += arr[l]
            a.append(arr[l])
        for l in range(1,i+1-j):
            v+= arr[-l]
            a.append(arr[-l])
        a.sort()
        # print(a,v)
        if k-i > 0:
            up = min(k-i-1,i)
            for l in range(up):
                if a[l] < 0:
                    v -= a[l]
                else:
                    break
        if v > ans:
            ans = v

print(ans)

Submission status: Runtime Error

Solution code:
# from collections import deque
n,k=map(int,input().split())
a=list(map(int,input().split()))
# v=deque()
# for i in range(n):
#     v.append(a[i])
# print(v)

ans=0
for i in range(1,k+1):
    for j in range(i+1):
        tmp=[]
        # print("{0} {1}".format(i,j))
        for l in range(j):
            if l<k:
                tmp.append(a[l])
        for r in range(i-j):
            if k-1-r>=0:
                tmp.append(a[-1-r])
        # print(tmp)
        tmp.sort(reverse=True)
        # print(tmp)
        # tmp.pop()
        # print(tmp)
        for m in range(k-i):
            if len(tmp)>0:
                tmp.pop()
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
V_=list(reversed(V))
ans=0
for k in range(1,K+1):
    k_=min(k,N)
    for kk in range(k_+1):
        sum=0
        l=[]
        for i in range(kk):
            sum+=V[i]
            l.append(V[i])
        for i in range(k-kk):
            sum+=V_[i]
            l.append(V_[i])
        l.sort()
        for j in range(min(k_,K-k_)):
            if l[j]>=0:
                break
            sum-=l[j]
        ans=max(ans,sum)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
A=0
s=0
S=0
while A<=K:
  B=0
  while B<=K-A:
    C=0
    while (C<=K-A-B and C<=A+B):
      if A==0:
        s=0
      else:
        for i in range(A):
          s=s+V[i]
      if B==0:
        s=s
      else:
        for j in range(B):
          s=s+V[N-j-1]
      left=[]
      for k in range(A):
        left.append(V[k])
      right=[]
      for l in range(B):
        right.append(V[N-l-1])
      v=left+right
      v=sorted(v)
      for m in range(C):
        s=s-v[m]
      print(v)
      print(s)
      C=C+1
    B=B+1
  A=A+1

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
#C,Dは最後でよし
r=min(n,k)
ans_list=[]
for i in range(len(v)):
    #print("----"+str(i)+"-------")
    for j in reversed(range(len(v))):
        if (i+1)+(len(v)-1-j)+1>r:
            #print(i+(len(v)-j),r)
            break
        get=v[:i+1]+v[j:]
        get=sorted(get)[::-1]
        #print(get)
        res = k-(i+(len(v)-j)+1)
        if res<0:
            res=0
        #print(res)
        for l in range(res):
            if get[len(get)-1]<0:
                #print(get[len(get)-1])
                get.pop()
            else:
                #print(get[len(get)-1])
                break
        ans_list.append(sum(get))
print(max(ans_list))

Submission status: Runtime Error

Solution code:
#coding:utf-8



def main():
    
    def dbg(*something):
        if DEBUG:
            print(*something)


    #正の宝石を戻すことはない
    #負の宝石を戻すのは後半
    from sys import stdin
    from math import ceil,floor
    DEBUG = True
    N,K = list(map(int,stdin.readline().split()))
    V = list(map(int,stdin.readline().split()))
    
    max_value = 0
    for getnum in range(1,K+1):  #取る宝石の総数
        for a in range(getnum + 1):  #左から石を取る回数
            J = [0] * getnum #石
            b = getnum - a #右から石を取る回数
            for i in range(a):
                J[i] = V[i]
            for i in range(b):
                J[a+i] = V[-i]
            J.sort()
            #dbg(J[K-getnum:])
            tmpmax = sum(J[K-getnum:])
            max_value = max(max_value,tmpmax)
    print(max_value)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=[int(i) for i in input().split()]
v=[int(i) for i in input().split()]
tmp=v.copy()
res=0
for right in range(k+1):
    for left in range(k-right+1):
        v=tmp.copy()
        ans=[]
        ans+=v[:right]
        ans+=v[n-left:]
        v=v[right:n-left]
        ans.sort()
        for i in range(min(k-right-left,len(ans))+1):
            res=max(res,sum(ans[i:]))
print(res)

Submission status: Runtime Error

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

ans = 0
for pull_num in range(K+1):
  	if pull_num > N:
        continue
    push_num = K - pull_num
    #print('pull_num:{}'.format(pull_num))
    for left_num in range(pull_num+1):
        right_num = pull_num - left_num
        #print('left_num:{} right_num:{}'.format(left_num, right_num))
        left_D = D[:left_num];  right_D = D[N-right_num:]
        #print('left_D:{} right_D:{}'.format(left_D, right_D))
        temochi = left_D + right_D
        temochi.sort()
        #print('temochi:{}'.format(temochi))
        index = bisect.bisect_left(temochi, 0)
        #print('index:{}'.format(index))
        #print('push_num:{}'.format(push_num))
        if push_num >= index:
            ans = max(ans, sum(temochi[index:]))
        else:
            ans = max(ans, sum(temochi[push_num:]))
print(ans)

Submission status: Runtime Error

Solution code:
#coding:utf-8



def main():
    
    def dbg(*something):
        if DEBUG:
            print(*something)


    #正の宝石を戻すことはない
    #負の宝石を戻すのは後半
    from sys import stdin
    from math import ceil,floor
    DEBUG = False
    N,K = list(map(int,stdin.readline().split()))
    V = list(map(int,stdin.readline().split()))


    #残りk回のとき深さk以下の場所に正の宝石がない→終了
    #i回の操作で取れる石>それまでにとるi-1個の石の和
    #深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
    '''
    '''




    def tansaku(left,right,rK,N,score):
        dbg(V[left:right+1],rK,N,score)

        N.sort()
        max_v = score + sum(N[rK:])
        


        # n = []
        for i in range(left,min(rK+left, ceil((right - left +1)/2)+left)):
            
            if V[i] <= 0:
                continue


            next_rK = rK - 1 - (i - left)
            next_tmp_v = tansaku(i+1,right,  next_rK, N+V[left:i], score+V[i])
            #max_v = max(next_tmp_v , max_v)
            if max_v < next_tmp_v:
                max_v = next_tmp_v


            break

        # n = []
        for i in range(right ,max(right - ceil((right-left+1)/2)  ,right-rK)  ,-1 ):
            
            if V[i] <= 0:
                continue

            next_rK = rK - 1 - (right - i)
            next_tmp_v = tansaku(   left,   i-1  ,next_rK,N+V[i+1:right+1],score+V[i])
            m#max_v = max(next_tmp_v , max_v)
            if max_v < next_tmp_v:
                max_v = next_tmp_v
            break


        return max_v

    print(tansaku(0,len(V)-1,K,[],0))





if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#coding:utf-8



def main():
    
    def dbg(*something):
        if DEBUG:
            print(*something)


    #正の宝石を戻すことはない
    #負の宝石を戻すのは後半
    from sys import stdin
    from math import ceil,floor
    DEBUG = False
    N,K = list(map(int,stdin.readline().split()))
    V = list(map(int,stdin.readline().split()))


    #残りk回のとき深さk以下の場所に正の宝石がない→終了
    #i回の操作で取れる石>それまでにとるi-1個の石の和
    #深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
    '''
    '''




    def tansaku(left,right,rK,N,score):
        dbg(V[left:right+1],rK,N,score)

        N.sort()
        # max_v = score + sum(N[rK:])
        max_v = score
        for n in N[rK:]:
            max_v += n


        # n = []
        for i in range(left,min(rK+left, ceil((right - left +1)/2)+left)):
            
            if V[i] <= 0:
                #n.append(V[i])
                continue

            #next_V = V[i+1:left+lenV]
            newN = [0] * (len(N) + i-left)
            for j,nj in enumerate(N):
                newN[j] = nj
            for j in range(left,i):
                newN[j+len(N)] = V[j]
            dbg(V[i])
            next_rK = rK - 1 - (i - left)
            # next_tmp_v = tansaku(i+1,right,  next_rK, N+V[left:i], score+V[i])
            next_tmp_v = tansaku(i+1,right,  next_rK, newN, score+V[i])
            max_v = max(next_tmp_v , max_v)


            break

        # n = []
        for i in range(right ,max(right - ceil((right-left+1)/2)  ,right-rK)  ,-1 ):
            
            if V[i] <= 0:
                #n.append(V[i])
                continue

            #next_V = V[:i]
            next_rK = rK - 1 - (right - i)
            #next_tmp_v = tansaku(   left,   i- left -1  ,next_rK,N+n,score+V[i])
            next_tmp_v = tansaku(   left,   i-1  ,next_rK,N+V[i+1:right+1],score+V[i])
            max_v = max(next_tmp_v , max_v)
            break


        return max_v

    print(tansaku(0,len(V)-1,K,[],0))





if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#coding:utf-8
def dbg(*something):
    if DEBUG:
        print(*something)






#正の宝石を戻すことはない
#負の宝石を戻すのは後半

from math import ceil,floor
DEBUG = False
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))


#残りk回のとき深さk以下の場所に正の宝石がない→終了
#i回の操作で取れる石>それまでにとるi-1個の石の和
#深さiにある石vの価値：v - (それまでにとるi-1個の石の和　except (k-i)個の負の石)
'''
'''




def tansaku(V,rK,N,score):
    dbg(V,rK,N,score)
    # if rK == 0:
    #     db[]
    #     return score + sum(N)


    max_v = score + sum(sorted(N)[rK:])
    #LV = V[:ceil(len(V)/2)]
    RV = V[floor(len(V)/2):][::-1]



    n = []
    for i in range(min(rK,ceil(len(V)/2))):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        next_V = V[i+1:]
        next_rK = rK - 1 - i
        next_tmp_v = tansaku(next_V, next_rK, N+n, score+V[i])
        max_v = max(next_tmp_v , max_v)


        break

    n = []
    # for i in range(min(rK,len(RV))):
        
    #     if RV[i] <= 0:
    #         n.append(RV[i])
    #         continue

    #     next_V = V[::-1][i+1:]
    #     next_rK = rK - 1 - i
    #     next_tmp_v = tansaku(next_V,next_rK,N+n,score+RV[i])
    #     max_v = max(next_tmp_v , max_v)
    #     break
    for i in range(    len(V)-1    ,   min(floor(len(V)/2),len(V)-1-rK),-1 ):
        
        if V[i] <= 0:
            n.append(V[i])
            continue

        next_V = V[:i]
        next_rK = rK - 1 - (len(V)-1-i)
        next_tmp_v = tansaku(next_V,next_rK,N+n,score+V[i])
        max_v = max(next_tmp_v , max_v)
        break


    return max_v

print(tansaku(V,K,[],0))
# for i in range(10,1,-1):
#     print(i)

Submission status: Runtime Error

Solution code:
n, k = map(int, raw_input().split())
V = map(int, raw_input().split())
achievable = 0
for i in range(1, k+1):
    #print "i=" + str(i)
    for l in range(-1, i):
        #print "l=" + str(l)
        candid = 0
        kr = k - i
        subV = V[0:l+1]
        negatives = [x for x in subV if x < 0]
        #print "kr=" + str(kr)
        if l == -1:
            pass
        else:
            candid += sum(subV)
            #print subV
        for r in range(n-1, n-i+l, -1):
            if l >= r:
                break
            candid += V[r]
            #print V[r]
            if V[r] < 0:
                negatives.append(V[r])
        negatives.sort(reverse=True)
        while len(negatives) > 0 and kr > 0:
            candid -= negatives.pop()
            kr -= 1
        achievable = max(achievable, candid)
print achievablen, k = map(int, raw_input().split())
V = map(int, raw_input().split())
achievable = 0
for i in range(1, k+1):
    #print "i=" + str(i)
    for l in range(-1, i):
        #print "l=" + str(l)
        candid = 0
        kr = k - i
        subV = V[0:l+1]
        negatives = [x for x in subV if x < 0]
        #print "kr=" + str(kr)
        if l == -1:
            pass
        else:
            candid += sum(subV)
            #print subV
        for r in range(n-1, n-i+l, -1):
            if l >= r:
                break
            candid += V[r]
            #print V[r]
            if V[r] < 0:
                negatives.append(V[r])
        negatives.sort(reverse=True)
        while len(negatives) > 0 and kr > 0:
            candid -= negatives.pop()
            kr -= 1
        achievable = max(achievable, candid)
print achievable

Submission status: Runtime Error

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

if N <= K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while True:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Runtime Error

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

if N < K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while True:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Runtime Error

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

if N < K:
    ans = sum(V)
    V = sorted(V)
    i = 0
    while True:
        if V[i] >= 0:
            break
        ans += abs(V[i])
        i += 1
    print(ans)
    exit()


ans = 0
for a in range(K,-1,-1):
    for i in range(a+1):
        left = V[:a-i]
        if i != 0:
            right = V[-i:]
        else:
            right = []
        print(left,right)
        lr = sorted(left+right)
        tans = sum(lr)
        for j in range(min(len(lr),K-a)):
            if lr[j] < 0:
                tans += abs(lr[j])
        ans = max(ans , tans)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
T = []
for i in range(k+1):
    if i <= n:
        for a in range(i+1):
            L = V[0:a]+V[n-i+a:n]
            L.sort()
            cur = sum(L)
            for b in range(k-i):
                if b < len(L):
                    if L[b] < 0:
                        cur -= L[b]
            if cur > ans:
                ans = cur
    else:
        L = V
        L.sort()
        cur = sum(L)
        for b in range(k-n):
            if L[b] < 0:
                cur -= L[b]
        if cur > ans:
            ans = cur
print(ans)

Submission status: Runtime Error

Solution code:
inf = input().split()
N = int(inf[0])
K = int(inf[1])
line = [int(v) for v in input().split()]

ans = 0
if K<N:
  for k in range(K+1):
    for i in range(k+1):
      item = line[:i]+line[N-k+i:]
      item.sort()
      count = 0
      while count<K-k and item:
        if item[0]>0:
          break
        item = item[1:]
        count += 1
      s = sum(item)
      if s>ans:
        ans = s
else:
  line.sort()
  count = 0
  while line[0]<0 and count<K:
    line = line[1:]
    count+=1
  s = sum(line)
  if s>ans:
    ans = s

print(ans)