CF2140F Sum Minimisation
Description
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 Format
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 Format
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$$$.
Explanation/Hint
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.