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;
int main() {
int S, N;
cin >> S >> N;
map<int, vector<pair<int, int>>> by_weight;
for(int t = 0; t < N; t++) {
int V, W, K;
cin >> V >> W >> K;
by_weight[W].push_back({V, K});
}
vector<vector<int>> dp(by_weight.size()+1, vector<int>(S+1));
int res = 0;
int i = 1;
for(auto& hi : by_weight) {
auto w = hi.first;
auto items = hi.second;
sort(items.begin(), items.end(), greater<pair<int, int>>());
for(int s = 1; s <= S; s++) {
dp[i][s] = dp[i-1][s];
int curr_i = 0, curr_cnt = 0, val = 0;
for(int cnt = 1; cnt * w <= s && curr_i < items.size(); cnt++) {
val += items[curr_i].first;
if(++curr_cnt == items[curr_i].second) {
curr_cnt = 0;
curr_i++;
}
dp[i][s] = max(dp[i][s], dp[i-1][s-cnt*w] + val);
}
}
res = max(dp[i][S], res);
i++;
}
cout << res << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:28:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int cnt = 1; cnt * w <= s && curr_i < items.size(); cnt++) {
| ~~~~~~~^~~~~~~~~~~~~~
# | 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... |