Submission #715222

#TimeUsernameProblemLanguageResultExecution timeMemory
715222cfjasonKnapsack (NOI18_knapsack)C++17
100 / 100
111 ms4864 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(int argc, char *argv[]) { ll s, n; cin >> s >> n; vector<ll> dp(s + 1); vector<vector<array<ll, 2>>> itemsByWeight(s + 1); for (int i = 0; i < n; i++) { ll v, w, k; cin >> v >> w >> k; itemsByWeight[w].push_back({v, k}); } for (int i = 0; i <= s; i++) { sort(itemsByWeight[i].rbegin(), itemsByWeight[i].rend()); } for (int i = 0; i < itemsByWeight.size(); i++) { for (int j = s; j >= 0; j--) { int cpIndex = j; int earnings = 0; for (int k = 0; k < itemsByWeight[i].size(); k++) { for (int d = 0; d < itemsByWeight[i][k][1]; d++) { if (cpIndex - i < 0) break; cpIndex -= i; earnings += itemsByWeight[i][k][0]; dp[j] = max(dp[j], dp[cpIndex] + earnings); } if (cpIndex - i < 0) break; } } } cout << dp[s] << endl; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main(int, char**)':
knapsack.cpp:17:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::array<long long int, 2> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |   for (int i = 0; i < itemsByWeight.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:21:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |       for (int k = 0; k < itemsByWeight[i].size(); 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...