Submission #920656

#TimeUsernameProblemLanguageResultExecution timeMemory
920656gustavo_dKnapsack (NOI18_knapsack)C++17
73 / 100
1063 ms19028 KiB
// https://oj.uz/problem/view/NOI18_knapsack > p83 e p199 #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("03") typedef long long ll; const int MAXS = 2000; const int MAXLOG = 12; ll dp[2][MAXS+1]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int S, n; cin >> S >> n; vector<ll> val(n); vector<int> w(n); vector<int> K(n); vector<pair<int, ll>> v; // peso, valor v.reserve(n*MAXLOG); int tmpn=n; for (int i=0; i<n; i++) { cin >> val[i] >> w[i] >> K[i]; K[i] = min(K[i], (S/w[i])); bool fi = true; int other = K[i]; for (int b=MAXLOG; b>=0; b--) { if (fi and ((1 << b) & K[i]) != 0) { fi = false; other -= ((1 << b) - 1); } else if (!fi) { tmpn++; v.push_back({(1 << b) * w[i], (1LL << b) * val[i]}); } } v.push_back({other * w[i], other * val[i]}); } for (int i=1; i<=tmpn; i++) { for (int P=1; P<v[i-1].first; P++) { dp[i&1][P] = max(dp[(i&1)^1][P], dp[i&1][P-1]); } for (int P=v[i-1].first; P<=S; P++) { dp[i&1][P] = max({ dp[(i&1)^1][P-v[i-1].first] + v[i-1].second, dp[(i&1)^1][P], dp[i&1][P-1] }); } } cout << dp[tmpn&1][S] << endl; 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...