제출 #1268477

#제출 시각아이디문제언어결과실행 시간메모리
1268477herominhsteveKnapsack (NOI18_knapsack)C++20
73 / 100
1095 ms488 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int S, N; cin >> S >> N; vector<long long> dp(S+1, -1e18); dp[0] = 0; for (int i=0; i<N; i++) { long long v; int w, k; cin >> v >> w >> k; for (int r=0; r<w; r++) { deque<pair<int,long long>> dq; for (int m=0; r+m*w<=S; m++) { int weight = r + m*w; long long val = dp[weight] - 1LL * m * v; while (!dq.empty() && dq.back().second <= val) dq.pop_back(); dq.emplace_back(m, val); while (!dq.empty() && dq.front().first < m - k) dq.pop_front(); dp[weight] = dq.front().second + 1LL * m * v; } } } cout << *max_element(dp.begin(), dp.end()) << "\n"; }
#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...