An operation on position i is called "balancing" iff Ai>Ai+1.
Lemma 1
If A turns into B after some operations, there must be min(B)≤⌊L(A)⌋ and max(B)≥⌈R(A)⌉.
Proof of Lemma 1
Assume that min(B)>⌊L(A)⌋. Let i be some index satisfying L(A,i)=L(A). Notice that L(B,i)≥min(B), so we have: ⌊L(B,i)⌋≥⌊min(B)⌋=min(B)>⌊L(A)⌋=⌊L(A,i)⌋
So L(B,i)>L(A,i), i.e. ∑k=1iBk>∑k=1iAk.
However, ∀j=i, operations on position j does not affect ∑k=1iBk, and operations on position i makes ∑k=1iBk smaller, so there must be ∑k=1iBk≤∑k=1iAk. Contradiction!
Similarly, we can prove that max(B)≥⌈R(A)⌉.
Lemma 2
If A turns into B after a balancing operation, there must be ⌊L(A)⌋=⌊L(B)⌋ and ⌈R(A)⌉=⌈R(B)⌉.
Proof of Lemma 2
Suppose that the balancing operation is performed on position i. Notice that ∀j=i,L(A,j)=L(B,j) and L(B,i)=L(A,i)−i1<L(A,i), so L(B)≤L(A), so ⌊L(B)⌋≤⌊L(A)⌋.
Assume ⌊L(B)⌋<⌊L(A)⌋, then ∀j=i,⌊L(B,i)⌋<⌊L(B,j)⌋. (Otherwise ∃j=i,⌊L(B)⌋=⌊L(B,j)⌋=⌊L(A,j)⌋≥⌊L(A)⌋)
If i=1, we have ⌊L(B,1)⌋<⌊L(B,2)⌋, i.e. A1−1<⌊2A1+A2⌋. Discuss the value of A2: if A2≤A1−2, then ⌊2A1+A2⌋≤A1−1, leading to contradiction; if A2=A1−1, then ⌊2A1+A2⌋=A1−1, also leading to contradiction.
If i>1, on one hand we have ⌊L(B,i)⌋<⌊L(B,i−1)⌋, so L(B,i)<L(B,i−1), after simplification we have ∑k=1i−1Ak>(i−1)(Ai−1)
On the other hand, we can calculate the difference between L(B,i) and L(B,i+1) using Ai+1≤Ai−1 and the previous inequality, after simplification we have L(B,i)−L(B,i+1)>−i+11
Decompose the difference into integral part and fractional part, we have {L(B,i)}>⌊L(B,i+1)⌋−⌊L(B,i)⌋−i+11+{L(B,i+1)}
Because ⌊L(B,i+1)⌋−⌊L(B,i)⌋≥1 and {L(B,i+1)}≥0, we have {L(B,i)}>i+1i. However, since L(B,i) can be expressed as a rational number with denominator i, so {L(B,i)}≤ii−1, leading to contradiction.
In summary, there must be ⌊L(A)⌋=⌊L(B)⌋. Similarly, we can prove that ⌈R(A)⌉=⌈R(B)⌉.
Construction and proof of the answer
For an array A of length n, perform balancing operations to it continuously until you get a monotonically non-decreasing array B. Since B is monotonically non-decreasing, there must be min(B)=B1=L(B)=⌊L(B)⌋ and max(B)=Bn=R(B)=⌈R(B)⌉. By Lemma 2 we have ⌊L(B)⌋=⌊L(A)⌋ and ⌈R(B)⌉=⌈R(A)⌉, so max(B)−min(B)=⌈R(A)⌉−⌊L(A)⌋.
According to Lemma 1, no matter what operations are performed on A, the final array C must satisfy max(C)−min(C)≥⌈R(A)⌉−⌊L(A)⌋, so ⌈R(A)⌉−⌊L(A)⌋ is the answer.