Given n items with weights and values, and a knapsack capacity, return the maximum total value you can achieve by picking each item at most once. This is the classic 0/1 knapsack formulation and the base of many partition-style DP problems.
weights = item weights, values = item values, capacity = knapsack capacity
maximum total value without exceeding capacity
Example 1:
Input:
weights = [1,3,4,5] values = [1,4,5,7] capacity = 7
Output:
9
Explanation:
Take items with weights 3 and 4 for a total value of 9.
Example 2:
Input:
weights = [2,3,4,5] values = [3,4,5,6] capacity = 5
Output:
7
Explanation:
Take items with weights 2 and 3 for total value 7.