Write a function to implement the Knapsack problem. The function should take in a list of items,
Question:
Write a function to implement the Knapsack problem. The function should take in a list of items, each with a weight and a value, and a maximum weight capacity, and return the maximum total value that can be obtained by selecting items such that the total weight of the items is less than or equal to the maximum weight capacity. The function should first sort the items by value-to-weight ratio and then use dynamic programming to solve the problem.
Example:
Input:
items = [
{"name": "item1", "weight": 2, "value": 6},
{"name": "item2", "weight": 2, "value": 10},
{"name": "item3", "weight": 3, "value": 12},
{"name": "item4", "weight": 5, "value": 20},
{"name": "item5", "weight": 9, "value": 22}
]
max_weight = 20
Output: 36
Explanation: The maximum total value that can be obtained by selecting items such that the total weight is less than or equal to 20 is 36, which is obtained by selecting "item2" and "item4".
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest