Submission #812667

#TimeUsernameProblemLanguageResultExecution timeMemory
812667AliVuKnapsack (NOI18_knapsack)C++17
73 / 100
1090 ms3668 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define el '\n' const int maxn = 2e3 + 3, N = 1e5 + 5; int dp[2][maxn], W[N], V[N], K[N]; void Solve() { int S, n; cin >> S >> n; for(int i = 1; i <= n; i++) { cin >> V[i] >> W[i] >> K[i]; K[i] = min(K[i], maxn); } for(int i = 0; i < 2; i++) for(int j = 0; j < maxn; j++) { dp[i][j] = -1; if(j == 0) dp[i][j] = 0; } for(int k = 1; k <= n; k++) { int i = k % 2; bitset<maxn> BIT; BIT[0] = 1; int p = 1; while(p < K[k]) { BIT |= (BIT << p); K[k] -= p; p <<= 1; } BIT |= (BIT << K[k]); for(int j = 0; j < maxn; j++) for(int z = 0; z < maxn; z++) if(BIT[z]) { if(j + z * W[k] < maxn && dp[i][j] != -1) dp[1 - i][j + z * W[k]] = max(dp[1 - i][j + z * W[k]], dp[i][j] + z * V[k]); dp[1 - i][j] = max(dp[1 - i][j], dp[i][j]); } } int ans = 0; for(int i = 1; i <= S; i++) ans = max({ans, dp[0][i], dp[1][i]}); cout << ans; } signed main() { ios_base::sync_with_stdio(false);cin.tie(0); // #ifndef ONLINE_JUDGE // freopen("inp.txt", "r", stdin);freopen("out.txt", "w", stdout); // #endif int t = 1;// cin >> t; while(t--) Solve(); }
#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...