Codeforces Round 1049 (Div. 2)
F
Sum Minimisation
Codeforces
2900
原题
← E2→
翻译中…

You are given an array $a$ of size $n$, and you can perform the following operation on the array $a$ any number of times (including zero):

翻译中…
  1. Select any set of $k$ distinct indices $S = \{i_1, i_2, \ldots, i_k\}$, where $1 \le i_j \le n$ for all $1\le j\le k$.
  2. Compute the sum $x = a_{i_1} + a_{i_2} + \cdots + a_{i_k}$, and set $y = x - \left\lfloor \frac{x}{k} \right\rfloor \cdot k$.
  3. Among the selected elements $a_{i_1}, \ldots, a_{i_k}$, decrease the $y$ smallest values by $1$.
  • If two elements have different values ($a_i\ne a_j$), the one with the smaller value is smaller.
    • If two elements have the same value ($a_i= a_j$), the one with the smaller index is smaller.
    • If $y=0$, no element is decreased.
翻译中…

Your task is to determine the minimum possible value of the sum of elements of the array $a$ that can be achieved after performing the operation any number of times or report that it can be decreased indefinitely.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^6$) — the length of the array $a$.

翻译中…

The second line of each testcase contains $n$ integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each testcase, print the minimum possible value of the sum of elements of the array $a$ that can be achieved after performing the above mentioned operation any number of times.

翻译中…

If the sum of elements of the array $a$ can be decreased indefinitely, print $-1$.

翻译中…

Note

翻译中…

In the first testcase, we can perform only one operation once on the whole array, which leads to the array $a = [2, 0]$, after which no operation leads to a decrease.

翻译中…

In the second testcase, no operation leads to a decrease and thus, the answer is the sum of the array elements given.

翻译中…

In the third test case, there exists a sequence of operations that will allow us to decrease the sum of elements of the array indefinitely.

翻译中…

样例

Input #1
3
2
2 1
4
3 3 3 3
8
1 2 3 4 5 6 7 8
Output #1
2
12
-1
InputCopy OutputCopy
题目信息
题号
2140F
时间限制
2 s
内存限制
256 MB
标签
数论

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