Can you help me realize the difference between my code ( time exceed error) and an accepted solution by rekhansh99 [24043418] ( just for credits

I am fairly new to competitive coding, please keep your answers as layman as possible

Problem:

https://www.codechef.com/UAGE2019/problems/UEMALGO2

My solution:

https://www.codechef.com/viewsolution/24043314

Accepted Solution:

https://www.codechef.com/viewsolution/24043418

i guess the accepted solution has 3 loops and for my code time complexity for each case is O(n)

as calculation is done with respect to the input itself

(yes , i was unable to understand the possible solutions approach)

for my code the approach is

i make the first element as 0, and then the input comes in

like 0,1,2,1,2

now i will start from 1 to n

and a counter i will keep track of the position my user is at

so when a[1] =1 (is entered my i is still 0 )

j loop keeps on taking input

untill j == i+k ( if i+k is encountered we compare i+1 and i+k)

and which ever one is smaller i will shift to it and sum will be added to a sum variable

Can you help me clear my doubts or optimize my approach, and possibly help me why i am getting time exceeded error