Wrong answer for Make Him Happy problem.

Can any one please explain why this solution gives wrong answer?

http://www.codechef.com/viewsolution/3957105

Take look at this solution. It has been well explained by @bugkiller and well commented. Hope this will help to find out the mistake :slight_smile: