Submission #975427

#TimeUsernameProblemLanguageResultExecution timeMemory
975427vjudge1Knapsack (NOI18_knapsack)C++98
49 / 100
170 ms262144 KiB
#include <bits/stdc++.h>
using  namespace std;
    int s,n;
void solve(){
    vector<int>v,w;
    int banyak=0;
    int tv,tw,tk;
    v.push_back(0);
    w.push_back(0);
    for (int i = 1; i <= n; i++)
    {
       
       cin>>tv>>tw>>tk;
        banyak+=tk;
       while (tk--)
       {
        v.push_back(tv);
        w.push_back(tw);
       }
    }
    
    int dp[banyak+1][s+1];
    for (int i = 0; i <= s; i++)
    {
        dp[0][i]=0;
    }
    for (int i = 0; i <= banyak; i++)
    {
        dp[i][0]=0;
    }
    for (int i = 1; i <= banyak; i++)
    {
        for (int j = 1; j <= s; j++)
        {
            if(w[i]>j){
                dp[i][j]=dp[i-1][j];
            }else{
                dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
            }
        }
    }
    cout<<dp[banyak][s];
}
int main(){
    cin>>s>>n;
    if(n==1){
        int W=s;
    int v[n];
    int we[n];
    int x;
    for (int i = 0; i < n; i++)
    {
        cin>>v[i];
        cin>>we[i];
        cin>>x;
    }
    if(n==1){
        if(W/we[0]<x){
            cout<<v[0]*(W/we[0]);
        }else{
             cout<<v[0]*x;
        }
    }
    }else{
        solve();
    }
    
    
}
#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...