Submission #638044

#TimeUsernameProblemLanguageResultExecution timeMemory
638044zandlerKnapsack (NOI18_knapsack)C++14
73 / 100
8 ms724 KiB
#include <bits/stdc++.h>
using namespace std;
int dp[2001];
pair<pair<int,int>,int> a[2001];
int pref[2001][2001];
int lst[2001];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int s, n; cin >> s >> n;
    for(int i=0;i<n;i++){
        cin >> a[i].first.first >> a[i].first.second;
        cin >> a[i].second;
        
    }
    sort(a, a+n);
    for(int i=n-1;i>=0;i--){
        //cout << lst[a[i].first.second] << ' ';
        int tmp = lst[a[i].first.second]+a[i].second;
        for(int j = lst[a[i].first.second]+1; j<=tmp ;j++){
            if(j*a[i].first.second > s)break;
            pref[a[i].first.second][j]=pref[a[i].first.second][j-1];
            pref[a[i].first.second][j]+=a[i].first.first;
            lst[a[i].first.second]=j;
        }
        //lst[a[i].first.second]=min(s, lst[a[i].first.second]+a[i].second);
    }
    for(int i=1;i<=s;i++){
        for(int j=s;j>=0;j--){
            for(int x=lst[i]; x>=1; x--){
                if(j+x*i<= s){
                    dp[j+x*i]=max(dp[j+x*i],dp[j]+pref[i][x]);
                }
            }
        }
    }
    cout << *max_element(dp,dp+s+1) << '\n';
    return 0;
}
#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...