Submission #1156130

#TimeUsernameProblemLanguageResultExecution timeMemory
1156130mohsinaKnapsack (NOI18_knapsack)C++20
0 / 100
2 ms1092 KiB


#include <bits/stdc++.h>
using namespace std;
int main()
{
//     The first line of input contains two positive integers, S and N.
// The next N lines of input will each contain three integers, where the i-th line contains Vi
// , Wi
// and Ki
// , the value in SGD, weight in kilograms and number of the i-th item respectively
//For all subtasks, 1 ≤ S ≤ 2000bagWeight, 1 ≤ Vi ≤ 1000000, 1 ≤ Wi ≤ S.
    int bagWeight=0,items=0;
    cin>>bagWeight>>items;
    vector<int> values(items,0),weights(items,0),counts(items,0);
    for(int i=0;i<items;i++){
        cin>>values[i]>>weights[i]>>counts[i];
    }
    vector<vector<int>> dp(items,vector<int> (bagWeight+1,-1));
    for(int i=0;i<items;i++){
        dp[i][0]=0;
    }
    for(int i=0;i<items;i++){
      //picking 0 to k items
       
        for(int k=counts[i];k>=1;k--){
            int currWeight=k*weights[i];
            int currValue=k*values[i];
            for(int j=bagWeight;j>=currWeight;j--){
                 if(i>0){
                    dp[i][j]=max(dp[i][j],dp[i-1][j]);
                 }
                 if(i-1>=0 && dp[i-1][j-currWeight]!=-1){
                     dp[i][j]=max(dp[i][j],currValue+dp[i-1][j-currWeight]);
                 }
                 if(dp[i][j-currWeight]!=-1){
                    dp[i][j]=max(dp[i][j],currValue+dp[i][j-currWeight]);
                 }
            }
       }
    }
    // cout<<"done\n";
    // for(int i=0;i<items;i++){
    //   //picking 0 to k items
    //   for(int j=0;j<=bagWeight;j++){
    //       cout<<dp[i][j]<<" ";
    //   }
    //   cout<<"\n";
    // }
    cout<<dp[items-1][bagWeight]<<"\n";
    // vector<int> dp(bagWeight+1,0);
    //for a given bagWeight,what is maximum value we can get
    // for(int i=0;i<items;i++){
    //     for(int j=bagWeight;j>=0;j++){
            
    //     }
    // }
//     15 5
// 4 12 1
// 2 1 1
// 10 4 1
// 1 1 1
// 2 2 1
    
    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...