Problem
You have a pile of stones with weights stones. Each turn, take the two heaviest stones, x <= y, and smash them together:
- if x == y, both are destroyed;
- otherwise the x stone is destroyed and the y stone now weighs y - x.
Keep going until at most one stone is left. Return its weight, or 0 if none remain.
Example 1
- Input
- [2, 7, 4, 1, 8, 1]
- Output
- 1
- Why
- 8,7 → 1; 4,2 → 2; 2,1 → 1; 1,1 → 0; one stone of 1 left
Example 2
- Input
- [2, 2]
- Output
- 0
Example 3
- Input
- [1]
- Output
- 1
Constraints
1 <= stones.length <= 301 <= stones[i] <= 1000