Submission #709323

#TimeUsernameProblemLanguageResultExecution timeMemory
709323nima_aryanKnapsack (NOI18_knapsack)C++17
100 / 100
108 ms35320 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   int limit, type_num;
   cin >> limit >> type_num;
   map<int, vector<pair<int, int>>> by_weight;
   for (int i = 1; i <= type_num; ++i) {
      int value, weight, amount;
      cin >> value >> weight >> amount;
      by_weight[weight].emplace_back(value, amount);
   }
   const i64 inf = 1e18;
   vector best(by_weight.size() + 1, vector<i64>(limit + 1, -inf));
   best[0][0] = 0;
   int at = 1;
   for (auto &[weight, items] : by_weight) {
      sort(items.begin(), items.end(), greater<pair<int, int>>());
      for (int sum = 0; sum <= limit; ++sum) {
         best[at][sum] = best[at - 1][sum];
         int copies = 0, type_at = 0, curr_used = 0;
         i64 profit = 0;
         while ((copies + 1) * weight <= sum && type_at < (int) items.size()) {
            copies += 1;
            profit += items[type_at].first;
            best[at][sum] = max(best[at][sum], best[at - 1][sum - copies * weight] + profit);
            curr_used += 1;
            if (curr_used == items[type_at].second) {
               curr_used = 0;
               type_at += 1;
            }
         }
      }
      at += 1;
   }
   cout << *max_element(best.back().begin(), best.back().end()) << '\n';
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */
#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...