Submission #1167263

#TimeUsernameProblemLanguageResultExecution timeMemory
1167263constnodeKnapsack (NOI18_knapsack)C++20
37 / 100
1 ms328 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

    int S, N;
    cin >> S >> N;
    
    // dp[j] will hold the maximum value for weight j
    vector<int> dp(S + 1, 0);
    
    // Process each item type
    for (int i = 0; i < N; i++){
        int v, w, k;
        cin >> v >> w >> k;
        
        // Binary splitting: decompose the k copies into powers of two groups.
        int amount = 1;
        while (k > 0) {
            int use = min(amount, k);
            int weight = w * use;
            int value  = v * use;
            
            // For each group, update the DP table from S downto weight.
            for (int j = S; j >= weight; j--) {
                dp[j] = max(dp[j], dp[j - weight] + value);
            }
            
            k -= use;
            amount *= 2;
        }
    }
    
    cout << dp[S] << "\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...