# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
438635 | Haidara | Knapsack (NOI18_knapsack) | C++17 | 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: Haidara *
* LANG: C++17 *
* PROB: *
* * * * * * * * * */
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define rep(i,x,n) for(int i=x;i<n;i++)
#define FOR(i,n) rep(i,0,n)
#define per(i,x,n) for(int i=x;i>n;i--)
#define ROF(i,x) for(int i=x;i>=0;i--)
#define v(i) vector< i >
#define p(i,j) pair< i , j >
#define pii pair<int,int>
#define m(i,j) map< i , j >
#define pq(i) priority_queue< i >
#define ff first
#define all(x) x.begin(),x.end()
#define ss second
#define pp push_back
using namespace std;
void SIO(string name="")
{
if(name!="")
{
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
}
const int inf=1LL<<62LL;
const int mod=1e9+7;
const int maxn=100001;
int s,n,v[maxn],w[maxn],k[maxn],dp[2001];
int solve(int inx=0,int curr=0)
{
if(dp[curr])
return dp[curr];
if(inx==n)
return 0;
FOR(i,k[inx]+1)
{
if(curr+i*w[inx]<=s)
dp[curr]=max(dp[inx][curr],solve(inx+1,curr+i*w[inx])+i*v[inx]);
else
break;
}
return dp[curr];
}
signed main()
{
SIO("");
cin>>s>>n;
FOR(i,n)
{
cin>>v[i]>>w[i]>>k[i];
k[i]=min(k[i],s/w[i]);
}
cout<<solve();
}