Mapfre interview question

Explain how to solve this with a heap?