제출 #860756

#제출 시각아이디문제언어결과실행 시간메모리
860756noyancanturkKnapsack (NOI18_knapsack)C++17
73 / 100
37 ms2720 KiB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define lim 100000
using namespace std;
const int mod=1000000007ll;

void solve(){
    int s,n;
    cin>>s>>n;
    priority_queue<pair<int,int>>weight[s+1];
    for(int i=0;i<n;i++){
        int v,w,k;
        cin>>v>>w>>k;
        weight[w].push({v,k});
    }
    int dp[s+1];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=s;i++){
        int cani=s/i,j=0;
        while(cani&&weight[i].size()){
            auto p=weight[i].top();
            weight[i].pop();
            int to=min(p.second,cani);
            if(to){
                if(!j){
                    for(int j=s-i;0<=j;j--){
                        dp[j+i]=max(dp[j+i],dp[j]+p.first);
                    }
                    to--;
                    cani--;
                    j++;
                }
                for(;j<=to;j<<=1){
                    for(int l=s-i*j;0<=l;l--){
                        dp[l+i*j]=max(dp[l+i*j],dp[l]+p.first*j);
                    }
                    to-=j;
                    cani-=j;
                }
                for(int l=s-i*to;0<=l;l--){
                    dp[l+i*to]=max(dp[l+i*to],dp[l]+p.first*to);
                }
                cani-=to;
            }
        }
    }
    cout<<*max_element(dp,dp+s+1);
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
#ifdef Local  
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
    int t=1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}
#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...