Hackerrank-Sherlock and Cost

How to solve the question Sherlock and Cost | HackerRank on hackerrank?