Submission #1121433

#TimeUsernameProblemLanguageResultExecution timeMemory
1121433HiepVu217Knapsack (NOI18_knapsack)C++17
100 / 100
70 ms28492 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 17; int s, n, v[N], w[N], k[N], sz; long long f[N], c; vector <pair <int, int>> sw[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s >> n; for (int i = 1; i <= n; ++i) { cin >> v[i] >> w[i] >> k[i]; sw[w[i]].push_back({v[i], k[i]}); } n = 0; for (int ww = 1; ww <= 1e6; ++ww) { sort (sw[ww].begin(), sw[ww].end(), greater<>()); int c = 0; for (auto [vv, kk]: sw[ww]) { if (c < s / ww) { ++n; v[n] = vv, w[n] = ww, k[n] = min (kk, s / ww - c); c += min (kk, s / ww - c); continue; } break; } } for (int i = 1; i <= n; ++i) { for (int j = s; j >= 1; --j) { for (int t = 1; t <= k[i] && t * w[i] <= j; ++t) { f[j] = max (f[j], f[j - t * w[i]] + 1LL * t * v[i]); } } } cout << f[s]; }
#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...