spoj gss1 tle

Hi,
I am getting tle for problem gss1.Can someone please help where I am going wrong.
My code: https://ideone.com/BN5ecX
Thanks.