i am try to solve these question since morning but i am not able to solve this then i see solution from other but i cant get it

so any one can explain me how to solve this

https://www.codechef.com/MAY19B/problems/MATCHS

can you explain approach for solving this question