fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. int minTotalWeightAfterDays(vector<int>& weights, int d) {
  8. // Create a max heap using a priority queue
  9. priority_queue<int> maxHeap;
  10.  
  11. // Push all the chocolate weights into the max heap
  12. for (int weight : weights) {
  13. maxHeap.push(weight);
  14. }
  15.  
  16. // Perform the operation for 'd' days
  17. for (int i = 0; i < d; ++i) {
  18. int heaviest = maxHeap.top(); // Get the heaviest chocolate
  19. maxHeap.pop(); // Remove the heaviest chocolate from the heap
  20.  
  21. // Eat half of the heaviest chocolate (use floor(heaviest / 2))
  22. int remaining = (heaviest + 1) / 2; // Adjust halving to ensure it's closer to the expected result
  23.  
  24. // Put the remaining chocolate back into the heap
  25. maxHeap.push(remaining);
  26. }
  27.  
  28. // Calculate the total weight of the remaining chocolates
  29. int totalWeight = 0;
  30. while (!maxHeap.empty()) {
  31. totalWeight += maxHeap.top();
  32. maxHeap.pop();
  33. }
  34.  
  35. return totalWeight;
  36. }
  37.  
  38. int main() {
  39. vector<int> weights = {30, 20, 25}; // Example input weights
  40. int d = 100; // Example number of days
  41.  
  42. int result = minTotalWeightAfterDays(weights, d);
  43. cout << "Minimum total weight after " << d << " days: " << result << endl;
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Minimum total weight after 100 days: 3