Submission #706415

#TimeUsernameProblemLanguageResultExecution timeMemory
706415AverageAmogusEnjoyerKnapsack (NOI18_knapsack)C++17
100 / 100
147 ms140856 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define nl '\n' using ll = long long; void maxself(int &a, int b) { a = max(a, b); } int main() { ios::sync_with_stdio(0); cin.tie(0); int S, N; cin >> S >> N; vector<int> quantiMax(S+1); for (int a=1; a<=S; a++) quantiMax[a] = (S+a-1)/a; vector<vector<pair<int, int>>> infos(S+1); int v, w, k; for (int a=0; a<N; a++) { cin >> v >> w >> k; if (w > S) continue; infos[w].pb({v, k}); } for (int a=1; a<=S; a++) sort(infos[a].begin(), infos[a].end()); vector<int> values, weights; values.pb(0); weights.pb(0); for (int a=1; a<=S; a++) { while(infos[a].size() && quantiMax[a]--) { int value = infos[a][infos[a].size()-1].first; infos[a][infos[a].size()-1].second--; values.pb(value); weights.pb(a); if (infos[a][infos[a].size()-1].second <= 0) infos[a].pop_back(); } } int dp[(int) values.size()][S+1]; memset(dp, 0, sizeof(dp)); for (int n=1; n < (int) values.size(); n++) { for (int w = 0; w <= S; w++) { if (w - weights[n] >= 0) maxself(dp[n][w], dp[n-1][w - weights[n]] + values[n]); maxself(dp[n][w], dp[n-1][w]); } } cout << dp[values.size()-1][S]; }
#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...