Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the items such that sum of the weights of those items of given subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).

**Prerequisite :** 0/1 Knapsack

Examples :

Input : val[] = {60, 100, 120}; wt[] = {10, 20, 30}; W = 50; Output : 220 //maximum value that can be obtained 30 20 //weights 20 and 30 are included. Input : val[] = {40, 100, 50, 60}; wt[] = {20, 10, 40, 30}; W = 60; Output : 200 30 20 10

**Approach :**

Let val[] = {1, 4, 5, 7}, wt[] = {1, 3, 4, 5}

W = 7.

The 2d knapsack table will look like :

Start backtracking from K[n][W].Here K[n][W] is 9.

Since, this value comes from the top (shown by grey arrow), the item in this row is not included. Go vertically upward in the table without including this in the knapsack. Now, this value K[n-1][W] which is 9 doesn’t come from the top which means the item in this row is included and go vertically up and then left by the weight of the included item ( shown by black arrow). Continuing this process include weights 3 and 4 with total value 9 in the knapsack.

## C++

`// CPP code for Dynamic Programming based ` `// solution for 0-1 Knapsack problem ` `#include <bits/stdc++.h> ` ` ` `// A utility function that returns maximum of two integers ` `int` `max(` `int` `a, ` `int` `b) { ` `return` `(a > b) ? a : b; } ` ` ` `// Prints the items which are put in a knapsack of capacity W ` `void` `printknapSack(` `int` `W, ` `int` `wt[], ` `int` `val[], ` `int` `n) ` `{ ` ` ` `int` `i, w; ` ` ` `int` `K[n + 1][W + 1]; ` ` ` ` ` `// Build table K[][] in bottom up manner ` ` ` `for` `(i = 0; i <= n; i++) { ` ` ` `for` `(w = 0; w <= W; w++) { ` ` ` `if` `(i == 0 || w == 0) ` ` ` `K[i][w] = 0; ` ` ` `else` `if` `(wt[i - 1] <= w) ` ` ` `K[i][w] = max(val[i - 1] + ` ` ` `K[i - 1][w - wt[i - 1]], K[i - 1][w]); ` ` ` `else` ` ` `K[i][w] = K[i - 1][w]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// stores the result of Knapsack ` ` ` `int` `res = K[n][W]; ` ` ` `printf` `(` `"%d\n"` `, res); ` ` ` ` ` `w = W; ` ` ` `for` `(i = n; i > 0 && res > 0; i--) { ` ` ` ` ` `// either the result comes from the top ` ` ` `// (K[i-1][w]) or from (val[i-1] + K[i-1] ` ` ` `// [w-wt[i-1]]) as in Knapsack table. If ` ` ` `// it comes from the latter one/ it means ` ` ` `// the item is included. ` ` ` `if` `(res == K[i - 1][w]) ` ` ` `continue` `; ` ` ` `else` `{ ` ` ` ` ` `// This item is included. ` ` ` `printf` `(` `"%d "` `, wt[i - 1]); ` ` ` ` ` `// Since this weight is included its ` ` ` `// value is deducted ` ` ` `res = res - val[i - 1]; ` ` ` `w = w - wt[i - 1]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `val[] = { 60, 100, 120 }; ` ` ` `int` `wt[] = { 10, 20, 30 }; ` ` ` `int` `W = 50; ` ` ` `int` `n = ` `sizeof` `(val) / ` `sizeof` `(val[0]); ` ` ` ` ` `printknapSack(W, wt, val, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code for Dynamic Programming based ` `// solution for 0-1 Knapsack problem ` ` ` `class` `GFG { ` ` ` ` ` `// A utility function that returns ` ` ` `// maximum of two integers ` ` ` `static` `int` `max(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(a > b) ? a : b; ` ` ` `} ` ` ` ` ` `// Prints the items which are put ` ` ` `// in a knapsack of capacity W ` ` ` `static` `void` `printknapSack(` `int` `W, ` `int` `wt[], ` ` ` `int` `val[], ` `int` `n) ` ` ` `{ ` ` ` `int` `i, w; ` ` ` `int` `K[][] = ` `new` `int` `[n + ` `1` `][W + ` `1` `]; ` ` ` ` ` `// Build table K[][] in bottom up manner ` ` ` `for` `(i = ` `0` `; i <= n; i++) { ` ` ` `for` `(w = ` `0` `; w <= W; w++) { ` ` ` `if` `(i == ` `0` `|| w == ` `0` `) ` ` ` `K[i][w] = ` `0` `; ` ` ` `else` `if` `(wt[i - ` `1` `] <= w) ` ` ` `K[i][w] = Math.max(val[i - ` `1` `] + ` ` ` `K[i - ` `1` `][w - wt[i - ` `1` `]], K[i - ` `1` `][w]); ` ` ` `else` ` ` `K[i][w] = K[i - ` `1` `][w]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// stores the result of Knapsack ` ` ` `int` `res = K[n][W]; ` ` ` `System.out.println(res); ` ` ` ` ` `w = W; ` ` ` `for` `(i = n; i > ` `0` `&& res > ` `0` `; i--) { ` ` ` ` ` `// either the result comes from the top ` ` ` `// (K[i-1][w]) or from (val[i-1] + K[i-1] ` ` ` `// [w-wt[i-1]]) as in Knapsack table. If ` ` ` `// it comes from the latter one/ it means ` ` ` `// the item is included. ` ` ` `if` `(res == K[i - ` `1` `][w]) ` ` ` `continue` `; ` ` ` `else` `{ ` ` ` ` ` `// This item is included. ` ` ` `System.out.print(wt[i - ` `1` `] + ` `" "` `); ` ` ` ` ` `// Since this weight is included its ` ` ` `// value is deducted ` ` ` `res = res - val[i - ` `1` `]; ` ` ` `w = w - wt[i - ` `1` `]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `int` `val[] = { ` `60` `, ` `100` `, ` `120` `}; ` ` ` `int` `wt[] = { ` `10` `, ` `20` `, ` `30` `}; ` ` ` `int` `W = ` `50` `; ` ` ` `int` `n = val.length; ` ` ` ` ` `printknapSack(W, wt, val, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for Dynamic Programming ` `# based solution for 0-1 Knapsack problem ` ` ` `# Prints the items which are put in a ` `# knapsack of capacity W ` `def` `printknapSack(W, wt, val, n): ` ` ` `K ` `=` `[[` `0` `for` `w ` `in` `range` `(W ` `+` `1` `)] ` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# Build table K[][] in bottom ` ` ` `# up manner ` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `): ` ` ` `for` `w ` `in` `range` `(W ` `+` `1` `): ` ` ` `if` `i ` `=` `=` `0` `or` `w ` `=` `=` `0` `: ` ` ` `K[i][w] ` `=` `0` ` ` `elif` `wt[i ` `-` `1` `] <` `=` `w: ` ` ` `K[i][w] ` `=` `max` `(val[i ` `-` `1` `] ` ` ` `+` `K[i ` `-` `1` `][w ` `-` `wt[i ` `-` `1` `]], ` ` ` `K[i ` `-` `1` `][w]) ` ` ` `else` `: ` ` ` `K[i][w] ` `=` `K[i ` `-` `1` `][w] ` ` ` ` ` `# stores the result of Knapsack ` ` ` `res ` `=` `K[n][W] ` ` ` `print` `(res) ` ` ` ` ` `w ` `=` `W ` ` ` `for` `i ` `in` `range` `(n, ` `0` `, ` `-` `1` `): ` ` ` `if` `res <` `=` `0` `: ` ` ` `break` ` ` `# either the result comes from the ` ` ` `# top (K[i-1][w]) or from (val[i-1] ` ` ` `# + K[i-1] [w-wt[i-1]]) as in Knapsack ` ` ` `# table. If it comes from the latter ` ` ` `# one/ it means the item is included. ` ` ` `if` `res ` `=` `=` `K[i ` `-` `1` `][w]: ` ` ` `continue` ` ` `else` `: ` ` ` ` ` `# This item is included. ` ` ` `print` `(wt[i ` `-` `1` `]) ` ` ` ` ` `# Since this weight is included ` ` ` `# its value is deducted ` ` ` `res ` `=` `res ` `-` `val[i ` `-` `1` `] ` ` ` `w ` `=` `w ` `-` `wt[i ` `-` `1` `] ` ` ` `# Driver code ` `val ` `=` `[ ` `60` `, ` `100` `, ` `120` `] ` `wt ` `=` `[ ` `10` `, ` `20` `, ` `30` `] ` `W ` `=` `50` `n ` `=` `len` `(val) ` ` ` `printknapSack(W, wt, val, n) ` ` ` `# This code is contributed by Aryan Garg. ` |

*chevron_right*

*filter_none*

**Output:**

220 30 20

## Recommended Posts:

- Unbounded Knapsack (Repetition of items allowed)
- Printing Longest Common Subsequence | Set 2 (Printing All)
- 0-1 Knapsack Problem | DP-10
- 0/1 Knapsack using Branch and Bound
- Fractional Knapsack Problem
- Implementation of 0/1 Knapsack using Branch and Bound
- A Space Optimized DP solution for 0-1 Knapsack Problem
- Number of ways to arrange N items under given constraints
- Printing Longest Bitonic Subsequence
- Printing Maximum Sum Increasing Subsequence
- Printing Shortest Common Supersequence
- Printing longest Increasing consecutive subsequence
- Printing brackets in Matrix Chain Multiplication Problem
- Printing Matrix Chain Multiplication (A Space Optimized Solution)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.