Submission #1231705

#TimeUsernameProblemLanguageResultExecution timeMemory
1231705mathmemonistKnapsack (NOI18_knapsack)C++20
29 / 100
2 ms1096 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define ff first 
#define se second
#define all(x) (x).begin(),(x).end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define f1(n) for(ll i = 0; i < n; i++)
#define f2(n) for(ll j = 0; j < n; j++)
#define f3(n) for(ll k = 0; k < n; k++)
#define pb push_back
#define mp make_pair
#define endl "\n"
using ll = long long; 
using ld = long double;
const int mod=1e9+7;
const int cons=2e3+5;
ll inf=1e17;
ll ninf=-inf;
ll n,m,k,q,x,y;
typedef pair<ll,ll>pt;
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const vector<pt>directions = {{-1,0},{1,0},{0,-1},{0,1}};
const vector<char>moves = {'U','D','L','R'};

int weight[101];
int value[101];
int freq[101];
int dp[101][2001];

ll power(ll a,ll b)
{
    if(b==0)
        return 1;
    if(b==1)
        return a;

    ll half=power(a,b/2)%mod;
    half=(half*half);

    if(b%2==1)
        half=(half*a)%mod;

    return half%mod;
}

ll modinv(ll a)
{
    return power(a,mod-2);
}

int rec(int level,int weightremain)
{

        // pruning 
        if(weightremain<=0) return 0;

        if(level>n)     return 0;

        if(dp[level][weightremain]!=-1) return dp[level][weightremain];


        // base case
        // current item will be taken or not taken 
        // that's it


        // cache check 


        // compute transition
        int ans=0;
        ans=max(ans,rec(level+1,weightremain));

        if(weightremain>=weight[level])
            ans=max(ans,value[level]+rec(level+1,weightremain-weight[level]));


        // save and return
        return dp[level][weightremain] = ans;

}

void solve() 
{  

       cin>>k>>n;

       // maximum weight is 2000 only
       for(int i=1;i<=n;i++)      cin>>value[i]>>weight[i]>>freq[i];

       if(n==1)
       {
            int val=k/weight[1];
            ll cnt=min(freq[1],val);
            cout<<cnt*value[1]<<endl;
            return;
       }

       memset(dp,-1,sizeof(dp));

       cout<<rec(1,k)<<endl;





       
}

int main() {
    fastio
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    //   precomp();
      // precompute();
    //precompute2();
    //precompute3();
    //precompute4();
    //rec(0,0);
    int t = 1;
    //cin >> t;
    while (t--) 
    {
       solve();
    }
    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...