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;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("TEST.inp","r",stdin);
map<int,vector<pair<int,int>>>mp;
int limit,n; cin>>limit>>n;
for(int i=1;i<=n;i++)
{
int v,w,k; cin>>v>>w>>k;
mp[w].push_back({v,k});
}
vector<vector<long long>>dp(mp.size()+1,vector<long long>(limit+1,0));
int i=1;
for(auto it:mp)
{
sort(it.second.begin(),it.second.end(),greater<pair<int,int>>());
for(int j=1;j<=limit;j++)
{
dp[i][j]=dp[i-1][j];
long long profit=0;
int cur_i=0,cnt=0,copy=0;
while((copy+1)*it.first<=j&&cur_i<it.second.size())
{
copy++;
profit+=it.second[cur_i].first;
cnt++;
dp[i][j]=max(dp[i][j],dp[i-1][j-copy*it.first]+profit);
if(cnt==it.second[cur_i].second)
{
cur_i++;
cnt=0;
}
}
}
i++;
}
cout<<dp[mp.size()][limit];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:24:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | while((copy+1)*it.first<=j&&cur_i<it.second.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... |