Submission #640319

#TimeUsernameProblemLanguageResultExecution timeMemory
640319htphong0909Knapsack (NOI18_knapsack)C++17
100 / 100
133 ms27472 KiB
#include <bits/stdc++.h> using namespace std; int DP[10001][10001] = {0}; map<int, vector<pair<int, int>>> byWeight; int main() { int n, m; cin >> m >> n; vector <pair<int, int>> arr; for (int i = 0; i < n; i++) { int v, w, num; cin >> v >> w >> num; byWeight[w].push_back({v, num}); } int t = 1; for (auto [w, num] : byWeight) { sort(num.begin(), num.end(), greater<pair<int, int>>()); for (int j = m; j >= 0; j--) { DP[t][j] = DP[t - 1][j]; int typeAt = 0; int copies = 1; int sum = 0; int cur_used = 1; while (copies * w <= j && typeAt < int(num.size())) { sum += num[typeAt].first; DP[t][j] = max(DP[t - 1][j - copies * w] + sum, DP[t][j]); copies++; cur_used++; if (cur_used > num[typeAt].second) { cur_used = 1; typeAt++; } } } t++; } int ans = 0; for (int i = 1; i < t; i++) { for (int j = 0; j <= m; j++) { if (DP[i][j] > ans) ans = DP[i][j]; } } cout << ans; 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...