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>
#define ll long long
#define MAXN 100010
#define MAXS 2010
using namespace std;
vector<pair<ll,ll> > items[MAXS];
vector<pair<ll,ll> > cur;
ll dp[MAXS][2];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll s,n,v,w,k,ans=0;
cin >> s >> n;
for (ll i=1;i<=n;i++)
{
cin >> v >> w >> k;
items[w].push_back({v,k});
}
for (ll i=1;i<=s;i++)
{
if (items[i].empty())
continue;
sort(items[i].begin(),items[i].end(),greater<pair<ll,ll> >());
cur.clear();
ll sum=0,pos=0;
for (ll j=0;(j+1)*i<=s;j++)
{
if (!items[i][pos].second)
pos++;
if (pos==items[i].size())
break;
items[i][pos].second--;
sum+=items[i][pos].first;
cur.push_back({(j+1)*i,sum});
}
for (auto j : cur)
{
w=j.first;
v=j.second;
for (ll l=0;l+w<=s;l++)
dp[l+w][1]=max(dp[l+w][1],dp[l][0]+v);
}
for (ll j=0;j<=s;j++)
dp[j][0]=dp[j][1];
}
for (ll i=1;i<=s;i++)
ans=max(ans,dp[i][0]);
cout << ans;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:30:20: 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]
30 | if (pos==items[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... |