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):
maxi = 0
for i in range(N):
    for o in range(i):
        fs = 0
        bs = 0
        hand = []
        for p in range(o):
            fs += V[p]
            hand.append(V[p])
        for p in range(i-o, N):
            bs += V[p]
            hand.append(V[p])
        ts = fs + bs
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
        for j in range(K-i):
            if hand[0] <= 0:
                del hand[0]
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

maximum = 0
for u in range(K):
    for i in u:
        frontSum = 0
        frontList = []
        backSum = 0
        backList = []
        for o in range(i):
            frontSum += V[o]
            frontList.append(V[o])
        for o in range(len(V)-(K-i), len(V)):
            backSum += V[o]
            backList.append(V[o])
        total = frontSum + backSum
        if total > maximum:
            maximum = total
        frontList.sort()
        backList.sort()
        for p in range(K-u):
            frontList = frontLIst[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
            for j in range(K-u-p):
                backList = backList[p:]
                newBackSum = 0
                for k in backList:
                    nweBackSum += k
                if newBackSum > backSum:
                    backSum = newBackSum
        newTotal = backSum + frontSum
        if newTotal > maximum:
            maximum = newTotal
print(maximum)


maximum = float("-inf")
for i in range(K): # gets the sum of first i numbers + last K-i numbers
    frontSum = 0
    frontList = []
    backSum = 0
    backList = []
    for o in range(i):
        frontSum += V[o]
        frontList.append(V[o])
    for o in range(len(V)-(K-i-1), len(V)):
        backSum += V[o]
        backList.append(V[o])
    total = frontSum + backSum
    if total > maximum:
        maximum = total
    frontList.sort()
    backList.sort()
    for p in range(2, i, 2):
        for j in range(p):
            frontList = frontList[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
    for p in range(2, K-i, 2):
        for j in range(p):
            backList = backList[p:]
            newBackSum = 0
            for k in backList:
                newBackSum += k
            if newBackSum > backSum:
                backSum = newBackSum
    newTotal = backSum + frontSum
    if newTotal > maximum:
        maximum = newTotal
print(maximum)

maximum = float("-inf")
for i in range(K):  # gets the sum of first i numbers + last K-i numbers
    frontSum = 0
    frontList = []
    backSum = 0
    backList = []
    for o in range(i):
        frontSum += V[o]
        frontList.append(V[o])
    for o in range(len(V)-(K-i), len(V)):
        backSum += V[o]
        backList.append(V[o])
    total = frontSum + backSum
    if total > maximum:
        maximum = total
    frontList.sort()
    backList.sort()
    for p in range(2, len(frontList), 2):
        for j in range(p):
            frontList = frontList[p:]
            newFrontSum = 0
            for k in frontList:
                newFrontSum += k
            if newFrontSum > frontSum:
                frontSum = newFrontSum
    for p in range(2, len(backList), 2):
        for j in range(p):
            backList = backList[p:]
            newBackSum = 0
            for k in backList:
                newBackSum += k
            if newBackSum > backSum:
                backSum = newBackSum
    newTotal = backSum + frontSum
    if newTotal > maximum:
        maximum = newTotal
print(maximum)


N, K = map(int, input().strip().split())
V = list(map(int, input().strip().split()))
H = []

maxi = 0
for i in range(N):
    for o in range(i):
        fs = 0
        bs = 0
        hand = []
        for p in range(o):
            fs += V[p]
            hand.append(V[p])
        for p in range(i-o, N):
            bs += V[p]
            hand.append(V[p])
        ts = fs + bs
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
            for j in range(K-i):
                if hand[0] <= 0:
                    del hand[0]
                else:
                    break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

N, K = map(int, input().strip().split())
V = list(map(int, input().strip().split()))
H = []
maxi = 0
for i in range(min(N, K)):
    for o in range(i+2):  
        ts = 0
        hand = []
        for p in range(o):
            hand.append(V[p])
        for p in range(N-(i-o)-1, N):  
            hand.append(V[p])
        for values in hand:
            ts += values
        if ts > maxi:
            maxi = ts
        if len(hand) > 0:
            hand.sort()
            for j in range(K-i-1):  
                if len(hand) > 0:
                    if hand[0] < 0:
                        del hand[0]
                    else:
                        break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted