# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
501362 | ala2 | Knapsack (NOI18_knapsack) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: alaa523
LANG: C++11
PROB: Fruit Feast
*/
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define F first
#define S second
using namespace std;
int n,s;
int v[1001000];
int w[1001000];
int k[1000100];
vector<pair<int,int>>a;
int dp[2001];
int dp2[2011];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
memset(dp,-1,sizeof dp);
cin>>s>>n;
for(int i=0; i<n; i++)
{
cin>>v[i]>>w[i]>>k[i];
k[i]=min(k[i],s);
k[i]=min(k[i],s/w[i]+1)
}
for(int i=0; i<n; i++)
{
for(int j=0; j<k[i]; j++)
{
a.pb({v[i],w[i]});
}
}
dp[0]=0;
int mx=0;
for(int j=0; j<a.size(); j++)
{
for(int i=0;i<=s;i++)
{
if(dp[i]!=-1)
{
if(a[j].S+i>s)
continue;
dp2[a[j].S+i]=max(dp[a[j].S+i],dp[i]+a[j].F);
}
}
for(int i=0;i<=s;i++)
dp[i]=max(dp[i],dp2[i]);
}
for(int i=1; i<=s; i++)
{
// cout<<" "<<i<<" "<<dp[i]<<endl;
mx=max(mx,dp[i]);
}
cout<<mx<<endl;
}
//dp[i]=the maxvalue we can reach with i weight
//dp[i]=max(dp[i],dp[i-w[j]]+v[j]);