-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.cpp
More file actions
29 lines (25 loc) · 715 Bytes
/
knapsack.cpp
File metadata and controls
29 lines (25 loc) · 715 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
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int knapsack(vector<int> &weight, vector<int> &value, int capacity) {
vector<vector<int>> dp(capacity + 1, vector<int>(value.size() + 1, 0));
for (int i = 1; i <= capacity; ++i) {
for (int j = 1; j <= value.size(); ++j) {
if (i - weight[j - 1] >= 0) {
dp[i][j] =
max(value[j - 1] + dp[i - weight[j - 1]][j - 1], dp[i][j - 1]);
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[capacity][value.size()];
}
int main() {
vector<int> value{3, 4, 5, 6};
vector<int> weight{2, 3, 4, 5};
int capacity = 5;
cout << knapsack(weight, value, capacity) << '\n';
return 0;
}