Submission #891411

#TimeUsernameProblemLanguageResultExecution timeMemory
891411Mustela_ErmineaKnapsack (NOI18_knapsack)C++14
100 / 100
99 ms3960 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; int S, N; ll dp[2001]; vector<pii> items [2001]; int main() { cin >> S >> N; fill(dp, dp + 2001, -1); dp[0] = 0; for (int i = 0; i < N; i++) { int v, w, k; cin >> v >> w >> k; items[w].push_back({v, k}); } for (int i = 1; i <= S; i++) { if (items[i].size() == 0) continue; sort(items[i].begin(), items[i].end()); reverse(items[i].begin(), items[i].end()); int bounds = S / i, p = 0; while (p < items[i].size() && bounds > 0) { for (int j = min(bounds, items[i][p].second); j > 0; j--) { for (int k = S; k >= i; k--) { if (dp[k - i] != -1) { dp[k] = max(dp[k], dp[k - i] + items[i][p].first); } } bounds--; } p++; } } ll res = 0; for (int i = 1; i <= S; i++) res = max(res, dp[i]); cout << res << endl; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:22:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |   while (p < items[i].size() && bounds > 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...