Dynamic Programming21 sections · 916 units
Open in Course

Sonya - Heap Operations

Double-push trick

Process elements left-to-right. For each bib_i, push it to the heap. If bi<heap.top()b_i < \text{heap.top()}, the array isn't non-decreasing yet.

Pop the largest value, pay the cost to reduce it to bib_i, then push bib_i again. Why twice? The first push represents bib_i itself. The second accounts for the slope change from merging the popped value. Each element is pushed once initially. Extra pushes balance slope changes. Total operations: O(nlogn)O(n \log n).