Pergunta de entrevista da empresa Mapfre

Explain how to solve this with a heap?