Submission #674294

#TimeUsernameProblemLanguageResultExecution timeMemory
674294ThinGarfieldKnapsack (NOI18_knapsack)C++11
100 / 100
204 ms43096 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define mp make_pair #define F first #define S second constexpr int maxs = 2e3 + 5; int n, s; vector<pii> items[maxs]; // items[i] = all items (val, ct) with weight i, sort by val ll vadd[maxs][maxs]; // vadd[i][j] = value added by taking j items of weight i ll dp[maxs][maxs]; // dp[i][j] = best value from i-sized basket w/ only items of weight <= j int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); cin >> s >> n; for (int i = 1; i <= n; i++) { int v, w, k; cin >> v >> w >> k; items[w].push_back(mp(v, k)); } for (int i = 1; i <= s; i++) { sort(items[i].rbegin(), items[i].rend()); int ct = 1; for (pii it : items[i]) { for (int j = 0; j < it.S && ct <= s / i; j++, ct++) { vadd[i][ct] = vadd[i][ct - 1] + it.F; } } // cout << i << " : "; // for (int j = 0; j <= ct; j++) { // cout << vadd[i][j] << ' '; // } // cout << '\n'; } for (int i = 1; i <= s; i++) { for (int j = 1; j <= s; j++) { dp[i][j] = dp[i][j - 1]; for (int k = 1; j * k <= i && vadd[j][k] != 0; k++) { dp[i][j] = max(dp[i][j], dp[i - j * k][j - 1] + vadd[j][k]); } // cout << dp[i][j] << ' '; } // cout << '\n'; } cout << dp[s][s] << '\n'; return 0; }
#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...