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):
[_, input_k] = map(int, input().split())

input_v = list(map(int, input().split()))

max_value = 0

for a in range(min(input_v, input_k + 1)):
    for b in range(min(input_v, input_k - a + 1)):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b 
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = sum(hold_jewelries[min(hold_minus, remove_length):])

        if (hold_value > max_value):
            max_value = hold_value

print(max_value)


[input_n, input_k] = map(int, input().split())

input_v = list(map(int, input().split()))

max_jewelries = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        print(hold_jewelries)
        remove_length = input_k - a - b 
        hold_jewelries = sorted(hold_jewelries)
        hold_minus = sum(1 for jewelry in hold_jewelries if jewelry < 0)
        hold_jewelries = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_jewelries) > max_jewelries):
            max_jewelries = sum(hold_jewelries)

print(max_jewelries)


[_, input_k] = map(int, input().split())

input_v = list(map(int, input().split()))

max_value = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b 
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_value) > max_value):
            max_jewelries = sum(hold_jewelries)

print(max_value)


[input_n, input_k] = map(int, input().split())

input_v = list(map(int, input().split()))

max_value = 0

for a in range(min(input_n, input_k) + 1):
    for b in range(min(input_n, input_k) - a + 1):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = sum(hold_jewelries[min(hold_minus, remove_length):])

        if (hold_value > max_value):
            max_value = hold_value

print(max_value)


Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Accepted