-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0-1 Knapsack Problem.cpp
More file actions
73 lines (62 loc) · 1.25 KB
/
Copy path0-1 Knapsack Problem.cpp
File metadata and controls
73 lines (62 loc) · 1.25 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define f first
#define sec second
lli dp[1001][1001];
int main()
{
lli n,i,a,b,c,w,m,j,k,t;
vector<lli> vv,vw;
cin>>t;
while(t--)
{
cin>>n>>w;
for(i=0;i<n;i++)
{
cin>>a;
vv.pb(a);
}
for(i=0;i<n;i++)
{
cin>>a;
vw.pb(a);
}
for(i=0;i<=n;i++)
{
dp[i][0] = 0;
}
for(i=0;i<=w;i++)
{
dp[0][i] = 0;
}
for(i=1;i<=w;i++) // j is the number of items to choose from and i is the current capacity to make that choice
{
for(j=1;j<=n;j++)
{
if(i>=vw[j-1])
{
a = vw[j-1];
dp[j][i] = max(dp[j-1][i-a] + vv[j-1] , dp[j-1][i]);
}
else
{
dp[j][i] = dp[j-1][i];
}
}
}
/*
for(i=0;i<=w;i++)
{
for(j=0;j<=n;j++)
{
cout<<dp[j][i]<<" ";
}
cout<<endl;
}
*/
cout<<dp[n][w]<<endl;
}
return 0;
}