-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack_problem.cpp
53 lines (43 loc) · 1008 Bytes
/
knapsack_problem.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
// Author: Jakub Pawlina
// Algorithm: Knapsack Problem
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
struct Item
{
int weight;
int value;
void read()
{
std::cin >> value >> weight;
}
};
int result[2][10005];
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int knapsack_size;
std::cin >> knapsack_size;
int items_number;
std::cin >> items_number;
for (int item_nr = 0; item_nr < items_number; ++item_nr)
{
Item item;
item.read();
for (int volume = 0; volume <= knapsack_size; ++volume)
{
if (volume < item.weight)
{
result[item_nr % 2][volume] = result[(item_nr + 1) % 2][volume];
}
else
{
result[item_nr % 2][volume] = std::max(result[(item_nr + 1) % 2][volume - item.weight] + item.value, result[(item_nr + 1) % 2][volume]);
}
}
}
if (knapsack_size && items_number) std::cout << std::max(result[0][knapsack_size], result[1][knapsack_size]) << '\n';
else std::cout << "0\n";
return 0;
}