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
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()


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()



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()


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()


import sys
import collections
import copy


scan = sys.stdin.readline


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

    results = []

    for a in range(min(n, k) + 1):
        for b in range(min(n, k) - a + 1):
            hands = [0] * n
            v = copy.deepcopy(values)

            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()


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted