#P10527. Minimum Final Stone Weight
Minimum Final Stone Weight
Minimum Final Stone Weight
Given an array of positive integers representing the weights of stones, each turn you can choose any two stones to smash. Let the weights be \(x\) and \(y\) with \(x \le y\). The smashing rules are as follows:
- If \(x = y\), both stones are completely destroyed.
- If \(x \neq y\), the stone with weight \(x\) is destroyed, and the stone with weight \(y\) becomes \(y - x\).
This process is repeated until there is at most one stone remaining. Output the minimum possible weight of the final stone. If no stone remains, output \(0\).
inputFormat
The first line contains an integer n
, indicating the number of stones. The second line contains n
space-separated integers, where the i-th
integer represents the weight of the i-th
stone.
outputFormat
Output a single integer representing the minimum possible final stone weight. If no stone remains, output 0
.
sample
3
7 4 2
1