제출 #1283883

#제출 시각아이디문제언어결과실행 시간메모리
1283883arashmemarKnapsack (NOI18_knapsack)C++20
29 / 100
4 ms1604 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100, maxs = 2010, inf = 2e9 + 10; deque <pair <int, int>> q[maxs]; int dp[maxs], cnt[maxs]; void upd(int ind, int x, int k) { while (q[ind].size() and x > q[ind].back().first) { q[ind].pop_back(); } q[ind].push_back({x, ++cnt[ind]}); if (q[ind].front().second == cnt[ind] - k) { q[ind].pop_front(); } return ; } int get(int ind) { if (q[ind].size()) { return q[ind].front().first; } return -inf; } int v[maxn], w[maxn], k[maxn]; int main() { int s, n; cin >> s >> n; for (int i = 1;i <= n;i++) { cin >> v[i] >> w[i] >> k[i]; } for (int i = 0;i < maxs;i++) { dp[i] = -inf; } dp[s] = 0; for (int j = 1;j <= n;j++) { for (int i = 0;i <= s;i++) { cnt[i] = 0; q[i].clear(); } for (int i = s;i >= 0;i--) { int back = dp[i]; dp[i] = max(dp[i], get(i % w[j]) + v[j] * cnt[i % w[j]]); upd(i % w[j], back - v[j] * (cnt[i % w[j]]), k[j]); } } int ans = 0; for (int i = 0;i <= s;i++) { ans = max(ans, dp[i]); } cout << ans; }
#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...