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;
ll dp[2005], ans;
tuple<int, int, int> arr[100005];
priority_queue<int, vector<int>, greater<int>> pq[2005];
vector<pair<int, int>> item;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int s, n, k, v, w;
cin >> s >> n;
for (int i = 0; i < n; i++) {
cin >> v >> w >> k;
arr[i] = make_tuple(v, w, k);
}
sort(arr, arr + n, greater<tuple<int, int, int>>());
for (int i = 0; i < n; i++) {
tie(v, w, k) = 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& p : item) {
tie(w, v) = p;
for (int j = s; j >= 0; j--) {
if (dp[j] == 0 && j != 0)
continue;
if (w + j <= s)
dp[w + j] = max(dp[w + j], dp[j] + v);
}
}
for (int i = 0; i <= s; i++) {
ans = max(ans, dp[i]);
}
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:30: 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]
27 | if (pq[w].size() > s / w)
| ~~~~~~~~~~~~~^~~~~~~
# | 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... |