Plz explain logic for this solution of RAISINS

Here’s the solution code for RAISINS CodeChef: Practical coding for everyone
Please someone explain the logic. This solution got 99 points during contest.