A - Add and Swap Editorial by evima
When \(N = 2\), performing the operation twice reverts \(A_2 - A_1\) to the initial value, so there’s no point in performing the operation more than twice. Therefore, we only need to check the cases where we perform the operation zero or one time.
When \(N \geq 3\), it is always possible to make \(A\) non-decreasing regardless of the input. We will demonstrate this along with how to do it.
For each \(i = 1, \ldots, N-1\) in this order, we perform the operation \(2iM + 1\) times choosing \(i\). Performing the operation \(2iM + 1\) times with \(i\) corresponds to replacing \(A_i\) and \(A_{i+1}\) with \(A_{i+1} + (iM + 1)K\) and \(A_i + iMK\), respectively.
After this procedure, the sequence becomes:
\(A' = \left( A_2 + (M + 1)K,\, A_3 + (2M + 1)K,\, A_4 + (3M + 1)K,\, \ldots,\, A_N + ((N - 1)M + 1)K,\, A_1 + N(N - 1)MK \right)\).
By choosing \(M\) to be sufficiently large (\(\left\lceil \dfrac{A_{\text{MAX}}}{K} \right\rceil\) or greater), we can confirm through calculations that the resulting \(A\) will always be in ascending order.
The maximum number of operations occurs when \(N = 50\), \(K = 1\), and \(A_{\text{MAX}} = 50\), but even in this case, the total number of operations is \(\sum_{i = 1}^{N - 1} (2iM + 1) = 122549\), which satisfies the operation limit.
posted:
last update: