제출 #471091

#제출 시각아이디문제언어결과실행 시간메모리
471091dqkKnapsack (NOI18_knapsack)C++17
100 / 100
122 ms34788 KiB
#include <bits/stdc++.h> using i64 = long long; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int s, n; std::cin >> s >> n; std::map<int, std::vector<std::pair<int, int>>> m; for (int i = 0; i < n; ++i) { int v, w, k; std::cin >> v >> w >> k; m[w].push_back({v, k}); } std::vector<std::vector<i64>> dp(m.size() + 1, std::vector<i64>(s + 1, 0)); int i = 0; i64 ans = 0; for (auto &[w, p] : m) { i++; std::sort(p.rbegin(), p.rend()); for (int j = 0; j <= s; ++j) { dp[i][j] = dp[i - 1][j]; int W = 0; i64 V = 0; for (auto [v, k] : p) { if (W > j) break; for (int l = 0; l < k; ++l) { W += w; V += v; if (W > j) break; dp[i][j] = std::max(dp[i][j], dp[i - 1][j - W] + V); } } ans = std::max(ans, dp[i][j]); } } std::cout << ans << "\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...