-
Notifications
You must be signed in to change notification settings - Fork 8
/
Unbounded_Knapsack.cpp
32 lines (29 loc) · 1.04 KB
/
Unbounded_Knapsack.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
#include <bits/stdc++.h>
using namespace std;
#define lint long long int
lint UnboundedKnapsack(lint Capacity,lint n, lint weight[],lint value[]){
lint maxProfit[Capacity+1];
for(lint i=0;i<Capacity+1;i++){
maxProfit[i]=0; // Initialzing our dp array with 0
}
for(lint i=0;i<W+1;i++){
for(lint j=0;j<n;j++){
if(weight[j]<=i){
maxProfit[i] = max(maxProfit[i], maxProfit[i-weight[j]] + value[j]); // Dp formula : maxProfit[i] = max value we can achieve with available items and knapsack capacity being i.
}
}
}
return maxProfit[Capacity];
}
int main(){
// The no. of items :
lint n = 4;
// Weights of all the items :
lint weight[4] = {5 , 10, 8, 15};
// Enter values of all the items :
lint value[4] = {40, 30, 50, 25};
// Enter the knapsack capacity :
lint Capacity = 120;
cout<<"The maximum value you can achieve in Unbounded Knapsack is: "<<UnboundedKnapsack(Capacity,n,weight,value);
return 0;
}