The answer is .
Let be the minimum just before an operation, and let be the removed value. Every surviving value falls into one of two cases:
Thus the minimum increases by at least one per operation. There are exactly operations, giving a lower bound of the initial minimum plus .
To attain the bound, always remove a current maximum. Every remaining value is at most the removed value, so every one increases by one. A minimum occurrence remains: if removing a maximum also removes a minimum, all current values are equal and another occurrence remains. Consequently, the minimum increases by exactly one per operation and the bound is attained.
For , no operation is performed and the same formula applies. The intended solution for the single subtask stores only the minimum while reading the input. Time is and auxiliary space is .
The naive solution repeatedly finds and removes a maximum and updates every remaining value explicitly. It is correct but takes time and space, so it times out on maximum-size inputs.
Each integer increases at most times. The answer and all simulation values are at most . All arithmetic fits signed 64-bit integers.
Solution written by GPT6