Maximize Museum Tour Value With Energy Budget
Dynamic ProgrammingAuto-graded · 17 test casesLast updated
Tests the 0/1 knapsack variant where you allocate a fixed energy budget across museum exhibits with costs and values, but some exhibits are only accessible after visiting a prerequisite.
Museum Tour Planning
You are planning a self-guided museum tour. The museum has n exhibits numbered 0 through n-1. Each exhibit has:
- An energy cost
costs[i]— how much energy it takes to visit exhibiti. - A satisfaction value
values[i]— how much enjoyment you get from visiting it. - A prerequisite
prereq[i]— you may only visit exhibitiif you have already decided to visit exhibitprereq[i]in your tour plan. Ifprereq[i] == -1, exhibitihas no prerequisite and can be visited freely.
You start with budget units of energy. You want to choose a subset of exhibits to visit (each at most once) that:
- Respects all prerequisite constraints (if you include exhibit
iwithprereq[i] != -1, you must also include exhibitprereq[i]). - Has a total energy cost ≤ budget.
- Maximizes the total satisfaction value.
Return the maximum total satisfaction value achievable.
Prerequisite structure guarantee
The prerequisite graph is a forest (a collection of trees): each exhibit has at most one direct prerequisite, and there are no cycles. A prerequisite chain can be arbitrarily deep (e.g., exhibit 3 requires exhibit 1 which requires exhibit 0).
Constraints
1 ≤ n ≤ 2001 ≤ len(costs) == len(values) == len(prereq) == n1 ≤ costs[i] ≤ 501 ≤ values[i] ≤ 100prereq[i]is-1or a valid indexjwherej < i(prerequisites always have a smaller index, so the graph is a DAG)0 ≤ budget ≤ 10_000- If
budget == 0, the answer is0. - Multiple exhibits may share the same prerequisite (a node can have many children).
Examples
Example 1
Input: max_museum_satisfaction([3, 2, 5, 1], [10, 5, 15, 3], [-1, -1, 0, 0], 6)
Output: 18
Example 2
Input: max_museum_satisfaction([4, 3, 2, 6, 1], [8, 6, 4, 12, 2], [-1, 0, 0, 1, 1], 7)
Output: 14
Example 3
Input: max_museum_satisfaction([5, 5, 5], [10, 10, 10], [-1, 0, 1], 3)
Output: 0
Hints
Hint 1
Think of each tree in the forest as an independent subproblem. For a single tree rooted at node r, define dp[r][w] = max satisfaction from the subtree rooted at r using exactly w energy, where you MUST include r itself.
Hint 2
To combine a root node with its children subtrees, treat the children as independent knapsacks that you merge one by one into the root's DP table — this is the classic 'tree knapsack' or 'grouped knapsack on a tree' pattern.
Hint 3
After computing dp[r][w] for each root r in the forest, combine all roots with a final knapsack merge (roots have no mutual prerequisites, so they can be freely mixed).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a tree knapsack (also called "dependency knapsack"), a classic variant of 0/1 knapsack where items have prerequisite dependencies forming a forest.
Key Insight
Because prereq[i] < i always holds, the dependency graph is a forest of trees. Taking exhibit i forces us to also take every ancestor of i up to the root. We exploit this by doing a post-order DP on each tree.
Algorithm
Step 1 — Tree DP per subtree.
For each tree rooted at node r, define:
dp[r][w]= maximum satisfaction achievable by spending exactlywenergy within the subtree rooted atr, given thatritself is included.
Base case (leaf node): dp[r][costs[r]] = values[r], all other entries -∞.
Merging a child c into r's current DP:
- For every
(w1, w2)pair wheredp_current[w1] ≥ 0andchild_dp[w2] ≥ 0:- We can either skip the entire child subtree (
w2 = 0) or include some exhibits from it. new_dp[w1 + w2] = max(..., dp_current[w1] + child_dp[w2])
- We can either skip the entire child subtree (
This is equivalent to merging independent groups one by one — the "knapsack on a tree" standard technique.
Step 2 — Global knapsack over roots.
Root nodes have no prerequisites among themselves, so after computing each root's tree DP, we merge them with a standard 0/1 budget knapsack:
global_dp[w] = max satisfaction using at most w total energy across all trees
For each root tree, iterate the budget downward (or use a copy) to avoid double-counting.
Correctness
- The tree DP enforces that if you include any node, you pay the cost of every ancestor in that tree (since the root is always included when you enter a tree).
- Merging children one-by-one is correct because sibling subtrees are independent given the parent is chosen.
- The final knapsack over roots is independent (no cross-tree prerequisites).
Complexity
Let n = number of exhibits, B = budget, C = max total cost of any single tree (≤ n × max_cost).
- Tree DP: For each node, merging one child of subtree size
s_cinto a current DP of sizes_curtakesO(s_cur × s_c)time. Summed over the whole tree, this isO(s_tree²)per tree (classic result for tree knapsack). Total:O(n² × max_cost²)in the worst case, but practically much smaller. - Global merge:
O(R × B)whereRis the number of roots, bounded byO(n × B). - Space:
O(n × max_cost)for DP arrays.
Given n ≤ 200, costs[i] ≤ 50, and budget ≤ 10,000, this runs comfortably within time limits.
from typing import List
def max_museum_satisfaction(costs: List[int], values: List[int], prereq: List[int], budget: int) -> int:
n = len(costs)
if budget == 0 or n == 0:
return 0
# Build children list
children = [[] for _ in range(n)]
roots = []
for i in range(n):
if prereq[i] == -1:
roots.append(i)
else:
children[prereq[i]].append(i)
def tree_dp(node: int) -> List[int]:
c = costs[node]
v = values[node]
dp = [-1] * (c + 1)
dp[c] = v
for child in children[node]:
child_dp = tree_dp(child)
child_max = len(child_dp) - 1
new_size = len(dp) - 1 + child_max
new_dp = [-1] * (new_size + 1)
for w1 in range(len(dp)):
if dp[w1] < 0:
continue
if new_dp[w1] < dp[w1]:
new_dp[w1] = dp[w1]
for w2 in range(1, child_max + 1):
if child_dp[w2] < 0:
continue
total_w = w1 + w2
if new_dp[total_w] < dp[w1] + child_dp[w2]:
new_dp[total_w] = dp[w1] + child_dp[w2]
dp = new_dp
return dp
global_dp = [0] * (budget + 1)
for root in roots:
tree = tree_dp(root)
new_global = global_dp[:]
for w_tree in range(1, len(tree)):
if tree[w_tree] < 0:
continue
for w_prev in range(budget - w_tree + 1):
candidate = global_dp[w_prev] + tree[w_tree]
if new_global[w_prev + w_tree] < candidate:
new_global[w_prev + w_tree] = candidate
global_dp = new_global
return max(global_dp)