Good one. My first reaction to this problem is the same idea as this answer: https://leetcode.com/discuss/37365/accepted-c-solution-o-nlogn-using-maxheap
So basically we need a MaxHeap which supports add emove op operations. But in C++ priority_queue doesn't support removal of arbitary elements. You may think that your own heap is needed to code, but in the above link, some book-keeping work can be a workaround: we simply record which elements are removed, and we simply pop it out when necessary.
And another solution is "Divide and Conqure": https://leetcode.com/discuss/37444/my-220ms-divide-and-conquer-solution-in-python-o-nlogn