Hackerrank-Sherlock and Cost

How to solve the question https://www.hackerrank.com/challenges/sherlock-and-cost on hackerrank?

//