#K92097. Last Stone Weight
Last Stone Weight
Last Stone Weight
You are given a collection of stones, each with a positive integer weight. The task is to simulate a process in which you repeatedly choose the two heaviest stones and smash them together. Suppose the heaviest stones have weights a and b with a ≥ b. If a = b, both stones are completely destroyed. If a ≠ b, the stone of weight a is reduced to weight a - b (i.e., the difference) and the stone of weight b is destroyed. This process is repeated until there is at most one stone left. If there is a stone remaining, output its weight; otherwise, output 0.
In mathematical form, if you have two stones with weights \(a\) and \(b\) where \(a \ge b\), then after smashing, the resulting stone (if any) will have weight \(a - b\). Continue this process until no more than one stone remains.
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer \(n\) (\(0 \le n \le 10^5\)), representing the number of stones. If \(n > 0\), the second line contains \(n\) space-separated positive integers representing the weights of the stones. If \(n = 0\), the second line will be absent.
outputFormat
Output a single integer to standard output (stdout) representing the weight of the last remaining stone after performing the described smashing process. If no stones remain, output 0.
## sample4
2 7 4 1
0