Submission #1271737

#TimeUsernameProblemLanguageResultExecution timeMemory
1271737pvb.tunglamKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms328 KiB
#include <bits/stdc++.h>
using namespace std;

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

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

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

    for (int i = 0; i < N; i++) {
        int v, w;
        long long k;
        cin >> v >> w >> k;

        if (k * 1LL * w >= S) {
            // coi nhu unbounded knapsack
            for (int j = w; j <= S; j++) {
                dp[j] = max(dp[j], dp[j - w] + v);
            }
        } else {
            // bounded knapsack b?ng binary splitting
            long long pow = 1;
            while (k >= pow) {
                int W = w * pow;
                int V = v * pow;
                for (int j = S; j >= W; j--) {
                    dp[j] = max(dp[j], dp[j - W] + V);
                }
                k -= pow;
                pow <<= 1;
            }
            if (k > 0) {
                int W = w * k;
                int V = v * k;
                for (int j = S; j >= W; j--) {
                    dp[j] = max(dp[j], dp[j - W] + V);
                }
            }
        }
    }

    cout << dp[S] << "\n";
}
#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...