Timelimit error-sortgame

i have tried the problem the ques and code is here:https://ide.codingblocks.com/s/52757.
one of the test cases has been passed, but other are showing time limit error.
please allow me know where i am wrong or what changes are need to be made

Look at constraints mentioned in question.
1 <= N <= 10^5

The asymptotic time complexity of your code is O(n^2) due to sortq.
Thus it gives TLE. Try to think of more optimized solution.

Go through the videos of Time and space complexity to get its clear idea.