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()))
ansl = []
p = 1
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i):
            if len(v1) != 0:
                del v1[0]
        if len(v1) != 0:
            ansl.append(sum(v1))
        elif len(v1) == 0 and k%2 == 0:
            ansl.append(0)
ans = max(ansl)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
p = 1
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i-2):
            if len(v1) != 0:
                del v1[0]
        if len(v1) != 0:
            ansl.append(sum(v1))
        elif len(v1) == 0 and k%2 == 0:
            ansl.append(0)
ans = max(ansl)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
if n <= k:
    ansl = []
    count = 0
    for i in range(n+1):
        for j in range(i+1):
            v1 = v[0:j]
            v2 = v[n-(i-j):n]
            v1.extend(v2)
            v1.sort()
            for x in range(k-i):
                if len(v1) != 0:
                    del v1[0]
            ansl.append(sum(v1))
    ans = max(ansl)
else:
    ansl = []
    for x in range(k+1):
        v0 = []
        v1 = v[0:x]
        v2 = v[n-(k-x):n]
        v3 = sorted(v1)
        v4 = sorted(v2)
        p = 0
        r = 0
        if len(v3) > 0:
            while v3[p] < 0 and len(v1) > 0:
                if v3[p] + v1[-1] < 0:
                    v1.remove(v3[p])
                    del v3[p]
                    if len(v1) > 0:
                        del v1[-1]
                else:
                    p = p + 1
                if p == x-1:
                    break
        if len(v4) > 0:
            while v4[r] < 0 and len(v2) > 0:
                if v4[r] + v2[0] < 0:
                    v2.remove(v4[r])
                    del v4[r]
                    if len(v2) > 0:
                        del v2[0]
                else:
                    r = r + 1
                if r == k - x-1:
                    break
        ansl.append(sum(v1)+sum(v2))
    ans = max(ansl)
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.sort()
    count = 0
    v0 = []
    for i in range(k-n):
        if v[i] < 0:
            ans = ans - v[i]
            count = count + 1
            v0.append(v[i])
        if (k - count)%2 == 1:
            if len(v0) == 0:
                ans = ans - min(v)
            elif len(v0) > 0 and v[count] > -min(v0):
                ans = ans + min(v0)
            elif len(v0) > 0 and v[count] <= -min(v0):
                ans = ans - v[count]
else:
    ansl = []
    for x in range(k+1):
        v0 = []
        v1 = v[0:x]
        v2 = v[n-(k-x):n]
        v3 = sorted(v1)
        v4 = sorted(v2)
        p = 0
        r = 0
        if len(v3) > 0:
            while v3[p] < 0 and len(v1) > 0:
                if v3[p] + v1[-1] < 0:
                    v1.remove(v3[p])
                    del v3[p]
                    if len(v1) > 0:
                        del v1[-1]
                else:
                    p = p + 1
                if p == x-1:
                    break
        if len(v4) > 0:
            while v4[r] < 0 and len(v2) > 0:
                if v4[r] + v2[0] < 0:
                    v2.remove(v4[r])
                    del v4[r]
                    if len(v2) > 0:
                        del v2[0]
                else:
                    r = r + 1
                if r == k - x-1:
                    break
        ansl.append(sum(v1)+sum(v2))
    ans = max(ansl)
print(ans)

Submission status: Runtime Error

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j >N:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k > len(dump_num):
                break;
            if dump_num[k] >= 1:
                break;
            num -=dump_num[k]
        
        if num > ans:
            ans = num
print(ans)

Submission status: Runtime Error

Solution code:
#D問題
N, M=map(int, input().split())
V = input().split()
V_i = [int(i) for i in V]

ans = 0
for i in range(M):
    for j in range(M-i):
        if i+j >M:
            break;
        num = 0
        dump_num = []
        for k in range(i):
            num += V_i[k]
            dump_num.append(V_i[k])
        for k in range(N-1,N-j-1,-1):
            num+=V_i[k]
            dump_num.append(V_i[k])
        dump_num.sort()
        for k in range(M-i-j):
            if k >= len(dump_num):
                break;
            if dump_num[k] >= 1:
                break;
            num -=dump_num[k]
        if num > ans:
            ans = num
print(ans)

Submission status: Runtime Error

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())
    lefts = [0] * (k + 1)
    rights = [0] * (k + 1)
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """
    for i in range(1, k + 1):
        _v = sorted(v[:i], reverse=True)
        lefts[i] = max(lefts[i], sum(_v))
        j = 0def solve(string):
    n, k, *v = map(int, string.split())
    """
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i):])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    """

    def s(w):
        l = [0] * (k + 1)
        for i in range(1, k + 1):
            _w = sorted(w[:i], reverse=True)
            tmp = sum(_w)
            l[i] = max(l[i], tmp)
            j = 0
            while i + j < k and len(_w) and _w[-1] < 0:
                j += 1
                tmp -= _w.pop()
                l[i + j] = max(l[i + j], tmp)
        return l

    lefts = s(v)
    rights = s(v[::-1])
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            lefts[i + j] = max(lefts[i + j], sum(_v))
    for i in range(1, k + 1):
        _v = sorted(v[-i:], reverse=True)
        rights[i] = max(rights[i], sum(_v))
        j = 0
        while i + j < k and len(_v) and _v[-1] < 0:
            j += 1
            _v.pop()
            rights[i+j] = max(rights[i+j], sum(_v))
    ans = max([lefts[i] + rights[k - i] for i in range(k + 1)])
    ans = min(ans, sum([_v for _v in v if _v > 0]))
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Runtime Error

Solution code:
def solve(string):
    n, k, *v = map(int, string.split())

    def dfs(deq, get, count, minus):
        if count == k:
            return sum(get)
        op1 = dfs(deq[1:], get + [deq[0]], count + 1, minus + (deq[0] < 0))
        op2 = dfs(deq[:-1], get + [deq[-1]], count + 1, minus + (deq[-1] < 0))
        m = min(get) if len(get) > 0 else 0
        op3 = 0 if m >= 0 else dfs(deq[:], sorted(get)[1:], count + 1, minus - 1)
        return max(op1, op2, op3)

    return str(dfs(v[:], [], 0, 0))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
const bool debug=false;
#define DEBUG if(debug==true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main(void) {
    int n,k;
    cin >> n >> k;
    vector<int> q;
    priority_queue<int, vector<int>, greater<int> > pq; 
    rep(i,n){
        int tmp;
        cin >> tmp;
        q.push_back(tmp);
    }
    int res=INT_MIN;
    rep(i,k+1){
        for(int j=0;i+j<=k;j++){
            for(int l=0;i+j+l<=k;l++){
                int tmp=0;
                rep(ii,i){
                    tmp += q[ii];
                    pq.push(q[ii]);
                }
                rep(ii,j){
                    tmp += q[n-1-ii];
                    pq.push(q[n-1-ii]);
                }
                rep(ii,l){
                    if(pq.empty()||pq.top()>=0)break;
                    tmp -= pq.top();
                    pq.pop();
                }
                if(tmp>res){
                DEBUG{printf("%d %d %d %d\n", i, j, l, tmp);}
                    res=tmp;
                }
                while (!pq.empty())pq.pop();
            }
        }
    }
    cout << res << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
a = min(n, k)
for i in range(a, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        m = k-i
        point = 0
        while v[point] < 0 and point < m:
            point += 1
        m = min(m, point)
        #print(take[m:])
        if take[m:] == []:
            sm = 0
        else:
            sm = sum(take[m:])
        if ans < sm:
            ans = sm
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for i in range(k, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        m = k-i
        point = 0
        while v[point] < 0:
            point += 1
        m = min(m, point)
        #print(take[m:])
        if take[m:] == []:
            sm = 0
        else:
            sm = sum(take[m:])
        if ans < sm:
            ans = sm
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for i in range(k, (k-1)//2, -1):
    for j in range(i+1):
        if i == j:
            take = v[:j]
        else:
            take = v[:j]+v[j-i:]
        #print([i,j])
        #print(take)
        take.sort()
        l = k-i
        m = 0
        while m < l or v[m] < 0:
            m += 1
        #print(take[m:])
        #print(l,m)
        sm = sum(take[m:])
        if ans < sm:
            ans = sm
print(ans)

Submission status: Runtime Error

Solution code:
import math, itertools, copy
 
# 入力受け取り
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

# N, K = 6, 4
# V = [-10, 8, 2, 1, 2, 6]
max = 0


# 操作上限をKとし、実際に操作する回数をk回とする
# k回の操作のうち、l回は取り出す操作、m回は詰める操作とする
# 詰める宝石がなくなってはいけないので、l >= m が成り立つ

for k in range(K + 1): # 操作する回数をkで表す
	for l in range((k + 1) // 2, k + 1): # k = l + m かつ　l >= m から、l >= k / 2　が言える
		for left in range(l + 1):
			# 初期化処理
			trg_V = copy.deepcopy(V)
			hand = []
			for i in range(left): # 左からleft回popする
				hand.append(trg_V.pop(0))
			for i in range(l - left): # 右からl - left回popする
				hand.append(trg_V.pop(-1))
			for i in range(k - l): # 手札を捨てる操作をm = k - l 回行う
				hand.pop(hand.index(min(hand)))
			total = sum(hand)
			# 最大値更新
			if max < total:
				max = total
				
print(max)

Submission status: Runtime Error

Solution code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(1, roopMax):
            for rightCount in range(0, roopMax - leftCount -1):
                leftList = baseList[0: leftCount]
                rightList = baseList[listCount - 1 - rightCount : listCount]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        handList.sort()
        for each in handList:
            if each < 0 and remainOpeCount > 0:
                removeCount += 1
                remainOpeCount -= 1
        
        maxVal = max(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
my.roop(baseList, opeCount)
print(my.maxNumber)

Submission status: Runtime Error

Solution code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(1, roopMax):
            for rightCount in range(0, roopMax - leftCount -1):
                leftList = baseList[0: leftCount - 1:]
                rightList = baseList[listCount - 1 - rightCount : listCount - 1]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        handList.sort()
        for each in handList:
            if each < 0 and remainOpeCount > 0:
                removeCount += 1
                remainOpeCount -= 1
        
        maxVal = max(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
my.roop(baseList, opeCount)
print(my.maxNumber)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(K):
    for j in range(K-i):
        now = 0
        if i + j > K:
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]


def solve(q, k, hand):
    if k == 0:
        return sum(hand)
    if len(hand) == 0:
        return min(solve(q[1:], k - 1, hand + [q[0]]), solve(q[:len(q) - 1], k - 1, hand + [q[len(q) - 1]]))
    return max(solve(q, k - 1, hand),
               solve(q[1:], k - 1, hand + [q[0]]),
               solve(q[:len(q) - 1], k - 1, hand + [q[len(q) - 1]]),
               solve([min(hand)] + q, k - 1, sorted(hand)[1:]),
               solve(q + [min(hand)], k - 1, sorted(hand)[:len(hand) - 1]))


print(solve(V, K, []))

Submission status: Runtime Error

Solution code:
import copy

n,m=map(int,input().split(' '))
li=list(map(int,input().split(' ')))

max_num=0

for i in range(m,0,-1):
  for j in range(i,0,-1):
    result=[]
    num=0
    for k in range(j):
      result.append(li[k])
      num+=li[k]
    for k in range(1,i-j+1):
      result.append(li[-k])
      num+=li[k]
      
    result.sort()
    n=0
    for k in range(len(result)):
      if n==(m-i):
        break
      if result[k]==min(result):
        result[k]=0
        n+=1
        
    if sum(result)>max_num:
      max_num=sum(result)
      
print(max_num)

Submission status: Runtime Error

Solution code:
import heapq
import copy

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


def dfs(q, have, count, operate):
    if operate == 0:
        heapq.heappush(have, q[0])
        q = q[1:]
    elif operate == 1:
        heapq.heappush(have, q[-1])
        q = q[:-1]
    elif operate == 2:
        sub = heapq.heappop(have)
        q.insert(0, sub)
    elif operate == 3:
        sub = heapq.heappop(have)
        q.append(sub)

    count += 1
    if count >= k:
        return sum(have)

    a = dfs(copy.deepcopy(q), copy.deepcopy(have), count, 0)
    b = dfs(copy.deepcopy(q), copy.deepcopy(have), count, 1)
    c = -float('inf')
    d = -float('inf')
    e = dfs(q, have, count, -1)  # 何もしない
    if len(have) > 0:
        c = dfs(copy.deepcopy(q), copy.deepcopy(have), count, 2)
        d = dfs(copy.deepcopy(q), copy.deepcopy(have), count, 3)

    return max(a, b, c, d, e)


print(dfs(v, [], -1, -1))

Submission status: Runtime Error

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

for left in range(k + 1):
    for right in range(k - left + 1):
        if left + right > n:
            break
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        for i in range(k - left - right):
            if 0 <= hold[i]:
                break
            total -= hold[i]
        M = max(M, total)

print(M)

Submission status: Runtime Error

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

for left in range(k + 1):
    for right in range(k - left + 1):
        if left + right > n:
            break
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        for i in range(min(k - left - right)):
            if 0 <= hold[i]:
                break
            total -= hold[i]
        M = max(M, total)

print(M)

Submission status: Runtime Error

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        for i in range(k - left - right):
            if i > len(hold):
                break
            if 0 <= hold[i]:
                break
            if hold[i] < 0:
                total -= hold[i]

        M = max(M, total)

print(M)

Submission status: Runtime Error

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

ans=0

for a in range(50):
  for b in range(50):
    if a+b <= min(n,k):
      li=v[:a]+v[n-b:]
      sumv = sum(li)
      li.sort()
      for i in range(min(k-a-b,len(li))):
        if li[i] < 0:
          li[i] = 0:
        else:
          break:
      ans=max(ans,sum(li))
      
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(" "))
v = [int(i) for i in input().split(" ")]
a,b,ans = 0,0,0

for i in range(k + 1):
    a = k - i
    b = i
    x,y = v[:a],[]
    if b != 0:
        y = v[-1 * b:]
    while(x != []):
        if min(x) >= 0:
            break
        elif max(x) <= 0:
            x = [0]
            break
        if min(x) == x[a-1] and min(x) < 0:
            x.pop()
            a -= 1
        elif abs(min(x)) >= abs(x[a-1]) and min(x) < 0:
            x.pop()
            x.remove(min(x))
            a -= 2
        else:
            break
    while(y != []):
        if min(y) >= 0:
            break
        elif max(y) <= 0:
            y =[0]
            break
        if min(y) == y[0] and min(y) < 0:
            y.pop(0)
        elif abs(min(y)) >=abs(y[0]) and min(y) < 0:
            y.pop(0)
            y.remove(min(y))
        else:
            break
    if ans < sum(x) + sum(y):
        ans = sum(x) + sum(y)

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(" "))
v = [int(i) for i in input().split(" ")]
a,b,ans = 0,0,0

for i in range(k + 1):
    a = k - i
    b = i
    x,y = v[:a],[]
    if b != 0:
        y = v[-1 * b:]
    while(x != []):
        if min(x) >= 0:
            break
        elif max(x) <= 0:
            x = [0]
            break
        if min(x) == x[a-1] and min(x) < 0:
            x.pop()
            a -= 1
        elif abs(min(x)) >= abs(x[a-1]) and min(x) < 0:
            x.pop()
            x.remove(min(x))
            a -= 2
        else:
            break
    while(y != []):
        if min(y) >= 0:
            break
        elif max(y) <= 0:
            y =[0]
            break
        if min(y) == y[0] and min(y) < 0:
            y.pop(0)
        elif abs(min(y)) >=abs(y[0]) and min(y) < 0:
            y.pop(0)
            y.remove(min(y))
        else:
            break
    if ans < sum(x) + sum(y):
        print(x,y)
        ans = sum(x) + sum(y)

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split(" "))
v = [int(i) for i in input().split(" ")]
a,b,ans = 0,0,0

for i in range(k + 1):
    a = k - i
    b = i
    x,y = v[:a],[]
    if b != 0:
        y = v[-1 * b:]
    while(x != []):
        if min(x) >= 0:
            break
        elif max(x) <= 0:
            x = [0]
            break
        if min(x) == x[a-1] and min(x) < 0:
            x.pop()
            a -= 1
        elif abs(min(x)) >= abs(x[a-1]) and min(x) < 0:
            x.pop()
            x.remove(min(x))
            a -= 2
        else:
            break
    while(y != []):
        if min(y) >= 0:
            break
        elif max(y) <= 0:
            y =[0]
        if min(y) == y[0] and min(y) < 0:
            y.pop(0)
        elif abs(min(y)) >=abs(y[0]) and min(y) < 0:
            y.pop(0)
            y.remove(min(y))
        else:
            break
    if ans < sum(x) + sum(y):
        print(x,y)
        ans = sum(x) + sum(y)

print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque

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

ans = 0
for i in range(k + 1):  # pop left
    for j in range(k + 1 - i):  # pop right
        d = deque(v)
        cur = 0
        a = []  # list for heap sort

        for _ in range(i):
            x = d.popleft()
            heapq.heappush(a, x)
            cur += x
            if not d:
                break

        for _ in range(j):
            x = d.pop()
            heapq.heappush(a, x)
            cur += x
            if not d:
                break

        sub = k - i - j  # rest trial num
        for _ in range(sub):
            if not a:
                break
            y = heapq.heappop(a)
            if y <= 0:
                cur -= y

        ans = max(ans, cur)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import collections


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    v = collections.deque([int(value) for value in scan().rstrip().split()])
    hands = [0] * n

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop())

            hands.sort()

            c_d = 0
            for c_d in range(min(n, k) - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break
            results.append(sum(hands))
            hands = [0] * n

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    v = collections.deque([int(value) for value in scan().rstrip().split()])
    hands = [0] * n

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop())

            hands.sort()

            c_d = 0
            for c_d in range(k - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break
            results.append(sum(hands))
            hands = [0] * n

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    v = collections.deque([int(value) for value in scan().rstrip().split()])
    hands = [0] * n

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop())

            hands.sort()

            c_d = 0
            for c_d in range(k - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break
            print(a, b, c_d)
            results.append(sum(hands))
            hands = [0] * n

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections


scan = sys.stdin.readline


def main():
    n, k = map(int, scan().rstrip().split())
    v = collections.deque([int(value) for value in scan().rstrip().split()])
    hands = []

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            for _ in range(a):
                hands.append(v.popleft())
            for _ in range(b):
                hands.append(v.pop(len(v) - 1))

            hands.sort()

            c_d = 0
            for c_d in range(k - a - b):
                if hands[c_d] < 0:
                    hands[c_d] = 0
                else:
                    break

            results.append(sum(hands))

    results.sort(reverse=True)
    print(results[0])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import heapq
from collections import deque

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

ans = 0
for i in range(k + 1):  # pop left
    for j in range(k + 1 - i):  # pop right
        d = deque(v)
        cur = 0
        a = []  # list for heap sort

        for _ in range(i):
            x = d.popleft() 
            heapq.heappush(a, x) 
            cur += x
            if not d:
                break

        for _ in range(j):
            x = d.pop()
            heapq.heappush(a, x)
            cur += x
            if not d:
                break

        sub = k - i - j  # rest trial num
        for _ in range(sub):
            if not a:
                break
            y = heapq.heappop(a)
            if y < 0:
                cur -= y

        ans = max(ans, cur)
print(ans)

Submission status: Runtime Error

Solution code:
def A(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(0)

def B(lst,hand):
    if len(lst) == 0:
        return
    else:
        return lst.pop(len(lst)-1)

def C(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.insert(0 , hand.pop( hand.index(min(hand)) ))

def D(lst,hand):
    if len(hand) == 0:
        return
    else:
        lst.append(hand.pop( hand.index(min(hand))) )

import random

if __name__ == "__main__":
    N,K = map(int, input().split())
    V = input().split()
    V = [int(i) for i in V]
    hand = []

    for l in range(10000):
        V_copy = [i for i in V]
        hand_copy = []
        for i in range(K):
            rand = random.randint(1,4)
            if rand == 1:
                hand_copy.append(A(V_copy,hand_copy))
            elif rand == 2:
                hand_copy.append(B(V_copy,hand_copy))
            elif rand == 3:
                C(V_copy,hand_copy)
            else:
                D(V_copy,hand_copy)
        hand.append(hand_copy)

    x = []
    for i in range(10000):
        x.append(sum(hand[i]))
    print(max(x))