Deputy Chef code review

I have just started with code chef and got stuck on this problem

My solution code is:

It works fine on inputs given in the question but when submitted shows wrong answer.
It would be great help if someone told me what’s wrong in my code.