Smallest stone after smashing

Write codeMedium25 min

Your task

You have a collection of stones with positive integer weights. In each turn you smash any two stones x and y (x <= y): they both vanish and a new stone of weight y-x appears (if y-x is 0 nothing remains). After repeatedly smashing, at most one stone is left. Return the smallest possible weight of that last stone (0 if none remains). There are at most 30 stones, each of weight at most 100.

Examples

last_stone_weight_ii(stones: list[int]) -> int
Example 1
Inputlast_stone_weight_ii([2, 7, 4, 1, 8, 1])
Output1