arranging cupcakes

please help…!!!
i m getting time limit exceed for my code .
here it is http://www.codechef.com/viewsolution/4429226