-
Notifications
You must be signed in to change notification settings - Fork 82
/
Modified_knapsack_with_repitition.cpp
58 lines (45 loc) · 1.14 KB
/
Modified_knapsack_with_repitition.cpp
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
#include<bits/stdc++.h>
using namespace std;
int solve(int v[],int w[],int n,int W)
{
//dp array to store values
int dp[n+1][W+1],i,j;
//initalising array with 0 weight allowed as 0
for(i=0;i<n+1;i++)
dp[i][0]=0;
//initalising array with 0 item available as 0
for(i=0;i<W+1;i++)
dp[0][i]=0;
//iterate to fill dp array
for(i=1;i<n+1;i++)
{
for(j=1;j<W+1;j++)
{
if(j<w[i-1])
dp[i][j]=dp[i-1][j];
// change in dp[i][j-w[i-1]] instead of dp[i-1][j-w[i-1]] as repitition is allowed
else
dp[i][j]=max(dp[i-1][j],dp[i][j-w[i-1]]+v[i-1]);
}
}
return dp[n][W];
}
int main() {
int t,i,a,n,W;
//input no of test cases
cin>>t;
while(t--)
{
//n is no of items and W is size of bag to be filled with items
cin>>n>>W;
//v is values of each item and w is weight of items
int v[n],w[n];
//input v and w
for(i=0;i<n;i++)
cin>>v[i];
for(i=0;i<n;i++)
cin>>w[i];
//prints maximum profit
cout<<solve(v,w,n,W)<<"\n";
}
}