Submission #476285

#TimeUsernameProblemLanguageResultExecution timeMemory
476285ViantiKnapsack (NOI18_knapsack)C++17
100 / 100
81 ms19424 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const ll MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int s, n; cin >> s >> n; vector<vector<pii>> a(s + 1); for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; a[w].push_back({v, k}); } for (int i = 0; i <= s; i++) { sort(a[i].rbegin(), a[i].rend()); } vector<vector<int>> dp(s + 1, vector<int>(s + 1, 0)); for (int i = 1; i <= s; i++) { for (int j = 0; j <= s; j++) { int curw = 0; int curv = 0; dp[i][j] = max(dp[i - 1][j], dp[i][j]); for (auto &el: a[i]) { for (int p = 0; p < el.second; p++) { curw += i; curv += el.first; if (curw > j)break; dp[i][j] = max(dp[i][j], dp[i - 1][j - curw] + curv); } if (curw > j)break; } } } int ans = 0; for (int i = 0; i <= s; i++) { ans = max(ans, dp[s][i]); } cout << ans << endl; }
#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...