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 = [2, 7, 4, 1, 8, 1]
Output: 1
Explanation: 8 and 7 -> 1, pile [2, 4, 1, 1, 1]; 4 and 2 -> 2, pile [2, 1, 1, 1];
2 and 1 -> 1, pile [1, 1, 1]; 1 and 1 -> 0, pile [1]. The answer is 1.
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