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;
typedef long long ll;
const int N = 1e5, S = 2e3 + 1;
ll n, s, dp[S];
vector<pair<ll,ll>> W[S];
int main(){
cin.tie(0) -> ios_base::sync_with_stdio(0);
cin >> s >> n;
if(n == 1)
{
long long v, w, k;
cin >> v >> w >> k;
cout << min(k, s / w) * v;
return 0;
}
for(int i=0;i<n;i++)
{
int v, w, k;
cin >> v >> w >> k;
W[w].push_back({v, k});
}
ll mx = 0;
for(int i=1;i<=s;i++)
{
if(W[i].empty()) continue;
sort(W[i].rbegin(), W[i].rend());
for(int j=s;j>=0;j--)
{
ll out_ptr = 0, in_taken = 0, all_taken = 0, sm = 0;
while(out_ptr < W[i].size() && j - (all_taken + 1) * i >= 0)
{
all_taken++;
in_taken++;
sm += W[i][out_ptr].first;
dp[j] = max(dp[j], dp[j - all_taken * i] + sm);
if(in_taken == W[i][out_ptr].second)
{
in_taken = 0;
out_ptr++;
}
}
mx = max(mx, dp[j]);
}
}
// for(int i=0;i<=s;i++)
// {
// cout << dp[i] << " ";
// }
// cout << '\n';
cout << mx;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:35:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while(out_ptr < W[i].size() && j - (all_taken + 1) * i >= 0)
| ~~~~~~~~^~~~~~~~~~~~~
# | 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... |