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;
const int nx=2e3+5;
int s, n, v, w, k, dp[2][nx], mx, cnt[nx];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq[nx];
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>s>>n;
for (int i=0; i<n; i++)
{
cin>>v>>w>>k;
pq[w].push({v, k});
cnt[w]+=k;
if (cnt[w]-pq[w].top().second>s/w) cnt[w]-=pq[w].top().second, pq[w].pop();
}
vector<pair<int, int>> d;
for (int i=1; i<=s; i++)
{
int tmp=s/i;
stack<pair<int, int>> s;
while (!pq[i].empty()) s.push(pq[i].top()), pq[i].pop();
while (tmp>0&&!s.empty())
{
tmp--;
auto [x, y]=s.top();
s.pop();
d.push_back({i, x});
if (--y!=0) s.push({x, y});
}
}
for (int i=0; i<d.size(); i++)
{
int c=i%2, p=1-c;
v=d[i].second; w=d[i].first;
//cout<<v<<' '<<w<<'\n';
for (int j=1; j<=s; j++)
{
dp[c][j]=dp[p][j];
if (j>=w) dp[c][j]=max(dp[c][j], dp[p][j-w]+v);
mx=max(mx, dp[c][j]);
}
}
cout<<mx;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i=0; i<d.size(); i++)
| ~^~~~~~~~~
# | 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... |