-
Notifications
You must be signed in to change notification settings - Fork 342
/
0-1 Knapsack
44 lines (37 loc) · 864 Bytes
/
0-1 Knapsack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Question: - You are 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.
//Note that we have only one quantity of each item.
//Dynamic Programming - 0/1 Knapsack
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int w;
cin>>w;
int val[n],wt[n],i,j;
for(i=0;i<n;i++)
cin>>val[i];
for(i=0;i<n;i++)
cin>>wt[i];
int dp[n+1][w+1];
for(i=0;i<=n;i++)
for(j=0;j<=w;j++)
{ if(i==0||j==0)
dp[i][j]=0;
else
{if(wt[i-1]>j)
dp[i][j]=dp[i-1][j];
else
dp[i][j]=max(val[i-1]+dp[i-1][j-wt[i-1]],dp[i-1][j]);
}
}
cout<<dp[n][w]<<endl;
}
return 0;
}