Dynamic programming · 7 / 15 Exercise Hard

The 0/1 knapsack

Objective

You have a knapsack with limited capacity and items with weight and value. Each item is taken whole or not at all. The table dp[w] stores the best value with capacity w; walking it from high to low avoids using an item twice.

Instructions

  • The first line has n capacity (number of items and capacity); then n lines weight value.
  • Show Maximum value: x.
Input
3 50
10 60
20 100
30 120
Output
Maximum value: 220

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise