#K85352. Smallest Remaining Stone Power
Smallest Remaining Stone Power
Smallest Remaining Stone Power
You are given a collection of stones, each with an associated power value. In each operation, you select the two stones with the largest power values, remove them from the collection, and if their difference is non-zero, insert a new stone with power equal to the absolute difference of the two. The process continues until there is at most one stone left. Your task is to compute the final stone’s power. In other words, given a list of integers (a_1, a_2, \dots, a_n), perform the following operation repeatedly:
$$\text{Let } x = \max\{a_i\} \text{ and } y = \max\{a_j \mid a_j \neq x \}\,. $$(\text{Then replace } x \text{ and } y \text{ with } |x-y|) (if (|x-y| \neq 0)).
If no stone remains, output 0.
This problem simulates a series of combine operations and asks you to compute the smallest possible power level of the remaining stone.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (n) ((1 \leq n \leq 10^5)) representing the number of stones. The second line contains (n) space-separated integers, each representing the power of a stone.
outputFormat
Output a single integer — the power of the final remaining stone. If no stone remains, output 0. The result should be written to standard output (stdout).## sample
3
4 9 7
2