Submission #1220053

#TimeUsernameProblemLanguageResultExecution timeMemory
1220053pauloaphKnapsack (NOI18_knapsack)C++20
73 / 100
1093 ms328 KiB
#include<iostream>
#include<algorithm>
#include <cmath>
using namespace std;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int C;
    int n;

    cin >> C >> n;

    long long v, w, b;
    vector<long long> dp(C + 1, 0);
    
    for(int i = 0; i < n; i++){
        int x = 1;
        cin >> v >> w >> b;

        if(b*w > C){
            b = C/w;
        }

        //Decomposição Binaria
        while(b > x){
            long long total_w = x * w;
            long long total_v = x * v;


            //Processa dp conforme insere os items
            for (int c = C; c >= total_w; c--) {
                dp[c] = max(dp[c], dp[c - total_w] + total_v);
            }

            b -= x;
            x = x << 1;
        }

        //Resto da decomposição
        long long total_w = b * w;
        long long total_v = b * v;

        for (int c = C; c >= total_w; c--) {
            dp[c] = max(dp[c], dp[c - total_w] + total_v);
        }
    }


   cout << dp[C] << endl;
   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...