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<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
int main(){
int s , n;
cin >> s >> n;
vector<vector<vector<int>>> products(s + 1);
for(int i = 0; i < n; i++){
int v , w , k;
cin >> v >> w >> k;
products[w].push_back({v , k , w});
}
vector<vector<int>> finalProduct;
for(int i = 1; i <= s; i++){
sort(products[i].begin() , products[i].end());
int sum = 0;
for(int j = products[i].size() - 1; j >= 0; j--){
if(sum + products[i][j][2] * products[i][j][1] > s){
int remain = s - sum;
finalProduct.push_back({products[i][j][0] , min(remain / products[i][j][2] , products[i][j][1]) , products[i][j][2]});
if(remain % (products[i][j][2]) != 0){
if(finalProduct.back()[1] < products[i][j][1]) finalProduct.back()[1]++;
}
}
else {
sum += products[i][j][2] * products[i][j][1];
finalProduct.push_back({products[i][j][0] , products[i][j][1] , products[i][j][2]});
}
}
}
// int prCount = 0;
// for(int i= 0; i < finalProduct)
// vector<vector<int>>
// cout << "test: " << endl;
// for(int i = 0; i < finalProduct.size(); i++){
// cout << finalProduct[i][0] << ' ' << finalProduct[i][2] << ' ' << finalProduct[i][1] << endl;
// }
// cout << "end Test" << endl;
vector<vector<int>> dp(finalProduct.size() + 1);
for(int i = 0; i < finalProduct.size() + 1; i++) dp[i].resize(s + 1);
for(int i = 0; i < s + 1; i++) dp[0][i] = 0;
for(int i = 0; i <= finalProduct.size(); i++){
dp[i][0] = 0;
}
for(int i = 1; i < finalProduct.size() + 1; i++){
for(int j = 1; j < s + 1; j++){
dp[i][j] = dp[i - 1][j];
for(int u = 1; u <= finalProduct[i - 1][1]; u++){
if(j - (u * finalProduct[i - 1][2]) >= 0) dp[i][j] = max(dp[i][j] , dp[i - 1][j - (u * finalProduct[i - 1][2])] + u * finalProduct[i - 1][0]);
else break;
}
}
}
int ans = 0;
for(int i = 0; i < s + 1; i++){
ans = max(ans , dp[finalProduct.size()][i]);
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < finalProduct.size() + 1; i++) dp[i].resize(s + 1);
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 0; i <= finalProduct.size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 1; i < finalProduct.size() + 1; i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |