Submission #917510

#TimeUsernameProblemLanguageResultExecution timeMemory
917510PieArmyKnapsack (NOI18_knapsack)C++17
100 / 100
73 ms19536 KiB
#include <bits/stdc++.h> #define fr first #define sc second #define pb push_back #define endl '\n'; typedef long long ll; const ll inf=2000000000000000005; using namespace std; ll pie(ll army){return (1ll<<army);} ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;} void code(){ int k,n;cin>>k>>n; map<int,vector<pair<int,int>>>mp; vector<int>w; for(int i=0;i<n;i++){ int x,y,z;cin>>x>>y>>z; mp[y].pb({x,z}); if(mp[y].size()==1)w.pb(y); } int m=w.size(); vector<vector<int>>dp(m+1,vector<int>(k+1,0)); for(int i=0;i<m;i++){ vector<pair<int,int>>&v=mp[w[i]]; int s=v.size(); sort(v.begin(),v.end(),[&](const pair<int,int> &x,const pair<int,int> &y){return x.fr>y.fr;}); for(int j=0;j<=k;j++){ dp[i+1][j]=max(dp[i+1][j],dp[i][j]); int carp=0,sum=0; for(int pos=0;pos<s;pos++){ int cur=v[pos].sc; while(cur--){ carp++; if(carp*w[i]+j>k)break; sum+=v[pos].fr; dp[i+1][carp*w[i]+j]=max(dp[i+1][carp*w[i]+j],dp[i][j]+sum); } if(carp*w[i]+j>k)break; } } } int ans=0; for(int x:dp[m])ans=max(ans,x); cout<<ans; } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);} int t=1; if(!t)cin>>t; while(t--){code();cout<<endl;} return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:49:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~
knapsack.cpp:49:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~
#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...