I went straight to max-heap because you always want to halve the largest element first, and that part felt solid.
Use a max-heap to always apply the operation to the element that yields the largest reduction in sum. For each operation, pop the maximum, compute its halved value, add the reduction to the total sum, and push the new value back. Repeat until k operations are used or the maximum is 0.
Pro tip: Mention that the greedy choice is optimal because the reduction from halving is monotonic with the element's value, and using a heap ensures we always pick the best candidate. Also, note that if the maximum becomes 0, further operations are useless, so we can stop early.
Clarify that each operation replaces an element x with ceil(x/2), and we want to minimize the sum using at most k operations. Note that zeros never change and large k may exceed useful operations.
Use a max-heap to efficiently retrieve the largest element, as the reduction from halving is greatest for larger values. This allows O(log n) per operation.
Initialize total sum. For each operation (up to k), pop the max, compute new value = ceil(max/2), update sum by subtracting (max - new value), and push new value if >0. Stop if max is 0.
Argue that the greedy choice is optimal: at each step, halving the largest element gives the maximum possible reduction, and this local optimality leads to global optimality because reductions are independent and monotonic.
Time: O((n + k) log n) for heap operations; Space: O(n). Handle zeros (skip), k=0 (return original sum), and very large k (stop when max is 0).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.