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
using namespace std;
const ll N = 1e6;
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ll s, n;
cin >> s >> n;
map<ll, vector<pair<ll, ll>>> mp;
for(int i = 0; i<n; i++){
ll v, w, k;
cin >> v >> w >> k;
if(w<=s && k>0){
mp[w].push_back({v, k});
}
}
ll m = mp.size();
ll ans = 0;
vector<vector<ll>> dp(m+1, vector<ll>(s+1, LONG_LONG_MIN));
dp[0][0] = 0;
ll i = 1;
for(auto x: mp){
auto items = x.second;
auto w = x.first;
sort(items.begin(), items.end(), greater<pair<ll, ll>>());
for(int j=0; j<=s; j++){
dp[i][j] = dp[i-1][j];
ll copies = 0, k = 0, cur = 0, profit = 0;
while((copies+1)*w<=j && k<items.size()){
copies++;
profit+=items[k].first;
if(dp[i-1][j-copies*w]!=LONG_LONG_MIN){
dp[i][j] = max(dp[i][j], dp[i-1][j-copies*w] + profit);
}
cur++;
if(cur==items[k].second){
cur = 0;
k++;
}
}
ans = max(dp[i][j],ans);
}
i++;
}
cout << ans << endl;
return 0;
}
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((copies+1)*w<=j && k<items.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... |