I was solving this problem. But I keep getting RTE on test2 and I dont understand why.
My idea is selecting the smallest 2*n elements by putting them into a set. Then I use two pointers on the array sorted by coordinate component in order to get the system of nested segments. No wonder what goes wrong with that.
Let's analize this testcase:
Your multiset will look like this
s = {-6, -5}
For
i = 0
andj = 2
, both weights are -5, so you are going in the first if and erasing -5 and then trying to erase it again, but there was only one -5 in the multiset.but wait.. in the multiset there must be 2 times -5. right?
You are deleting the biggest value in multiset when it size is bigger than 2 * n, so no, there is only one -5 at the end in it.