0/1 Knapsack
Mediumjavapythonccppjavascript
Given N items with values and weights and a bag capacity W, choose a subset of items to maximise total value without exceeding the capacity. Each item may be used at most once.
Input Format
The first line contains an integer N. The second line contains N space-separated integers — the values. The third line contains N space-separated integers — the weights. The fourth line contains an integer W, the capacity.
Output Format
Print the maximum total value.
Example 1
Input
3 60 100 120 10 20 30 50
Output
220
Explanation: Taking items 2 and 3 gives value 220, which is best.
- 1 <= N <= 100
- 1 <= value[i] <= 1000000
- 1 <= weight[i] <= 10000
- 1 <= W <= 10000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- House RobberMedium · java, python, c, cpp, javascriptSolve Problem
- Longest Common SubsequenceMedium · java, python, c, cpp, javascriptSolve Problem
- Longest Increasing SubsequenceMedium · java, python, c, cpp, javascriptSolve Problem