Submission #1298294

#TimeUsernameProblemLanguageResultExecution timeMemory
1298294aslayeryl81Knapsack (NOI18_knapsack)C++20
73 / 100
1095 ms728 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int s, n;
    cin >> s >> n;
    vector<int> dp(s + 1, 0);
    while(n--) 
    {
        int v, w, k;
        cin >> v >> w >> k;
        if(w > s) continue;
        for(int r = 0; r < w; r++) 
        {
            deque<pair<int, int>> q;
            for(int i = 0, pos = r; pos <= s; i++, pos += w) 
            {
                int base = dp[pos] -(int)i * v;
                while(!q.empty() and q.front().second < i - k)
                    q.pop_front();
                while(!q.empty() and q.back().first <= base)
                    q.pop_back();
                q.emplace_back(base, i);
                dp[pos] = q.front().first +(int)i * v;
            }
        }
    }
    cout << dp[s];
    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...