제출 #877922

#제출 시각아이디문제언어결과실행 시간메모리
877922codexistentKnapsack (NOI18_knapsack)C++14
0 / 100
2 ms1884 KiB
#include <iostream>
#include <queue>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOR_EXC(i, a, b) for(int i = a; i < b; i++)
 
int main(){
    long long S, N, V, W, K;
    cin >> S >> N;
 
    long long DP[N + 1][S + 1]; // DP[items 1...n][shopping cart size used]
    FOR(n, 0, N) FOR(s, 0, S) DP[n][s] = 0ll;
    FOR(n, 1, N){
        cin >> V >> W >> K;
      	K = max(K, S);
        FOR(a, 0, W - 1){
            deque<pair<long long, int>> sw; // sliding window deck
            for(int s = a, i = 0; s <= S; s += W, i++){
                DP[n][s] = DP[n - 1][s];
                if(!sw.empty()) DP[n][s] = max(DP[n][s], sw.front().first + V*i);
                
                // update deck
                if(!sw.empty() && i - sw.front().second >= K) {
                    sw.pop_front();
                }
                long long upd = DP[n - 1][s] - V*i;
                while(!sw.empty() && sw.back().first < upd) sw.pop_back();
                sw.push_back(make_pair(upd, i));
            }
        }
    }
 
    long long R = 0;
    FOR(s, 0, S) R = max(R, DP[N][s]);
 
    cout << R << endl;
}
#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...