HackerEarth Minimum cost problem solution