Submission #1143579

#TimeUsernameProblemLanguageResultExecution timeMemory
1143579bluecherrycoderKnapsack (NOI18_knapsack)C++20
49 / 100
185 ms1236 KiB
#include <bits/stdc++.h>
using namespace std;
#define speedup ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

int main()
{
    speedup;
    int s,n;
    cin>>s>>n;// max wt limit, and total typof items
    
    vector<pair<int,int>> item (n+1);// cotly, weight
    vector<int> freq (n+1);
    
    for(int i=1;i<=n;i++)
    {
        int cost,wt,k;
        cin>>cost>>wt>>k;//input
        
        item[i]={cost,wt};//setting the item values
        freq[i]=k;
    }
    
    // for(int i=1;i<=n;i++)cout<<item[i].first<<" "<<item[i].second<<" "<<freq[i]<<"\n";
    
    
    if(n==1)// special case to calculate for subtask 1 which has any k val but n=i-1
    {
        int curr_wt=item[1].second;
        int temp=s/curr_wt;
        
        if(temp>=freq[1]) cout<<freq[1]*item[1].first;
        else cout<<temp*item[1].first;
    }
    
    else
    {
    vector<vector<int>> dp(n+1, vector<int> (s+1,0));//definition
    
    for(int i=1;i<=n;i++)
    {
        for(int wt=1;wt<=s;wt++)
        {
            int itr=freq[i];//no of times ith item can be chosen
            
            for(int j=1;j<=itr;j++)
            {
                if((item[i].second*j)<=wt)
                dp[i][wt]=max(dp[i][wt], dp[i-1][wt-(item[i].second*j)]+item[i].first*j);
            }
            
            dp[i][wt]=max(dp[i][wt], dp[i-1][wt]);
            
        }
    }
    
    
    // for(int i=1;i<=n;i++)
    // {
    //     for(int wt=1;wt<=s;wt++)
    //     {
    //         cout<<dp[i][wt]<<" ";
    //     }
    //     cout<<"\n";
    // }
    
    cout<<dp[n][s];
    }
    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...