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 int ll
#define pii pair<int,int>
#define tiii tuple<int,int,int>
using namespace std;
const ll INF = numeric_limits<ll>::max() / 2;
int32_t main() {
int s, n;
cin >> s >> n;
//vector<tiii> items(n);
vector<vector<tiii>> itemsPerWeight(s + 1);
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
//items[i] = {v, w, k};
if (w <= s) itemsPerWeight[w].push_back({ v, w, k });
}
vector<int> dp(s + 1, -INF);
dp[0] = 0;
// iterate through each block
for (int i = 1; i <= s; i++) {
// generate the block for this weight
priority_queue<pii> pq;
for (auto p : itemsPerWeight[i]) {
pq.push({ get<0>(p), get<2>(p) });
}
vector<int> values;
values.push_back(0);
for (int k = 1; k <= s / i; k++) {
if (pq.empty()) break;
auto p = pq.top();
pq.pop();
values.push_back(values[k - 1] + p.first);
if (p.second > 1) pq.push({ p.first,p.second-1 });
}
// do dp transitions
for (int k = s; k > 0; k--) {
for (int j = 0; j < values.size(); j++) {
if (k - (j * i) < 0) break;
dp[k] = max(dp[k], dp[k - (j * i)] + values[j]);
}
}
}
int ma = 0;
for (int i = 0; i <= s; i++) {
ma = max(ma, dp[i]);
}
cout << ma << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:45:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int j = 0; j < values.size(); j++) {
| ~~^~~~~~~~~~~~~~~
# | 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... |