How to solve Sartaj Drinks Gotchi from Codehard?

Link to the problem- https://www.codechef.com/COHA2019/problems/GOTCHI