Submission #638058

#TimeUsernameProblemLanguageResultExecution timeMemory
638058zandlerKnapsack (NOI18_knapsack)C++14
100 / 100
71 ms11612 KiB
#include <bits/stdc++.h> using namespace std; int dp[2001]; pair<pair<int,int>,int> a[(int)1e6+1]; 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 = min(lst[a[i].first.second]+a[i].second, s/a[i].first.second); for(int j = lst[a[i].first.second]+1; j<=tmp ;j++){ 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...