Submission #578175

#TimeUsernameProblemLanguageResultExecution timeMemory
578175lamKnapsack (NOI18_knapsack)C++17
100 / 100
103 ms36464 KiB
#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define maxn 2010 #define int long long #define taskname "spainting" using namespace std; int s,n; int dp[maxn][maxn]; vector <pair<int,int>> a[maxn]; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // freopen(taskname".in","r",stdin); // freopen(taskname".out","w",stdout); cin>>s>>n; for (int i=1; i<=n; i++) { int v,w,k; cin>>v>>w>>k; if (w<=s&&k>0) a[w].push_back({v,k}); } for (int i=0; i<=s; i++) for (int j=0; j<=s; j++) dp[i][j]=-1e18; dp[0][0]=0; for (int i=1; i<=s; i++) { sort(a[i].begin(),a[i].end(),greater<pair<int,int>>()); for (int j=0; j<=s; j++) { dp[i][j]=dp[i-1][j]; if (a[i].empty()) continue; int cnt=0; int used=0; int it=0; int sum=0; while ((cnt+1)*i<=j&&it<(int)(a[i].size())) { cnt++; sum+=a[i][it].first; if (dp[i-1][j-cnt*i]!=-1e18) dp[i][j]=max(dp[i][j],dp[i-1][j-cnt*i]+sum); used++; if (used==a[i][it].second) { used=0; it++; } } } } int ans=-1e18; for (int i=0; i<=s; i++) for (int j=0; j<=s; j++) ans=max(ans,dp[i][j]); cout<<ans; }
#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...