Ctrl K
Problem

Boulder Crusher

Easy · Boulder Crusher

A crusher at a building site has a hopper full of boulders; the `i`-th one weighs `weights[i]`. On each cycle the crusher picks the two **heaviest** boulders and smashes them together. Say their weights are `a ≤ b`: - if `a == b`, both boulders turn to dust; - otherwise the lighter one turns to dust and the heavier one now weighs `b - a`. Cycles repeat while more than one boulder is left in the hopper. Return the weight of the remaining boulder, or `0` if none is left.…

Solve the problem →