A quarry has a pile of boulders with integer weights stones. Each round a machine picks the two heaviest boulders, of weights x <= y, and smashes them together:
- if
x == yboth boulders are destroyed; - otherwise the lighter one is destroyed and the heavier one becomes a boulder of weight
y - x.
Rounds continue until at most one boulder is left. Return the weight of that last boulder, or 0 if none remain.
Examples
Input: stones = [9, 4, 13, 6, 6, 2]
Output: 2
Explanation: 13 and 9 -> 4, pile [4, 6, 6, 2, 4]; 6 and 6 are both destroyed, pile [4, 2, 4];
4 and 4 are both destroyed, pile [2]. The answer is 2.
Input: stones = [2, 2]
Output: 0
Constraints
1 <= len(stones) <= 10**5,1 <= stones[i] <= 10**6- Target complexity: O(n log n).
Goals
- Simulate a max-heap with negated values in heapq
- Repeatedly extract the two largest items and push a combined result
- Terminate correctly when zero or one item remains