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
#define pb push_back
#define pf push_front
#define pi pair<int,int>
const int MOD = 1e9+7;
int main(){
cin.tie(0)->sync_with_stdio(false);
int S, N;
cin >> S >> N;
map<int, vector<pi>> bw;
for(int i=0; i<N; i++){
int v, w, k;
cin >> v >> w >> k;
bw[w].pb({v, k});
}
vector<vector<ll>> dp(bw.size()+1, vector<ll>(S+1, -1e9));
dp[0][0] = 0;
int at = 1;
for(auto &[w, item]: bw){
sort(item.begin(), item.end(), greater<pi>());
for(int i=0; i<=S; i++){
dp[at][i] = dp[at-1][i];
int cur = 0;
int type_at = 0;
int used = 0;
ll profit = 0;
while((cur+1)*w <= i and type_at < item.size()){
cur++;
profit += item[type_at].first;
dp[at][i] = max(dp[at][i], dp[at-1][i-cur*w] + profit);
used++;
if(used == item[type_at].second){
used = 0;
type_at++;
}
}
}
at++;
}
cout << *max_element(dp.back().begin(), dp.back().end()) << '\n';
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:33:37: 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((cur+1)*w <= i and type_at < item.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... |