제출 #1218181

#제출 시각아이디문제언어결과실행 시간메모리
1218181shuvo_06Knapsack (NOI18_knapsack)C++20
73 / 100
1093 ms560 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int S, N;
    cin >> S >> N;

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

    for (int i = 0; i < N; i++) {
        int val, wt, cnt;
        cin >> val >> wt >> cnt;

        if (wt > S) continue;

        for (int rem = 0; rem < wt; rem++) {
            deque<pair<int, int>> dq;
            for (int j = 0, pos = rem; pos <= S; j++, pos += wt) {
                int curr = dp[pos] - j * val;

                while (!dq.empty() && dq.back().first <= curr)
                    dq.pop_back();
                dq.emplace_back(curr, j);

                while (dq.front().second < j - cnt)
                    dq.pop_front();

                dp[pos] = dq.front().first + j * val;
            }
        }
    }

    cout << *max_element(dp.begin(), dp.end()) << '\n';
    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...