#K55507. Last Fighter Strength
Last Fighter Strength
Last Fighter Strength
Given a list of fighters with their respective strengths, your task is to simulate the following process:
Repeatedly, select the two fighters with the highest strengths. Let these two fighters have strengths \(a\) and \(b\) (with \(a \ge b\)). If \(a \neq b\), then insert a new fighter with strength \(|a-b|\). Continue this process until at most one fighter remains.
If no fighter remains, output 0; otherwise, output the strength of the last remaining fighter.
This process can be mathematically modeled as:
\[ \begin{cases} a, b = \text{two largest elements of } S,\\ \text{if } a \neq b, \text{ then } S \gets S \setminus \{a, b\} \cup \{|a-b|\},\\ \text{repeat until } |S| \le 1. \end{cases} \]
Please note that the input starts with the number of fighters followed by their strengths.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains an integer n (n \(\ge\) 0) representing the number of fighters.
- If n > 0, the second line contains n space-separated integers representing the strength of each fighter.
outputFormat
Output a single integer to standard output (stdout): the strength of the last remaining fighter after applying the process. If no fighter remains, output 0.
## sample6
2 7 4 1 8 1
1