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 s second
#define f first
int main(){
int s, n;
cin >> s >> n;
map<int, vector<pair<int,int>>> items;
for(int i=0; i<n; i++){
int v, w, k; cin >> v >> w >> k;
items[w].push_back({v, k});
}
vector<vector<ll>> dp(items.size()+1, vector<ll>(s+1));
dp[0][0] = 0;
int at = 1;
for(auto item: items){
sort(item.s.begin(), item.s.end(), greater<pair<int,int>>());
for(int i=0; i<=s; i++){
dp[at][i] = dp[at-1][i];
int copy = 0, type_at = 0, curr_used = 0;
ll profit = 0;
while((copy+1)*item.first <= i and type_at < item.s.size()){
copy++;
profit += item.s[type_at].f;
dp[at][i] = max(dp[at][i], dp[at-1][i-copy*item.f] + profit);
curr_used++;
if(curr_used == item.s[type_at].s){
curr_used = 0;
type_at++;
}
}
}
at++;
}
cout << dp[items.size()][s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:23: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]
23 | while((copy+1)*item.first <= i and type_at < item.s.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... |