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 fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define ff first
#define ss second
#define endl "\n"
#define pb(x) push_back(x)
#define pp() pop_back()
#define inf LONG_MAX
#define dvg(x) cout<<#x<<" "<<x<<endl;
#define dvg2(x, y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define dvgv(x) cout<<#x<<" { "; for(auto i : x) {cout<<i<<" ";} cout<<"}"<<endl;
#define dvgp(x) cout<<#x" {"<<x.ff<<", "<<x.ss<<"}"<<endl;
int dx[] {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int const N = 1e5 + 10;
int main()
{
fastio();
int w, n;
cin>>w>>n;
map<int, vector<pll>> v;
for(int i = 0; i < n; i++){
int wt;
ll val, amt;
cin>>val>>wt>>amt;
v[wt].push_back({val, amt});
}
vector<ll> dp(vector<ll>(w + 1, INT32_MIN));
dp[0] = 0;
for(auto &[wt, items] : v){
vector<ll> best = dp;
sort(items.begin(), items.end(), greater<pll>());
for(int i = 0; i <= w; i++){
int copies = 0;
int type_at = 0;
int cur_used = 0;
ll profit = 0;
while((copies + 1)*wt <= i && type_at < items.size()){
copies++;
profit += items[type_at].ff;
if(dp[i - copies*wt] != INT32_MIN){
best[i] = max(best[i], dp[i - copies*wt] + profit);
}
cur_used++;
if(cur_used == items[type_at].ss){
cur_used = 0;
type_at++;
}
}
}
dp = best;
}
cout<<*max_element(dp.begin(), dp.end())<<endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:44:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto &[wt, items] : v){
| ^
knapsack.cpp:52:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while((copies + 1)*wt <= 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... |