This is the classic 0/1 knapsack problem in disguise. It connects subset sum to DP and teaches the 1D optimization of 2D knapsack.
Enable JavaScript for the full StreamPrep guide.