Problem: 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.

Submission attempts (in order):
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans = sum(chosen[k-i:i])
            if tmp_ans > 0:
                tmp_ans2 = sum(filter(lambda x: x > 0, chosen))
                tmp_ans = max(tmp_ans, tmp_ans2)

        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans1 = sum(chosen[k-i:i])
            tmp_ans2 = sum(filter(lambda x: x > 0, chosen))
            tmp_ans = max(tmp_ans1, tmp_ans2)

        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = 0

for i in range(1, min(n, k)+1, 1):
    for j in range(i+1):
        l = j
        r = n-(i-j)

        d = data[:l]
        d += data[r:]
        d.sort()

        del_num = min(k-i, i)
        for l in range(del_num):
            if d[l] >= 0:
                del_num = l
                break
        ans = max(ans, sum(d[del_num:]))

print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted