First when I submitted a code I got wrong answer, but when I changed slightly and resubmitted then I got TLE .I am sure that this code can not go to infinite loop after changing. So what might be the reasons for this?
There are several test cases. If for any test case your answer is wrong, you’ll get wrong answer.
If your program ran correctly for that test case, next test case is checked. Your program might have failed to get an answer to next test case in time thats why TLE.
Well , my guess is first your code was giving wrong output. Then you corrected it , and it became right but your method took more time then required time limit specified in problem. Hence TLE. This has happened to me many times.Code chef shows wrong answer as soon as it encounters first wrong answer.
My advice is to change the algorithm so that code runs faster.