-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-1_Knapsack.cpp
More file actions
35 lines (32 loc) · 845 Bytes
/
Copy path0-1_Knapsack.cpp
File metadata and controls
35 lines (32 loc) · 845 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
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, S, cw = 0, cp = 0;
cin >> N >> S;
int size[1001] = {0}, value[1001] = {0}, res[1001] = {0};
for (int i = 1; i <= N; i++)
{
cin >> size[i];
}
for (int i = 1; i <= N; i++)
{
cin >> value[i];
}
int profit[1001][1001] = {0};
for (int selected = 1; selected <= N; selected++)
{
for (int cap = 1; cap <= S; cap++)
{
if (size[selected] <= cap)
profit[selected][cap] = max(profit[selected - 1][cap], profit[selected - 1][cap - size[selected]] + value[selected]);
else
profit[selected][cap] = profit[selected - 1][cap];
}
}
cout << profit[N][S];
return 0;
}