-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKanpsack.cpp
More file actions
30 lines (28 loc) · 875 Bytes
/
Copy pathKanpsack.cpp
File metadata and controls
30 lines (28 loc) · 875 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
#include<bits/stdc++.h>
using namespace std;
int main() {
int knapsackCapacity, noOfItems;
cin >> noOfItems >> knapsackCapacity;
vector<int> weights(noOfItems);
vector<int> values(noOfItems);
for(int i=0; i<noOfItems; ++i) cin >> weights[i];
for(int i=0; i<noOfItems; ++i) cin >> values[i];
vector<vector<int>> dp(noOfItems + 1, vector<int>(knapsackCapacity + 1, 0));
for(int i=0; i<=noOfItems; ++i) {
for(int j=0; j<=knapsackCapacity; ++j) {
if(i == 0 || j == 0) continue;
if(weights[i-1] > j) {
dp[i][j] = dp[i][j-1];
continue;
}
dp[i][j] = max(dp[i][j-1], dp[i-1][j-weights[i-1]] + values[i-1]);
}
}
for(vector<int> a : dp) {
for(int b : a) {
printf("%4d", b);
}
cout << "\n";
}
return 0;
}