Submission #491328

#TimeUsernameProblemLanguageResultExecution timeMemory
491328DDTerziev04Knapsack (NOI18_knapsack)C++14
0 / 100
0 ms332 KiB
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int MAXN=1e5, MAXS=2000; long long dp[MAXS+1]; int p[MAXN], w[MAXN], h[MAXN], cnt[MAXS]; vector<pair<int, int> > v[MAXS]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long s, n; cin >> s >> n; for(int i=0; i<=s; i++) { cnt[i]=0; } for(int i=0; i<n; i++) { int a, b, c; cin >> a >> b >> c; v[b].push_back({a, c}); cnt[b]+=c; } n=0; for(int i=0; i<=s; i++) { sort(v[i].begin(), v[i].end()); while(cnt[i]!=0 && cnt[i]>s/i) { cnt[i]-=v[i].back().second; v[i].pop_back(); } for(pair<int, int> pr:v[i]) { p[n]=pr.first, w[n]=i, h[n]=pr.second; n++; } } for(int i=0; i<=s; i++) { dp[i]=-1; } dp[0]=0; long long ans=0; for(int i=0; i<n; i++) { for(int j=s; j>=0; j--) { for(int u=1; u<=h[i] && u*w[i]<=j; u++) { if(dp[j-u*w[i]]==-1) { continue; } dp[j]=max(dp[j], dp[j-u*w[i]]+u*p[i]); } ans=max(ans, dp[j]); } } cout << ans << "\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...