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;
#define ll long long
const int mod = 1000000007;
const int INF = 1e9;
const int N = (int)1e5+5;
vector<int>adj[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//
int limit, type_num; cin >> limit >> type_num;
map<int, vector<pair<int, int>>>by_weight;
for(int i = 0; i < type_num; i++){
int val, weight, amt;
cin >> val >> weight >> amt;
if(weight <= limit && amt > 0){
by_weight[weight].push_back({val, amt});
}
}
vector<vector<ll>>best(by_weight.size()+1, vector<ll>(limit+1, INT32_MIN));
best[0][0] = 0;
int at = 1;
for(auto& [w, items] : by_weight){
sort(items.begin(), items.end(), greater<pair<int, int>>());
for(int i = 0; i <= limit; i++){
best[at][i] = best[at-1][i];
int copies = 0;
int type_at = 0;
int cur_used = 0;
ll profit = 0;
while((copies+1)*w <= i && type_at < items.size()){
copies++;
profit += items[type_at].first;
if(best[at-1][i-copies*w] != INT32_MIN){
best[at][i] = max(best[at-1][i], best[at-1][i-copies*w]+profit);
}
cur_used++;
if(cur_used == items[type_at].second){
cur_used = 0;
type_at++;
}
}
}
at++;
}
cout << *max_element(best.back().begin(), best.back().end()) <<'\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:33:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | while((copies+1)*w <= i && type_at < items.size()){
| ~~~~~~~~^~~~~~~~~~~~~~
# | 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... |