Using heap to solve sorted lists