Submission #899759

#TimeUsernameProblemLanguageResultExecution timeMemory
899759GhettoKnapsack (NOI18_knapsack)C++17
73 / 100
92 ms262144 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...