

I could not pass all the test cases and got TLE.
Can someone help with an optimal solution
How to practice such kind of questions?
EDIT
I used priority queue to solve this one. It only passed 5 of the test cases and got TLE on the rest.
Can we do better than n(logn)?