Getting TLE

link of problem–>> https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/swap-it-2/
link of my solution–>> https://www.hackerearth.com/submission/17626488/
my logic–>>We first pick the smallest element from array a1, a2, a3…(ak or an) [We consider ak when k is smaller, else n]. We place the smallest element to the a0 position after shifting all these elements by 1 position right. We subtract number of swaps (number of swaps is number of shifts minus 1) from k. If still we are left with k > 0 then we apply the same procedure from the very next starting position i.e., a2, a3,…(ak or an) and then place it to the a1 position. So we keep applying the same process until k becomes 0.

please someone help and thanks in advance.