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
#define ti tuple<int, int, int>
using namespace std;
int s, n, k, v, w, cw, cv;
ll dp[2010], ans;
ti arr[1000010];
priority_queue<int, vector<int>, greater<int>> pq[2010];
vector<pair<int, int>> item;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> s >> n;
for(int i=0; i<n; i++) {
cin >> v >> w >> k;
arr[i] = {v, w, k};
}
sort(arr, arr+n, greater<ti>());
for(int i=0; i<n; i++) {
v = get<0>(arr[i]);
w = get<1>(arr[i]);
k = get<2>(arr[i]);
k = min(k, s/w);
while(k--) {
pq[w].push(v);
if(pq[w].size() > s/w) break;
}
}
for(int i=1; i<=s; i++) {
while(!pq[i].empty()) {
item.push_back({i, pq[i].top()});
pq[i].pop();
}
}
for(auto i: item) {
cw = i.first;
cv = i.second;
for(int j=s; j>=0; j--) {
if(dp[j] == 0 && j != 0) continue;
if(cw + j <= s) dp[cw+j] = max(dp[cw+j], dp[j]+cv);
}
}
for(int i=0; i<=s; i++) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:28:29: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | if(pq[w].size() > s/w) break;
| ~~~~~~~~~~~~~^~~~~
# | 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... |