arranging cupcakes

please help…!!!
i m getting time limit exceed for my code .
here it is CodeChef: Practical coding for everyone