Some more homework

plzz smone help me out to optimize my solution its giving time limit exceeded: 8.32 secs
http://www.codechef.com/viewsolution/2195392

is my algo correct ?
or am i making a serious mistake in there ?
plzz guide me frnds

p.s. a frustrated programmer