제출 #979490

#제출 시각아이디문제언어결과실행 시간메모리
979490kilkuwuKnapsack (NOI18_knapsack)C++17
73 / 100
1058 ms29008 KiB
#include <bits/stdc++.h> #define nl '\n' template <typename T> inline bool ckmax(T& x, const T& y) { return x < y ? x = y, 1 : 0; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int S, N; std::cin >> S >> N; std::vector<std::map<int, int>> cnt(S + 1); for (int i = 0; i < N; i++) { int v, w, k; std::cin >> v >> w >> k; k = std::min(k, S / w); if (w <= S) { cnt[w][v] += k; } } std::vector<std::pair<int, int>> items; std::vector<int> dp(S + 1); for (int i = 1; i <= S; i++) { for (auto [v, k] : cnt[i]) { if (k >= 3) { int q = k - 1; k = 1 + (q & 1); if (2 * i <= S) { cnt[2 * i][2 * v] += q >> 1; } } while (k--) { for (int j = S; j >= i; j--) { ckmax(dp[j], dp[j - i] + v); } } } } std::cout << dp[S] << nl; }
#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...