TLE in trip

Could somebody please tell me why am I getting TLE in the trip problem
Could anyone please tell me why am I getting a TLE in my code:

include stdio.h

include stdlib.h

inline int next_int() {

int n = 0;

char c = getchar();

while (!(‘0’ <= c && c <= ‘9’)) {

c = getchar(); } while (‘0’ <= c && c <= ‘9’) {

n = n * 10 + c - ‘0’;

c = getchar();


return n;

} int main() { int m,n,i;



int a=(int )malloc(n *sizeof(int));

int ways=(int )malloc(n *sizeof(int));

int ans=(int )malloc(n *sizeof(int));







i=0;//i points to the current location

int j=i+1;//j points to the location just after i

while(i<=n-1) {




else if(ans[i]+1<ans[j]) {ans[j]=ans[i]+1;







printf("%d %dn",ans[n-1]-1,ways[n-1]%1000000007);

return 0;}

you have to put your code someplace we could read it, for example to

the above code is pasted in as trip.c by JASKARAN_1.

can you give the link? I can’t find it.

If you want to test your code to find where is TLE problem, you have to generate some big input.

I tried your algorithm on ideone with 15s timelimit - and I got TLE.

Input is generated by this C++ code:

#include <cstdio>

int main() {
	printf( "100000 1000\n");
	for ( int i = 0; i < 100*1000; ++i ) {
		printf( "%d\n", i );
	return 0;

You can try it on your PC :wink:

I don’t know how you got the TLE for 15s limit.I took the output from your code and wrote it in a file and made my program read the input file just like codechef does.It takes less than a second.
Could anybody please explain me the editorial solution or help me out with my solution.I’m not able to understand how to calculate the number of ways?