Problem 403736 · medium · Level 04 Non-Linear Data Structures

Orchard Harvest Rounds

heaps · greedy · max-heap · ceil division

An orchard has trees whose current fruit counts are trees. You will perform exactly k harvest rounds. In each round you pick one tree, collect all of its fruit (adding that count to your score), and the tree regrows to ceil(count / 3) fruit, so it can be picked again later.

Return the maximum score you can collect in k rounds.

Examples

Input:  trees = [7, 7, 7, 7], k = 4
Output: 28
Explanation: pick each tree once.

Input:  trees = [2, 15, 4, 4, 1], k = 3
Output: 24
Explanation: pick the 15 (regrows to 5), then that same tree again for 5 (regrows to 2),
             then one of the 4s: 15 + 5 + 4 = 24.

Constraints

  • 1 <= len(trees) <= 10**5, 1 <= trees[i] <= 10**9
  • 1 <= k <= 10**5
  • Target complexity: O(n + k log n).

Goals

  • Drive a repeated best-choice simulation with a max-heap
  • Compute ceil(x / 3) with integer arithmetic
  • Accumulate a score across k rounds
Starting Python…