Submission #467691

#TimeUsernameProblemLanguageResultExecution timeMemory
467691xn_17Knapsack (NOI18_knapsack)C++14
73 / 100
1091 ms2704 KiB
//oj.uz--NOI18_KNAPSACK
//USACO Knapsack DP

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_tuple

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

    tuple<int,int,int> items[n];

    for(int i = 0; i < n; i++)
    {
        int v, w, c; cin >> v >> w >> c;
        items[i] = mp(v,w,c);
    }

    ll dp[s+1];

    for(int i = 0; i <= s; i++)
    {
        dp[i] = -1e10;
    }

    dp[0] = 0;

    for(int i = 0; i < n; i++)
    {
        int val, wgh, copy; tie(val, wgh, copy) = items[i];
        int upto = min(copy, s/wgh);

        //cout << "VAL " << val << " WEIGHT " << wgh << " COPIES " << copy << " UPTO " << upto << endl; 

        for(int k = s; k >= wgh; k--)
        {
            for(int j = 1; j <= upto; j++)
            {
                if(k >= wgh*j && (dp[k] != -1e10 || dp[k-wgh*j] != -1e10))
                { 
                    dp[k] = max(dp[k], dp[k-wgh*j] + val*j);
                }
            }

            //cout << "K " << k << " " << dp[k] << endl;
        }
    }

    ll ans = 0;

    for(int i = 0; i <= s; i++)
    {
        ans = max(ans, dp[i]);
    }

    cout << ans;
}
#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...