Description

You are given an array of integers stones where stones[i] is the weight of the ith stone. We smash the two heaviest stones together. Return the weight of the last remaining stone, or 0 if there are no stones left.

Examples

Input:stones = [2,7,4,1,8,1]
Output:1
Explanation:

After all smashes, one stone of weight 1 remains.

Input:stones = [3,7,2]
Output:2
Explanation:

Starting with stones [3,7,2]. Smash the two heaviest: 7 and 3. Since 7 > 3, the remaining stone has weight 7-3=4. Now stones are [2,4]. Smash 4 and 2: remaining stone is 4-2=2. One stone remains with weight 2.

Input:stones = [10,4,2,10]
Output:2
Explanation:

Smash the two heaviest stones (10 and 10): both are destroyed. Remaining: [4,2]. Smash 4 and 2: 4-2=2. One stone of weight 2 remains.

Constraints

  • 1 ≤ stones.length ≤ 30
  • 1 ≤ stones[i] ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!