Submission #945850

#TimeUsernameProblemLanguageResultExecution timeMemory
945850hihihihawKnapsack (NOI18_knapsack)C++17
73 / 100
202 ms262144 KiB
#pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define pii pair<int,int> #define sz(v) (int)v.size() #define fi first #define se second #define INF 1223372036854775807 #define MOD 1000000007 #define cint(x) int x;cin>>x; #define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i]; #define coutarr(a) for (auto d:a) cout<<d<<" "; cout<<endl; #define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl; #define AYBERK_SARICA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define endl '\n' #define ld long double #define mid (start+end)/2 int t=1; int testCase=1; void solve(){ int s,n; cin>>s>>n; int v[n],w[n],k[n]; for (int i=0;i<n;i++) cin>>v[i]>>w[i]>>k[i]; vector<pii> z[s+1]; for (int i=0;i<n;i++) z[w[i]].pb({v[i],k[i]}); for (int i=0;i<=s;i++){ sort(z[i].begin(),z[i].end()); reverse(z[i].begin(),z[i].end()); } vector<int> vRiyal; vector<int> wRiyal; for (int i=1;i<=s;i++){ int a=0; int p=0; while (a<=s/i+3){ if (p>=sz(z[i]))break; if (z[i][p].se==0) p++; else{ vRiyal.pb(z[i][p].fi); wRiyal.pb(i); z[i][p].se--; a++; } } } int nn=sz(vRiyal); int dp[nn][s+1]={}; for (int j=1;j<=s;j++){ if (j>=wRiyal[0]) dp[0][j]=vRiyal[0]; } for (int i=1;i<nn;i++){ for (int j=1;j<=s;j++){ if (j>=wRiyal[i]) dp[i][j]=max(dp[i-1][j-wRiyal[i]]+vRiyal[i],dp[i-1][j]); else dp[i][j]=dp[i-1][j]; } } cout<<dp[nn-1][s]; } int32_t main(){ //AYBERK_SARICA; if (0){ freopen("in.txt", "r", stdin); //freopen("wormsort.out", "w", stdout); } if (t==1) solve(); else{ cin>>t; while (t--) solve(); } return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:86:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   freopen("in.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...