You have an array stones of positive weights. Each turn you take the two heaviest stones, weights x and y with x = y , and smash them: if x == y both are destroyed, otherwise the stone of weight x is destroyed and the heavier one becomes weight y - x . Repeat until at most one stone remains, then return its weight — or 0 if no stone is left. Input format Input is a single array, e.g. [[2,7,4,1,8,1]] .