diff options
Diffstat (limited to '01-algorithmic_toolbox/04-dynamic_programming/02-knapsack/knapsack.cpp')
-rw-r--r-- | 01-algorithmic_toolbox/04-dynamic_programming/02-knapsack/knapsack.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/01-algorithmic_toolbox/04-dynamic_programming/02-knapsack/knapsack.cpp b/01-algorithmic_toolbox/04-dynamic_programming/02-knapsack/knapsack.cpp new file mode 100644 index 0000000..c284560 --- /dev/null +++ b/01-algorithmic_toolbox/04-dynamic_programming/02-knapsack/knapsack.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <vector> + +using std::vector; + +int optimal_weight(int W, const vector<int> &w) { + //write your code here + int current_weight = 0; + for (size_t i = 0; i < w.size(); ++i) { + if (current_weight + w[i] <= W) { + current_weight += w[i]; + } + } + return current_weight; +} + +int main() { + int n, W; + std::cin >> W >> n; + vector<int> w(n); + for (int i = 0; i < n; i++) { + std::cin >> w[i]; + } + std::cout << optimal_weight(W, w) << '\n'; +} |