-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack.cpp
62 lines (53 loc) · 966 Bytes
/
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
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
#include <iostream>
#include <algorithm>
#define N 10
using namespace std;
void print(int list[N], int n){
for(int i=0; i<n; i++){
cout<<list[i]<<" ";
}
cout<<endl;
}
int profit(int n, int p[N], int w[N], int list[N], int index[N], int wt, int maxw, int l){
if(wt > maxw){
print(list, n);
return -p[index[l-1]];
}
else if(l==n){
print(list, n);
return 0;
}
else{
int r;
if(l==0)
r = 0;
else
r = index[l-1] + 1;
int prof = 0;
for(int i=r; i<n; i++){
list[l] = w[i];
index[l] = i;
int t = p[i] + profit(n, p, w, list, index, wt+w[i], maxw, l+1);
if(t>prof)
prof = t;
list[l] = 0;
index[l] = -1;
}
return prof;
}
}
int main()
{
int n, maxw;
cin>>n;
cin>>maxw;
int list[N] = {0}, index[N], w[N], p[N];
for(int i=0; i<n;i++){
cin>>w[i];
index[i] = -1;
}
for(int i=0; i<n;i++)
cin>>p[i];
cout << endl << "profit is = " << profit(n, p, w, list, index, 0, maxw, 0) << endl;
return 0;
}