alien chef: time limit exceeded problem

This is my first time here.
I solved the problem with code:Download
it worked pretty well on my system where as its showing time exceeded here.

it would be great if any one can see through my code(submission: 1314761) and point out the error if any.