make heap algorithm incorporated to match costs