This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
int main() {
std::map<int, std::vector<std::pair<int, int>>> groups;
int s, n;
std::cin >> s >> n;
for (int i = 1; i <= n; ++i) {
int v, w, k;
std::cin >> v >> w >> k;
groups[w].emplace_back(v, k);
}
std::vector<long long> dp[2];
dp[0].resize(s + 1, 0);
dp[1].resize(s + 1, 0);
int row = 0;
for (auto &[weight, objects]: groups) {
std::sort(objects.begin(), objects.end(), std::greater<>());
for (int i = 0; i <= s; ++i) {
dp[row][i] = dp[1 - row][i];
int count = 0;
int ptr = 0;
int used = 0;
long long profit = 0;
while ((count + 1) * weight <= i && ptr < objects.size()) {
count++;
profit += objects[ptr].first;
dp[row][i] = std::max(dp[row][i], dp[1 - row][i - count * weight] + profit);
used++;
if (used == objects[ptr].second) {
used = 0;
ptr++;
}
}
}
row = 1 - row;
}
row = 1 - row;
std::cout << *std::max_element(dp[row].begin(), dp[row].end());
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:36:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while ((count + 1) * weight <= i && ptr < objects.size()) {
| ~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |