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;
//#define int long long
#define ar array
const int mxn = 1e6 + 5;
int s,dp[2005],n;
map<int,vector<pair<int,int>>> mp;
vector<ar<int,3>> vt(1);
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> s >> n;
for(int i = 1;i <= n;i++) {
int v,w,k;
cin >> v >> w >> k;
if(w > s || k <= 0) continue;
ar<int,3> arr;
arr[0] = v,arr[1] = w,arr[2] = k;
//vt.push_back(arr);
mp[w].push_back({v,k});
}
for(auto x : mp) {
sort(x.second.begin(),x.second.end(), greater<pair<int,int>> ());
int limit = (s/x.first) + 2,cnt = 0,itr = 0;
while(cnt <= limit && itr < x.second.size()) {
if(cnt + x.second[itr].second <= limit) {
vt.push_back({x.second[itr].first , x.first , x.second[itr].second});
cnt += x.second[itr].second;
itr++;
}
else {
if(cnt == limit) break;
vt.push_back({x.second[itr].first , x.first , limit - cnt});
break;
}
}
}
n = vt.size() - 1;
for(int i = 1;i <= n;i++) {
int v = vt[i][0] , w = vt[i][1] , k = vt[i][2];
for(int j = s;j >= 0;j--) {
for(int t = 1;t * w <= j && t <= k;t++) {
if(dp[j - t * w] + t * v >= dp[j]) dp[j] = dp[j - t * w] + t * v;
}
}
}
cout << dp[s] << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:24:35: 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(cnt <= limit && itr < x.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... |