Think of profit as a function of the last action. Each buy/sell pair contributes to the piecewise linear profit function. The heap tracks slope-change points. Pushing twice after a sell lets you retroactively change the decision. If a higher price appears later, the second can be popped as a "buy" price.
This is Slope Trick in disguise: you're maintaining a convex function of decisions without storing all states. The heap gives you updates.