Loading...
Loading...
Loading...
A crusher holds n stones. Each round it takes the two heaviest stones with weights x and y, where x ≥ y. If x equals y, both vanish. Otherwise one stone of weight x - y remains. Print the weight of the last stone, or 0 if none is left.
Example 1
Input
6 2 7 4 1 8 1
Output
1
The last stone weighs 1.
Example 2
Input
2 3 3
Output
0
Try the problem first. Open one hint at a time.
You need the largest value again and again, so use a heap.
heapq is a min-heap, so push negative weights.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor