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>
#define ll long long
#define pii pair<int, int>
using namespace std;
int S, N;
ll dp[2001];
vector<pii> items [2001];
int main() {
cin >> S >> N;
fill(dp, dp + 2001, -1); dp[0] = 0;
for (int i = 0; i < N; i++) {
int v, w, k; cin >> v >> w >> k;
items[w].push_back({v, k});
}
for (int i = 1; i <= S; i++) {
if (items[i].size() == 0) continue;
sort(items[i].begin(), items[i].end()); reverse(items[i].begin(), items[i].end());
int bounds = S / i, p = 0;
while (p < items[i].size() && bounds > 0) {
for (int j = min(bounds, items[i][p].second); j > 0; j--) {
for (int k = S; k >= i; k--) {
if (dp[k - i] != -1) {
dp[k] = max(dp[k], dp[k - i] + items[i][p].first);
}
}
bounds--;
} p++;
}
}
ll res = 0;
for (int i = 1; i <= S; i++) res = max(res, dp[i]);
cout << res << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:22:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while (p < items[i].size() && bounds > 0) {
| ~~^~~~~~~~~~~~~~~~~
# | 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... |