Problem. You have stones with positive integer weights. Each turn, take the two heaviest, x ≤ y, and smash them: if x == y both are destroyed, otherwise a stone of weight y − x remains. When at most one stone is left, return its weight (or 0).
[2, 7, 4, 1, 8, 1]
8 and 7 → 1 [2, 4, 1, 1, 1]
4 and 2 → 2 [2, 1, 1, 1]
2 and 1 → 1 [1, 1, 1]
1 and 1 → 0 [1] → 1
Every round asks the same question, "what are the two largest right now?", while the collection changes. That's a priority queue, and the cleanest first use of a heap.
ML context. The mirror process, always combine the two smallest, is how Huffman coding assigns short codes to frequent symbols: the basis of compressed token streams and a classic example of greedy merging with a heap (Change B). The same "repeatedly take the best pair" loop appears in agglomerative clustering, where the two closest clusters merge.