Submission #1279672

#TimeUsernameProblemLanguageResultExecution timeMemory
1279672hanguyendanghuyKnapsack (NOI18_knapsack)C++20
100 / 100
153 ms2816 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
constexpr ll MAXN=3e5+5,MAXV=3e5,MOD=1e9+7,INF=1e18;
ll n,m,i,j,p,k,ans,dem,st,en,dp[MAXN];
struct h{
    ll weight,cost,cnt;
} a[MAXN];
bool cmp(h x,h y){
    pair<ll,ll> d={x.cost,x.cnt},p={y.cost,y.cnt};
    return (x.weight<y.weight||(x.weight==y.weight&&d>p));
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>m>>n;
    for(i=1;i<=n;i++){
        cin>>a[i].cost>>a[i].weight>>a[i].cnt;
    }
    sort(a+1,a+n+1,cmp);
    ll dem=0;
    for(i=1;i<=n;i++){
        ll weight=a[i].weight,cost=a[i].cost,cnt=a[i].cnt;
        if(a[i].weight!=a[i-1].weight) dem=0;
        if(m/weight<dem) continue;
        cnt=min(cnt,m/weight-dem);
        dem+=cnt;
        for(j=m;j>=weight;j--){
            for(ll k=1;k<=cnt&&j-k*weight>=0;k++){
                dp[j]=max(dp[j],dp[j-k*weight]+cost*k);
            }
        }
    }
    cout<<*max_element(dp+1,dp+m+1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...