Grouping is Good (Code Overflow 1.2)

Question Link:-https://www.codechef.com/COVO2020/problems/GRIG/
I checked the other solutions i didnt got what they are doing .can some one please help the solution for it.