Submission #1127702

#TimeUsernameProblemLanguageResultExecution timeMemory
1127702eirinayukariKnapsack (NOI18_knapsack)C++20
0 / 100
5 ms324 KiB
// from miraiya 

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define FOR(i, l, r) for(int i = (l); i <= (r); i++)
#define FOD(i, r, l) for(int i = (r); i >= (l); i--)
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int mod  = 1e9 + 7;

int n, s, w[maxn], v[maxn], a[maxn];
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> s;
    FOR(i, 1, n) cin >> w[i] >> v[i] >> a[i];

    vector<int> dp(s + 1, 0);

    FOR(i, 1, n) 
    {
        FOR(r, 0, w[i] - 1) 
        {
            deque<pair<int, int>> dq;
            for (int j = 0; j * w[i] + r <= s; ++j) 
            {
                int idx = j * w[i] + r;
                int val = dp[idx] - j * v[i];
                while (!dq.empty() && dq.back().se <= val) 
                {
                    dq.pop_back();
                }
                dq.emplace_back(j, val);
                while (!dq.empty() && dq.front().fi < j - a[i]) 
                {
                    dq.pop_front();
                }
                dp[idx] = dq.front().se + j * v[i];
            }
        }
    }

    cout << dp[s] << endl;
    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...