How to solve Sartaj Drinks Gotchi from Codehard?

Link to the problem- CodeChef: Practical coding for everyone