Submission #1121432

#TimeUsernameProblemLanguageResultExecution timeMemory
1121432HiepVu217Knapsack (NOI18_knapsack)C++17
0 / 100
18 ms23892 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 17; int n, s, 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 >> n >> s; for (int i = 1; i <= n; ++i) { cin >> w[i] >> v[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...