Submission #943581

#TimeUsernameProblemLanguageResultExecution timeMemory
943581tnknguyen_Knapsack (NOI18_knapsack)C++14
100 / 100
84 ms31600 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define pll pair<long long, long long> const int sz = 1e6 + 5; vector<pll> W[sz]; long long s, n; long long f[sz]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("main.inp", "r", stdin); //freopen("main.out", "w", stdout); cin>>s>>n; for(int i=1;i<=n;++i){ long long v, w, k; cin>>v>>w>>k; W[w].push_back({v, k}); } f[0] = 1; for(int i=1;i<=s;++i){ if(W[i].size() == 0){ continue; } sort(W[i].begin(), W[i].end(), greater<pll>()); for(int j=s;j>=i;--j){ long long sum = 0; int t = 0, cur = 1; for(int k=1;i*k<=j;++k){ sum += W[i][t].first; if(f[j - i*k] != 0){ f[j] = max(f[j], f[j - i*k] + sum); } if(cur == W[i][t].second){ cur = 1; t++; if(t >= (int)W[i].size()){ break; } } else{ cur++; } } cout<<endl; } } long long ans = 0; for(int i=1;i<=s;++i){ ans = max(ans, f[i]); } cout<<ans - 1; //f[0] = 1. 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...