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 sys


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

    
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K + 1):
        if A < K:
            for B in range(K - A + 1):
                left_jewels = V_A.copy()[:A]
                right_jewels = V_B.copy()[:B]

                if len(left_jewels) >= len(right_jewels):
                    jewels = left_jewels
                    jewels.extend(right_jewels)
                else:
                    jewels = right_jewels
                    jewels.extend(left_jewels)
                jewels.sort()

                score = sum(jewels)

                for i in range(min(K - A - B + 1, len(jewels))):
                    jewel = jewels[i]
                    if jewel < 0:
                        score -= jewel
                    else:
                        break
                ret = max(ret, score)
        else:
            jewels = V_A.copy()[:A]
            score = sum(jewels)
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

import sys


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

    
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(min(K + 1, N + 1)):
        for B in range(min(K - A + 1, N - A + 1)):
            left_jewels = V_A.copy()[:A]
            right_jewels = V_B.copy()[:B]

            if len(left_jewels) >= len(right_jewels):
                jewels = left_jewels
                jewels.extend(right_jewels)
            else:
                jewels = right_jewels
                jewels.extend(left_jewels)
            jewels.sort()

            score = sum(jewels)

            for i in range(min(K - A - B, len(jewels))):
                jewel = jewels[i]
                if jewel < 0:
                    score -= jewel
                else:
                    break
                    
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Accepted