Submission #1275636

#TimeUsernameProblemLanguageResultExecution timeMemory
1275636almazKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms580 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<long long> dp(S + 1, 0);

    for (int i = 0; i < N; i++) {
        long long V, W, K;
        cin >> V >> W >> K;

        if (K * W >= S) {
            // Unbounded knapsack (много предметов, больше чем вместимость)
            for (int w = W; w <= S; w++) {
                dp[w] = max(dp[w], dp[w - W] + V);
            }
        } else {
            // Binary splitting
            long long cnt = 1;
            while (K > 0) {
                long long take = min(cnt, K);
                long long val = take * V;
                long long wt = take * W;

                for (int w = S; w >= wt; w--) {
                    dp[w] = max(dp[w], dp[w - wt] + val);
                }

                K -= take;
                cnt <<= 1;
            }
        }
    }

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