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 int long long
#define pb push_back
#define mp make_pair
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e17*2;
const int N = 2e5 + 5;
const int L = 1e4 + 5;
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int s, n;
cin>>s>>n;
vector<int> items[s+1];
for(int i = 0; i < n; i++){
int v, w, k;
cin>>v>>w>>k;
/*
for(int j = 1; j <= min(k, s/w); j++){
items[w].pb(v);
}
if(items[w].size() > L){
sort(items[w].rbegin(), items[w].rend());
while(items[w].size() * w > s){
items[w].pop_back();
}
}
*/
vector<int> cnt(13);
cnt[0] = min(k, s/w + 1);
for(int i = 0; i < 12; i++){
int x = (cnt[i] - 1)/2;
cnt[i] -= x*2;
cnt[i+1] += x;
}
for(int i = 0; i < 12; i++){
if((1 << i) * w > s) break;
for(int j = 0; j < cnt[i]; j++){
items[w * (1 << i)].pb(v * (1 << i));
}
}
}
vector<pair<int, int> > arr;
for(int i = 1; i <= s; i++){
sort(items[i].rbegin(), items[i].rend());
while(items[i].size() * i > s){
items[i].pop_back();
}
for(auto it: items[i]) arr.pb(mp(i, it));
}
vector<int> dp(s+1);
for(int i = 0; i < arr.size(); i++){
for(int j = s - arr[i].first; j >= 0; j--){
dp[j + arr[i].first] = max(dp[j + arr[i].first], dp[j] + arr[i].second);
}
}
int ans = 0;
for(int i = 0; i <= s; i++){
ans = max(ans, dp[i]);
}
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:54:35: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
54 | while(items[i].size() * i > s){
| ~~~~~~~~~~~~~~~~~~~~^~~
knapsack.cpp:61:22: 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]
61 | for(int i = 0; i < arr.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... |