제출 #1128637

#제출 시각아이디문제언어결과실행 시간메모리
1128637firegirlwaterboyKnapsack (NOI18_knapsack)C++20
100 / 100
85 ms33148 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; void solve() { int s, n; cin >> s >> n; map<int, vector<pii>> groups; for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; groups[w].push_back({v, k}); } int idx = 1, g = groups.size(); vector<vector<ll>> dp(g + 1, vector<ll>(s + 1)); for (auto &[w, val] : groups) { sort(val.begin(), val.end(), greater<pii>()); for (int i = 1; i <= s; i++) { dp[idx][i] = dp[idx - 1][i]; ll cnt = 1, sz = 0, curr = 0, acc = 0; while (i >= cnt * w && sz < val.size()) { acc += val[sz].first; dp[idx][i] = max(dp[idx][i], dp[idx - 1][i - cnt * w] + acc); cnt++, curr++; if (curr == val[sz].second) curr = 0, sz++; } } idx++; } cout << *max_element(dp[g].begin(), dp[g].end()) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); 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...