Plz explain logic for this solution of RAISINS

Here’s the solution code for RAISINS https://www.codechef.com/viewsolution/29774197
Please someone explain the logic. This solution got 99 points during contest.