# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
629394 | huyle | Knapsack (NOI18_knapsack) | C++17 | 71 ms | 4044 KiB |
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 i64 = long long;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 69
#endif
const i64 inf = 1e18;
int32_t main() {
int s, n;
scanf("%d %d", &s, &n);
std::vector<std::vector<std::pair<int, int>>> a(s + 1);
std::vector<int> wei(n);
for (int i = 0; i < n; ++i) {
int v, w, k;
scanf("%d %d %d", &v, &w, &k);
a[w].push_back({v, k});
wei[i] = w;
}
for (int i = 0; i <= s; ++i)
std::sort(a[i].rbegin(), a[i].rend());
std::sort(wei.begin(), wei.end());
wei.resize(std::unique(wei.begin(), wei.end()) - wei.begin());
n = (int)wei.size();
std::vector<i64> dp(s + 1, -inf);
dp[0] = 0;
for (int ii = 0; ii < n; ++ii) {
int i = wei[ii];
for (int j = s; ~j; --j) {
if (dp[j] == -inf)
continue;
int cnt = 0, idx = 0, res = 0, total = 0;
while (idx < (int)a[i].size()) {
res += a[i][idx].first;
++cnt;
++total;
if (j + (i64)total * i > s)
break;
dp[j + total * i] = std::max(dp[j + total * i], dp[j] + res);
if (cnt == a[i][idx].second) {
cnt = 0;
++idx;
}
}
}
}
printf("%lld\n", *std::max_element(dp.begin(), dp.end()));
}
Compilation message (stderr)
# | 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... |