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):
from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []

def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, k):
    i, j, k_ = node
    if i+j+k_ < k:
        return ((i+1, j, k_), (i, j+1, k_), (i, j, k_+1))
    return tuple()

def function(node, ans, W, V, memo):
    if node in memo:
        return memo[node]
    
    i, j, k = node
    W_, V_ = map(deepcopy, (W, V))
    for _ in range(i):
        if V_:
            heappush(W_, V_.pop())
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    for _ in range(k):
        if W_:
            _ = heappop(W_)
    memo[node] = max(ans, sum(W_))
    return memo[node]

ans = dfs((0, 0, 0), partial(option, k=k), partial(function, W=W, V=V, memo={}))
print(ans)

from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []

def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, limit):
    i, j = node
    if i+j < limit:
        return ((i+1, j), (i, j+1))
    return tuple()

def function(node, ans, W, V, limit, memo):
    if node in memo:
        return max(ans, memo[node])
    i, j = node
    W_, V_ = map(deepcopy, (W, V))
    for _ in range(i):
        if V_:
            heappush(W_, V_.pop())
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    score = sum(W_)
    for _ in range(min(limit-i-j, i+j)):
        if W_ and W_[0] < 0:
            score -= heappop(W_)
    memo[node] = score
    print(i, j, min(limit-i-j, i+j), memo[node])
    return max(ans, memo[node])

ans = dfs((0, 0), partial(option, limit=min(n, k)), partial(function, W=W, V=V, limit=k, memo={}))
print(ans)

from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []


def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, k):
    i, j = node
    if i+j < k:
        return ((i+1, j), (i, j+1))
    return tuple()

def function(node, ans, W, V, k, memo):
    if node in memo:
        return max(ans, memo[node])

    i, j = node
    W_, V_ = get_W_V(i)
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    score = sum(W_)
    for _ in range(min(k-i-j, i+j)):
        if W_ and W_[0] < 0:
            score -= heappop(W_)
    memo[node] = score
    return max(ans, memo[node])

ans = dfs((0, 0), partial(option, k=k), partial(function, W=W, V=V, k=k, memo={}))
print(ans)

n, k, *V = map(int, open(0).read().split())
def calc(i, j, k):
    W = sorted(V[:i] + V[:-j-1:-1], reverse=True)
    for _ in range(k):
        if W[-1] < 0:
            W.pop()
    return sum(W)

ans = 0
r = min(n, k)+1
for i in range(r):
    for j in range(r-i):
        for k_ in range(min(i+j, k+1-i-j)):
            ans = max(ans, calc(i, j, k_))
print(ans)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted