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;
using lint = long long;
using pli = pair<lint, int>;
const int MAX_N = 1e5 + 5, MAX_S = 2000 + 5;
int s, n;
lint v[MAX_N];
int w[MAX_N], k[MAX_N];
lint dp[MAX_N][MAX_S];
priority_queue<pli> trans[MAX_S];
int main() {
// freopen("knapsack.in", "r", stdin);
cin >> s >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], s);
}
for (int i = n; i >= 1; i--) {
for (int c = 0; c <= s; c++) {
if (c < w[i]) {
while (trans[c].size())
trans[c].pop();
trans[c].push({dp[i + 1][c], c});
dp[i][c] = dp[i + 1][c];
} else {
trans[c % w[i]].push({dp[i + 1][c] - v[i] * (c / w[i]), c});
while (trans[c % w[i]].top().second < c - k[i] * w[i]) // Should always have size
trans[c % w[i]].pop();
int new_c = trans[c % w[i]].top().second;
assert((c - new_c) % w[i] == 0);
dp[i][c] = dp[i + 1][new_c] + v[i] * ((c - new_c) / w[i]);
}
cout << i << " " << c << ": " << dp[i][c] << endl;
}
}
cout << dp[1][s] << '\n';
}
# | 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... |