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;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
int main(){
int s, n;
cin >> s >> n;
vector<int> val(s+1, -1);
vector<vector<pair<int, int>>> weights(s+1);
val[0] = 0;
for (int i = 0; i < n; i++){
int v, w, k;
cin >> v >> w >> k;
weights[w].push_back({v, k});
}
for (int i = 0; i <= s; i++) sort(weights[i].begin(), weights[i].end(), greater<pair<int, int>>());
for (int i = 0; i <= s; i++){
if (weights[i].size() == 0) continue;
for (int j = s; j > -1; j--){
int value = 0;
int ptr = 0;
int curr = weights[i][ptr].second;
for (int x = 1; x * i <= j && ptr < weights[i].size(); x++){
value += weights[i][ptr].first;
curr--;
if (curr == 0) ptr++, curr = weights[i].size() == ptr ? 0 : weights[i][ptr].second;
if (val[j-x*i] == -1) continue;
val[j] = max(val[j-x*i]+value, val[j]);
}
}
}
cout << (*max_element(val.begin(), val.end())) << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int x = 1; x * i <= j && ptr < weights[i].size(); x++){
| ~~~~^~~~~~~~~~~~~~~~~~~
knapsack.cpp:30:64: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
30 | if (curr == 0) ptr++, curr = weights[i].size() == ptr ? 0 : weights[i][ptr].second;
| ~~~~~~~~~~~~~~~~~~^~~~~~
# | 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... |