제출 #1262859

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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

    vector<int> V(N), W(N), K(N);
    for (int i = 0; i < N; i++) cin >> V[i] >> W[i] >> K[i];

    vector<long long> dp(S + 1, -1e18);
    dp[0] = 0;

    for (int i = 0; i < N; i++) {
        int w = W[i], v = V[i], k = K[i];
        // chia theo modulo
        for (int r = 0; r < w; r++) {
            deque<pair<long long,int>> dq;
            for (int j = 0; r + j * w <= S; j++) {
                int idx = r + j * w;
                long long val = dp[idx] - 1LL * j * v;
                // pop từ sau nếu không tốt hơn
                while (!dq.empty() && dq.back().first <= val) dq.pop_back();
                dq.emplace_back(val, j);
                // pop từ trước nếu quá K bước
                while (!dq.empty() && j - dq.front().second > k) dq.pop_front();
                dp[idx] = dq.front().first + 1LL * j * v;
            }
        }
    }

    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...