Codeforces Round 973 (Div. 2)
D
Minimize the Difference
Codeforces
1900
原题
← CE →
翻译中…

Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem.

翻译中…

You are given an array $a_1, a_2, \ldots, a_n$ of length $n$. We can perform any number (possibly, zero) of operations on the array.

翻译中…

In one operation, we choose a position $i$ ($1 \leq i \leq n - 1$) and perform the following action:

翻译中…
  • $a_i := a_i - 1$, and $a_{i+1} := a_{i+1} + 1$.
翻译中…

Find the minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^{12}$).

翻译中…

The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer: the minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.

翻译中…

Note

翻译中…

In the third testcase, you can perform the operation twice with $i = 1$.

翻译中…

After that, the array is $a = [2, 3, 2, 3]$, and $\max(2, 3, 2, 3) - \min(2, 3, 2, 3) = 3 - 2 = 1$.

翻译中…

样例

Input #1
5
1
1
3
1 2 3
4
4 1 2 3
4
4 2 3 1
5
5 14 4 10 2
Output #1
0
2
1
1
3
InputCopy OutputCopy
题目信息
题号
2013D
时间限制
2 s
内存限制
256 MB
标签
二分
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。