Open the dragon scroll

I have solved this question.

This is my code link:

and according to the submission details my code uses a time 0.03 (seconds I believe) and a memory of 2.2M.

Whereas, as there are sample submissions along side the problem, one of those sample submissions link is:

The time that this code uses according to codechef is 0.00 and memory is 1.6M. And I believe that this code is better than my code. But the logic and the complexity appears the same to me. Apart from the fact that I am using a function, but i don’t think that shall affect this much and I am using pointers to my functions so the memory used shall also be the same I believe.

Please reply guys. Thanks for your help in advance.

This is the problem link:

just in case if that helps.