Submission #658959

#TimeUsernameProblemLanguageResultExecution timeMemory
658959NONTACKnapsack (NOI18_knapsack)C++11
49 / 100
14 ms18256 KiB
#include <bits/stdc++.h>
using namespace std;

int n, s;
vector<int> item;
vector<int> weight;
int dp[1001][2001];

void print()
{
    for(int i = 1; i < n; i++){
        for(int j = 0; j <= s; j++) cout<<dp[i][j]<<" ";
        cout<<endl;
    }
}

int main()
{
    cin>>s>>n;

    if(n == 1){
        int v, w, k; cin>>v>>w>>k;
        if(s / w >= k) cout<<k * v;
        else cout<<(s / w) * v;
        return 0;
    }

    item.push_back(0); weight.push_back(0);
    for(int i = 0; i < n; i++){
        int v, w, k; cin>>v>>w>>k;
        while(k--){
            item.push_back(v);
            weight.push_back(w);
        }
    }

    n = item.size();
    for(int i = 1; i < n; i++){
        int w = weight[i], v = item[i];
        for(int j = 1; j <= s; j++){
            dp[i][j] = dp[i - 1][j];
            if(j - w >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
        }
    }

    //print();

    cout<<dp[n - 1][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...