This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define file "file"
#define pll pair<long long, long long>
const long long N=2005, INF=1e12;
long long W,n,i,j,k,v,w,sum,t,dem,dp[N][N],res;
vector<pll> a[N];
int main()
{
//freopen(file".inp", "r", stdin);
//freopen(file".out", "w", stdout);
ios_base::sync_with_stdio(); cin.tie();
cin>>W>>n;
for (i=1;i<=n;i++)
{
cin>>v>>w>>k;
if (w<W && k>0) a[w].push_back({v, min(k, W/w)});
}
memset(dp, -0x3f, sizeof dp);
dp[0][0]=0;
for (i=1;i<=W;i++)
{
if (a[i].size()>0) sort(a[i].begin(), a[i].end(), greater<pll>());
for (j=0;j<=W;j++)
{
dp[i][j]=dp[i-1][j];
if (a[i].size()==0) continue;
sum=0; t=0; k=0; dem=0;
while ((sum+1)*i <= j && k<a[i].size())
{
sum++;
t+=a[i][k].first; dem++; //cout<<i<<' '<<j<<' '<<k<<' '<<dem<<'\n';
if (dp[i-1][j-sum*i] > -INF) dp[i][j]=max(dp[i][j], dp[i-1][j-sum*i] + t);
if (dem==a[i][k].second) k++, dem=0;
}
res=max(res, dp[i][j]);
}
}
cout<<res;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:37:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while ((sum+1)*i <= j && k<a[i].size())
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |