Submission #680434

#TimeUsernameProblemLanguageResultExecution timeMemory
680434sz1218Knapsack (NOI18_knapsack)C++14
100 / 100
159 ms35180 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef vector<int> vi; const int NN = 1e6+3; int n, S; ll dp[2003][2003]; int main() { //setIO(""); cin >> S >> n; int V, W, K; map<int, vector<pi>> by_weight; for(int j=0;j<n;j++){ cin >> V >> W >> K; by_weight[W].push_back({V, K}); } int a = 1; for(auto&[w, item] : by_weight){ sort(item.rbegin(), item.rend()); for(int i=0;i<=S;i++){ dp[a][i] = dp[a-1][i]; int ta = 0; int copies = 0; int cur_used = 0; ll profit = 0; while((copies+1) * w <= i && ta < item.size()){ copies++; profit += item[ta].first; dp[a][i] = max(dp[a][i], dp[a-1][i-copies*w] + profit); cur_used++; if(cur_used == item[ta].second){ cur_used = 0; ta++; } } } a++; } ll ans = 0; for(int i=1;i<=S;i++){ ans = max(ans, dp[a-1][i]); } cout << ans << endl; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:22:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   22 |     for(auto&[w, item] : by_weight){
      |              ^
knapsack.cpp:30:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |             while((copies+1) * w <= i && ta < item.size()){
      |                                          ~~~^~~~~~~~~~~~~
#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...