forked from mr-robot-2008/c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnbounded_knapsack
More file actions
48 lines (38 loc) · 877 Bytes
/
Unbounded_knapsack
File metadata and controls
48 lines (38 loc) · 877 Bytes
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
//this is a same probelem as 0-1 knapsack but here we can choose item any number of time
//the famous problem is :Given a knapsack weight W and a set of n items with certain value vali and weight wti, we need to calculate the maximum amount that could make up this quantity exactly.
/* W = 8
val[] = {10, 40, 50, 70}
wt[] = {1, 3, 4, 5}
ans-> 110*/
#include<bits/stdc++.h>
using namespace std;
int unboundedKnapsack(int W, int n,
int val[], int wt[])
{
int dp[W+1];
memset(dp, 0, sizeof dp);
for (int i=0; i<=W; i++)
for (int j=0; j<n; j++)
if (wt[j] <= i)
dp[i] = max(dp[i], dp[i-wt[j]] + val[j]);
return dp[W];
}
int main()
{
int n;
cin>>n;
int val[n];
int wt[n];
for(int i=0;i<n;i++)
{
cin>>val[i];
}
for(int i=0;i<n;i++)
{
cin>>wt[i];
}
int W;
cin>>W;
cout << unboundedKnapsack(W, n, val, wt);
return 0;
}