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:
import sys
from collections import deque

fin = sys.stdin.readline
N, K = [int(elem) for elem in fin().split()]

v_list = [int(elem) for elem in fin().split()]

sum_list = []
for k in range(K+1):
    for p in range(k+1):
        for left in range(k-p+1):
            for right in range(k-p-left+1):
                if (left + right) < p:
                    continue
                each_v_list = v_list[:left] + list(deque(v_list, right))
                each_v_list = sorted(each_v_list)
                each_v_list = each_v_list[p:]
                each_sum = sum(each_v_list)
                sum_list.append(each_sum)
print(max(max(sum_list), 0))

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
V_lis = [int(_) for _ in input().split()]

point_lis = []

for k in range(1, K+1):
  
  val_lis = []
  point = 0
  
  for left_k in range(1, k+1):
    val_lis = V_lis[:left_k-1]
    if left_k < k:
      val_lis += V_lis[-(k-left_k):]

    point = sum(val_lis)
    num_of_neg = [_ for _ in val_lis if _ < 0]
    num_of_neg.sort()
  
    diff = K - k + 1
    if diff >= len(num_of_neg):
      point -= sum(num_of_neg)
    elif diff > 0:
      for x in range(diff):
        point -= num_of_neg[-x]
      
    point_lis.append(point)
  
print(max(point_lis))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split(" ")]
D = [int(x) for x in input().split(" ")]
i = 0
j = len(D) - 1
memo = dict()

hands = set()

steps = 0

def maxSum(d, hands, k):
    if k == 0:
        return sum(hands)

    key = "{}::{}::{}".format(str(d), str(min(hands) if hands else 'none'), k)
    if key in memo:
        return memo[key]
    
    patterns = []

    if len(d) > 0:
        el = d[0]
        patterns.append(maxSum(d[1:], hands.union(set([el])), k - 1))
        if len(d) > 1:
            el = d[-1]
            patterns.append(maxSum(d[:-1], hands.union(set([el])), k - 1))

    if hands:
        m = min(hands)
        newHand = hands.difference(set([m]))
        patterns.append(maxSum([m] + d, newHand, k - 1))
        patterns.append(maxSum(d + [m], newHand, k - 1))

    res = max(patterns)
    memo[key] = res
    # global steps
    # steps += 1
    # print("HELLO", steps)
    return res

print(maxSum(D, set(), K))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

fin = sys.stdin.readline
N, K = [int(elem) for elem in fin().split()]

v_list = [int(elem) for elem in fin().split()]

sum_list = []
for k in range(K):
    for p in range(K+1):
        for left in range(K-p+1):
            for right in range(K-p-left+1):
                if (left + right) < p:
                    continue
                each_v_list = v_list[:left] + list(deque(v_list, right))
                each_v_list = sorted(each_v_list)
                each_v_list = each_v_list[p:]
                each_sum = sum(each_v_list)
                sum_list.append(each_sum)
print(max(max(sum_list), 0))

Submission status: Wrong Answer

Solution code:

def get_data(data_left, nokori):
    # print(data_left, nokori)
    while 1:
        if len(data_left) == 0:
            break
        if data_left[-1] <= 0:
            data_left.pop(-1)
            nokori += 1
        else:
            break

    # print(data_left, nokori)

    while nokori > 0:
        if len(data_left) == 0:
            break
        min_data = min(data_left)
        if min_data < 0:
            data_left.pop(data_left.index(min_data))
            nokori -= 1
        else:
            break
        # print('uuu', data_left)

    # print(data_left)

    while 1:
        if len(data_left) == 0:
            break
        saigo = data_left[-1]
        min_data = min(data_left)

        if saigo + min_data < 0:
            data_left.pop(-1)

            if len(data_left) == 0:
                nokori += 1
                break
            if min_data not in data_left:
                nokori += 1
            else:
                # print(data_left.index(min_data))
                data_left.pop(data_left.index(min_data))
        else:
            break

    return nokori, data_left

def main():

    num_houseki, num_sousa = map(int, input().split())
    data = list(map(int, input().split()))


    ans = 0


    # dp = defaultdict(int)

    for i in range(num_sousa + 1):

        nokori = 0
        left_ind = min(i, num_houseki)
        nokori += i - left_ind
        righ_ind = max(left_ind, num_houseki - num_sousa + i)
        nokori += num_sousa - i - num_houseki + righ_ind
        data_left = data[:left_ind]
        data_right = data[righ_ind:]

        # print('<><<><<><')
        # print(data_left)
        # print(data_right)

        pre_left, pre_right = data_left.copy(), data_right.copy()

        while 1:
            nokori, data_left = get_data(data_left, nokori)
            nokori, data_right = get_data(data_right[::-1], nokori)
            data_right = data_right[::-1]

            if pre_left == data_left and pre_right == data_right:
                break
            else:
                pre_left, pre_right = data_left.copy(), data_right.copy()




        # print('----')
        # print(data_left)
        # print(data_right)

        kari = sum(data_right) + sum(data_left)
        if kari > ans:
            ans = kari

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ans = 0
for i in range(K+1):
    for j in range(K-i+1):
        tmp = V[:i] + V[(N-j):]
        tmp.sort()
        for k, t in enumerate(tmp):
            if t>=0 or k+1 > K-i-j:
                ans = max(sum(tmp[k:]), ans)  
                break

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = [int(v) for v in input().split()]

    m = 0
    for i in range(K):
        for j in range(K-i):
            if j == 0:
                a = V[:K-i]
            else:
                a = V[:K-i-j] + V[-j:]
            a.sort()
            s = sum(a[i:])
            if s > m:
                m = s

    print(m)

main()

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heappop, heappush

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    ans = 0
    L = [0] * (K+1)
    R = [0] * (K+1)
    for i in range(N):
        left = V[:i]
        right = V[::-1][:N-i]

        L = [0] * (K+1)
        R = [0] * (K+1)

        for j in range(i):
            left_heap = []
            for v in left[:j]:
                heappush(left_heap, v)

            cnt = j
            while cnt < K and len(left_heap) > 0:
                v = heappop(left_heap)
                if v > 0:
                    heappush(left_heap, v)
                    break

                cnt += 1

            if cnt <= K:
                L[cnt] = max(sum(left_heap), L[cnt])


        for j in range(N-i):

            right_heap = []
            for v in right[:j]:
                heappush(right_heap, v)

            cnt = j
            while cnt < K and len(right_heap) > 0:
                v = heappop(right_heap)
                if v > 0:
                    heappush(right_heap, v)
                    break

                cnt += 1

            if cnt <= K:
                R[cnt] = max(sum(right_heap), R[cnt])

    
        R = R[::-1]
        for left, right in zip(L, R):
            ans = max(ans, left + right)

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
Vi = [i+1 for i in range(len(V))]
V.extend(V)
Vi.extend(Vi)
ans = 0
for k in range(1, min(K+1, N+1)):
    for s in range(len(V)):
        part_Vi = Vi[s:s+k]
        if 1 not in part_Vi and N not in part_Vi:
            continue
        can_remove = K - k - 1
        part_V = V[s:s+k]
        part_V = sorted(part_V)
        plus_i = 0
        for v in part_V:
            if can_remove <= 0:
                break
            if v < 0:
                plus_i += 1
                can_remove -= 1
            else:
                break
        ans = max(ans, sum(part_V[plus_i:]))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 26 22:14:45 2019

@author: maezawa
"""
#from collections import deque
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
#v = deque(list(map(int, input().split())))

for left in range(k):
    right = k - left
    larr = v[:left]
    rarr = v[n-right:]
    if len(larr)>0 and larr[-1] < 0:
        larr.pop()
    if len(rarr)>0 and rarr[0] < 0:
        rarr.pop(0)
    larr.sort()
    rarr.sort()
    while len(larr)>1:
        if larr[0]+larr[1] < 0:
            larr = larr[2:]
        else:
            break
    while len(rarr)>1:
        if rarr[0]+rarr[1] < 0:
            rarr = rarr[2:]
        else:
            break
ans = sum(larr)+sum(rarr)
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify

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

# N jewels in dequeue D
# at most K operations
# 1 <= N <= 50
# 1 <= K <= 100
# -10000000 <= Vi <= 10000000

# Operation A, B: Remove left or right
# Operation C, D: Add to left or right

S = 0
for take in range(K + 1):
    for takeL in range(take + 1):
        for takeR in range(take - takeL + 1):
            hand = []
            for i in range(takeL):
                if i >= len(V):
                    break
                heappush(hand, V[i])
            for i in range(takeR):
                if N - i - 1 < 0 or N - i - 1 >= len(V):
                    break
                heappush(hand, V[N- i - 1])
            S = max(sum(hand), S)
            for i in range(K - take):
                if not hand or hand[0] >= 0:
                    break
                heappop(hand)
                S = max(sum(hand), S)
print(S)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
V = [int(x) for x in input().split()]
ans = 0

if N >= K:
    a = K+1
else:
    a = N+1
for i in range(1,a)[::-1]:
    Vn = V[:i]
    Vns = Vn
    for k in range(K-i):
        if len(Vns)==0:
            Vns += [0]
            break 
        if min(Vns) < 0:
            Vns.pop(Vns.index(min(Vns)))
        else: break
    val = sum(Vns)
    if ans < val:   ans = val
    
    for j in range(1,i+1):
        Vn = [V[-j]] + Vn[:-1]
        Vns = Vn
        for k in range(K-i):
            if len(Vns)==0:
                Vns += [0]
                break 
            if min(Vns) < 0:
                Vns.pop(Vns.index(min(Vns)))
            else: break
        val = sum(Vns)
        if ans < val:   ans = val


print(max(0,ans))

Submission status: Wrong Answer

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

left = np.cumsum([0] + v)
right = np.cumsum([0] + v[::-1])

results = []
for backn in range(k + 1):
    at = k - backn

    t = min(n, at)
    # print('----', backn, t)
    for shift in range(t + 1):
        p = v[:t - shift] + v[n - shift:]
        # print(v[:t - shift], v[n - shift:])
        p.sort()
        value = sum(p[backn + 1:])
        results.append(value)
print(max(results + [0]))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def uy():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,m = uy()

a = l()

su = 0
for i in range(n):
	ll = max(i,n-(m - i))
	for j in range(ll,n):
		q = []
		co = 0
		p = a[:i]
		pp = a[j:]
		for k in range(len(p)):
			if p[k] < 0:
				q.append(p[k])
		for k in range(len(pp)):
			if pp[k] < 0:
				q.append(pp[k])
		co += sum(p)
		co += sum(pp)
		q.sort()
		for k in range(min(len(q),j-ll)):
			co += abs(q[k])
		su = max(su,co)
print(su)

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
V_lis = [int(_) for _ in input().split()]

point_lis = []

for k in range(1, K+1):
  
  val_lis = []
  point = 0
  
  for left_k in range(1, k+1):
    val_lis = V_lis[:left_k-1]
    if left_k < k:
      val_lis += V_lis[-(k-left_k):]

    print(val_lis)
    point = sum(val_lis)
    num_of_neg = [_ for _ in val_lis if _ < 0]
    num_of_neg.sort()
  
    diff = K - k
    if diff >= len(num_of_neg):
      point -= sum(num_of_neg)
    elif diff > 0:
      for x in range(diff):
        point -= num_of_neg[-x]
      
    point_lis.append(point)
  
print(max(point_lis))

Submission status: Wrong Answer

Solution code:

def get_data(data_left, nokori):
    # print(data_left, nokori)
    while 1:
        if len(data_left) == 0:
            break
        if data_left[-1] <= 0:
            data_left.pop(-1)
            nokori += 1
        else:
            break

    # print(data_left, nokori)

    while nokori > 0:
        if len(data_left) == 0:
            break
        min_data = min(data_left)
        if min_data < 0:
            data_left.pop(data_left.index(min_data))
            nokori -= 1
        else:
            break
        # print('uuu', data_left)

    # print(data_left)

    while 1:
        if len(data_left) == 0:
            break
        saigo = data_left[-1]
        min_data = min(data_left)

        if saigo + min_data < 0:
            data_left.pop(-1)

            if len(data_left) == 0:
                nokori += 1
                break
            if min_data not in data_left:
                nokori += 1
            else:
                # print(data_left.index(min_data))
                data_left.pop(data_left.index(min_data))
        else:
            break

    return nokori, data_left

def main():

    num_houseki, num_sousa = map(int, input().split())
    data = list(map(int, input().split()))


    ans = 0


    # dp = defaultdict(int)

    for i in range(num_sousa + 1):

        nokori = 0
        left_ind = min(i, num_houseki)
        nokori += i - left_ind
        righ_ind = max(left_ind, num_houseki - num_sousa + i)
        nokori += num_sousa - i - num_houseki + righ_ind
        data_left = data[:left_ind]
        data_right = data[righ_ind:]

        # print('<><<><<><')
        # print(data_left)
        # print(data_right)


        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]



        # print('----')
        # print(data_left)
        # print(data_right)

        kari = sum(data_right) + sum(data_left)
        if kari > ans:
            ans = kari

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc128/tasks/abc128_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect

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

    ans = 0
    # print(D)
    for k in range(K+1):
        front_max = 0
        for pushk in range(k):
            front_index = min(N, k-pushk)
            k_front = D[:front_index]
            # print('k_front = {}'.format(k_front))

            k_front.sort()
            cc_k = k_front[pushk:]
            front_max = max(front_max, max(sum(k_front), sum(cc_k)))

        back_max = 0
        back = K - k
        # print(back)
        for backk in range(back):
            back_index = min(N, N - (back - backk))
            k_back = D[back_index:]
            # print('k_back = {}, back_index = {}'.format(k_back, back_index))
            k_back.sort()
            cc_k = k_back[backk:]
            back_max = max(back_max, max(sum(k_back), sum(cc_k)))

        # print('front = {}, back = {}'.format(front_max, back_max))

        ans = max(ans, front_max + back_max)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in range(n):
    cum[i+1] = cum[i] + v[i]

ans = 0
cnt = 0
for i in range(n):
    for j in range(n):
        # if i > j:
        #     continue
        l = (j - i + 1)
        if n - l > k:
            continue
        a = v[:i] + v[j+1:]
        b = a[:]
        a.sort()
        tmp = cum[n] - (cum[j+1] - cum[i])
        tmp1 =tmp
        for x in range(k - (n-l)):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])
        # print('{}:{}:{}'.format(b, tmp1, tmp))
        cnt += 1
ans = max([ans, v[0], v[-1]])
if k >= 3 and n >= 2:
    ans = max([ans, v[1], v[-2]])
if k >= 4 and n >= 3:
    ans = max([ans, sum(v[1:3]), sum(v[-3:-1])])
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(k+1):
    tmp = []
    for j in range(i+1):
        choice = v[:j] + v[-(i-j):]
        if i != j:
            if choice not in tmp:
                tmp.append(choice)
        else:
            tmp.append(v[:j])

    for cho in tmp:
        cho.sort()
        cho.reverse()
        for l in range(k-i+1):
            ans = max(ans, sum(cho[:l]))

print(ans)

Submission status: Wrong Answer

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

q =0
v =0
de = []
de.append(0)
Vr=[]
Vr.append(0)
for i in range(K):
    if q==N:
        break
    if V[q]>=0 and V[q]>=de[0]:
        v+=V[q]
        Vr.append(v)
        q+=1
    elif V[q]>=0 and V[q]<de[0]:
        v+=de[0]
        de[0]=0
        de = sorted(de,reverse=True)
        Vr.append(v)
    elif V[q]<0 and de[0]==0:
        de.append(abs(V[q]))
        sorted(de,reverse=True)
        v-=de[0]
        de = sorted(de,reverse=True)
        Vr.append(v)
        q+=1
    elif abs(V[q])>de[0]:
        v+=de[0]
        de[0]=0
        de = sorted(de,reverse=True)
        Vr.append(v)
    else:
        de.append(abs(V[q]))
        v-=abs(V[q])
        de = sorted(de,reverse=True)
        q+=1
q =0
v =0
de = []
de.append(0)
Vl=[]
Vl.append(0)
for i in range(K):
    if q==N:
        break
    if V[N-1-q]>=0 and V[N-1-q]>=de[0]:
        v+=V[N-1-q]
        Vr.append(v)
        q+=1
    elif V[N-1-q]>=0 and V[N-1-q]<de[0]:
        v+=de[0]
        de[0]=0
        de = sorted(de,reverse=True)
        Vr.append(v)
    elif V[N-1-q]<0 and de[0]==0:
        de.append(abs(V[N-1-q]))
        sorted(de,reverse=True)
        v-=de[0]
        de = sorted(de,reverse=True)
        Vr.append(v)
        q+=1
    elif abs(V[N-1-q])>de[0]:
        v+=de[0]
        de[0]=0
        de = sorted(de,reverse=True)
        Vr.append(v)
    else:
        de.append(abs(V[N-1-q]))
        v-=abs(V[N-1-q])
        de = sorted(de,reverse=True)
        q+=1
if len(Vr)<K+1:
    for i in range(K+1-len(Vr)):
        Vr.append(0)
if len(Vl)<K+1:
    for i in range(K+1-len(Vl)):
        Vl.append(0)
for i in range(K+1):
    Vr[i]+=Vl[K-i]
print max(Vr)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split(" ")]
D = [int(x) for x in input().split(" ")]
i = 0
j = len(D) - 1
memo = dict()

hands = set()

steps = 0

def maxSum(d, hands, k):
    if k == 0:
        return sum(hands)

    key = "{}::{}::{}".format(str(d), str(min(hands) if hands else 'none'), k)
    if key in memo:
        return memo[key]
    
    patterns = [sum(hands)]

    if len(d) > 0:
        el = d[0]
        patterns.append(maxSum(d[1:], hands.union(set([el])), k - 1))
        if len(d) > 1:
            el = d[-1]
            patterns.append(maxSum(d[:-1], hands.union(set([el])), k - 1))

    if hands:
        m = min(hands)
        newHand = hands.difference(set([m]))
        patterns.append(maxSum([m] + d, newHand, k - 1))
        patterns.append(maxSum(d + [m], newHand, k - 1))

    res = max(patterns)
    memo[key] = res
    # global steps
    # steps += 1
    # print("HELLO", steps)
    return res

print(maxSum(D, set(), K))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
now = 0
for i in range(k+1):
  for j in range(0,min(n,k-i+1)):
    l = k-(i+j)
    li = lis[:i]
    if j > 0:
      li += lis[-j:]
    li.sort()
    for h in range(l):
      try:
        if min(li) < 0:
          li.remove(min(li))
      except:
        continue
    ans = max(ans,sum(li))
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()
  l2=l[:]
  l2.reverse()

  mx=-inf

  for i in range(-(-k//2),k+1):
    for j in range(k-i+1):
      # print(i,j)
      for k in range(1):
        if k==0:
          t1=l[:i]
          t2=l2[-j:]
          # print(t1)
          # print(t2)
          t=t1+t2
          t.sort()
          for p in range(k-(i+j)):
            t.remove(t[0])
          mx=max(mx,sum(t))
          # print(mx)
        else:
          t2=l[:i]
          t1=l2[-j:]
          # print(t1)
          # print(t2)
          t=t1+t2
          t.sort()
          for p in range(k-(i+j)):
            t.remove(t[0])
          mx=max(mx,sum(t))
          # print(mx)
        # print(mx)

  return mx*2


print(main())

Submission status: Wrong Answer

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

max = 0
for i in range(k):
    for j in range(i+1):
        sp = i - j
        msp = -j
        if msp == 0:
            msp = len(v_)
        d = []
        d.extend(v_[:sp])
        d.extend(v_[msp:])
        d.sort()
        retmax = sum(d)
        for p in range(min(len(d), k-i-1)):
            if d[p] < 0:
                retmax -= d[p]
        if retmax > max:
            max = retmax
print(max)