Submission #1319433

#TimeUsernameProblemLanguageResultExecution timeMemory
1319433f0rswornKnapsack (NOI18_knapsack)C++20
100 / 100
68 ms33144 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(a) a.begin(), a.end() ll dp[2001][2001]; const ll N = 0xc0c0c0c0c0c0c0c0; int main() { int S, n; scanf("%d%d", &S, &n); memset(dp, 0xc0, sizeof(dp)); map<int, vector<pair<int, int>>> by_weight; for (int i = 0; i < n; ++i) { int v, w, k; scanf("%d%d%d", &v, &w, &k); if (w <= S && k > 0) { by_weight[w].push_back({v, k}); } } dp[0][0] = 0; int at = 1; for (auto &[w, items] : by_weight) { sort(all(items), greater<pair<int, int>>()); for (int i = 0; i <= S; ++i) { dp[at][i] = dp[at - 1][i]; int copies = 0; int type_at = 0; int curr_used = 0; ll profit = 0; while ((copies + 1) * w <= i && type_at < items.size()) { copies++; profit += items[type_at].first; if (dp[at - 1][i - copies * w] != N) { dp[at][i] = max(dp[at][i], dp[at - 1][i - copies * w] + profit); } curr_used++; if (curr_used == items[type_at].second) { curr_used = 0; type_at++; } } } at++; } ll ans = N; for (int i = 0; i <= S; ++i) ans = max(ans, dp[by_weight.size()][i]); printf("%lld\n", ans); return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:10:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |   scanf("%d%d", &S, &n);
      |   ~~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     scanf("%d%d%d", &v, &w, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...