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 <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int s, n;
cin >> s >> n;
map<int, vector<pair<int, int>>> mp;
for (int i = 1; i <= n; i++) {
int v, w, a;
cin >> v >> w >> a;
if (w <= s && a > 0) {
mp[w].push_back({v, a});
}
}
vector<vector<long long>> dp(mp.size() + 1, vector<long long>(s + 1, INT_MIN));
dp[0][0] = 0;
int i = 1;
for (auto& [w, items] : mp) {
sort(items.begin(), items.end(), greater<pair<int, int>>());
for (int j = 0; j <= s; j++) {
dp[i][j] = dp[i - 1][j];
int amt = 0, curr = 0, used = 0;
long long x = 0;
while ((amt + 1) * w <= j && curr < items.size()) {
amt++;
x += items[curr].first;
if (dp[i - 1][j - amt * w] != INT_MIN) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - amt * w] + x);
}
used++;
if (used == items[curr].second) {
used = 0;
curr++;
}
}
}
i++;
}
cout << *max_element(dp.back().begin(), dp.back().end()) << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:19:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | for (auto& [w, items] : mp) {
| ^
knapsack.cpp:25:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while ((amt + 1) * w <= j && curr < items.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... |