arranging cupcakes

exceeded
limit
time

#1

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