Submission #745277

#TimeUsernameProblemLanguageResultExecution timeMemory
745277Azm1tKnapsack (NOI18_knapsack)C++17
73 / 100
137 ms262144 KiB
#include "bits/stdc++.h" using namespace std; #define forn for (int i = 0; i < n; i++) #define int long long #define ed "\n" #define cyes cout << "YES\n" #define cno cout << "NO\n" #define pb push_back #define deb cout << "Hi\n" #define pint pair<int, int> const long long MOD = 1000000007; void dbg_out(){cerr<<"\n";} template<typename Head,typename... Tail> void dbg_out(Head H,Tail... T){cerr<<' '<<H;dbg_out(T...);} #define debug(...) cerr<<"("<<#__VA_ARGS__<<"):",dbg_out(__VA_ARGS__) //-------------------------------------------------------------------------------------------------------------------------------------------------------------------// int32_t main(){ std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int w, n; cin>>w>>n; vector<array<int, 3>> v; forn{ int vi, wi, ki; cin>>vi>>wi>>ki; v.pb({vi, wi, ki}); } vector<array<int, 2>> v2; for(auto val: v){ auto[vi, wi, ki] = val; for(int i = 0; i <= 34; i++){ int x = (1LL << i); if(ki < x) break; ki -= x; v2.pb({vi*x, wi*x}); } if(ki > 0) v2.pb({vi*ki, wi*ki}); } n = v2.size(); int dp[n][w+1]; for(int i = 0; i < n; i++){ for(int j = 0; j <= w; j++) dp[i][j] = 0; } for(int i = 0; i < n; i++){ for(int j = 1; j <= w; j++){ if(i == 0){ if(v2[i][1] == j) dp[i][j] = v2[i][0]; } else{ dp[i][j] = dp[i-1][j]; if(j - v2[i][1] >= 0) dp[i][j] = max(dp[i][j], dp[i-1][j-v2[i][1]] + v2[i][0]); } } } // forn{ // for(int j = 0; j <= w; j++) cout << dp[i][j] << " "; // cout << ed; // } int ans = 0; for(int i = 0; i <= w; i++) ans = max(ans, dp[n-1][i]); cout << ans << ed; }
#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...