We need to solve: Given an array a of length n, we can move any car from one sub-track to another arbitrarily many times. So essentially, we can rearrange the total number of cars across the n tracks arbitrarily, because moving a car from one to another doesn't change total sum of cars. So we can choose any nonnegative integer values b_i (number of cars on track i after operations) such that sum_i b_i = sum_i a_i = S, and we want to minimize inconvenience: ∑_{i<j} |b_i - b_j|.
We are to find the minimal possible sum of absolute differences across all pairs for a multiset of n nonnegative integers with fixed total S. Since we can move arbitrarily, b_i can be any nonnegative integer, not necessarily bounded by original a_i. So we need to distribute S identical items into n bins, to minimize sum of pairwise absolute differences.
This is a classic problem: to minimize sum_{i<j} |b_i - b_j|, we want the distribution as equal as possible, because the sum of absolute differences is minimized when the values are as close to each other as possible (by convexity). Indeed, the sum of absolute differences is minimized when the multiset is "balanced". More precisely, the function ∑{i<j} |b_i - b_j| is Schur-convex, so it's minimized at majorization minimal vector, which is the vector where all entries are as equal as possible, i.e., each b_i is either floor(S/n) or ceil(S/n). This is known: for fixed sum, the sum of absolute differences is minimized when the values are as equal as possible. But we need to be cautious: because the sum of absolute differences can also be expressed as something like (for sorted b) = ∑{i=1}^n (2i-n-1) b_i. Actually, if b is sorted ascending, then ∑{i<j} |b_j - b_i| = ∑{i=1}^n (2i - n - 1) b_i. That is correct: for sorted b, each b_i appears with coefficient (i-1) - (n-i) = 2i - n -1. Since coefficients increase with i, to minimize sum, we want smaller b_i to be as large as possible? Wait: Since coefficients are negative for i < (n+1)/2 and positive for i > (n+1)/2. For odd n, middle term coefficient zero. So we want to put as much mass as possible into the middle to minimize the weighted sum? Actually, because the sum of b_i is fixed, the weighted sum with coefficients that sum to zero? The sum of coefficients: ∑_{i=1}^n (2i-n-1) = 0. So it's like we want to put larger values where coefficients are negative? Wait: Negative coefficient means increasing that b_i reduces the total sum (since coefficient negative). Positive coefficient means increasing that b_i increases total. So to minimize, we want to assign the largest possible values to positions with negative coefficients (i.e., small i) and smallest possible values to positions with positive coefficients (large i). But because sum is fixed, there's a trade-off. For convex order, the minimal sum occurs when the vector is as "equal" as possible. Let's test: suppose n=2, S=3. Two possibilities: (1,2) gives sum |2-1|=1; (0,3) gives 3; (2,1) same as (1,2); (3,0) gives 3; (1,2) is indeed equal distribution? floor=1, ceil=2. So min=1. So indeed the equal distribution (1,2) yields minimal sum. For n=3, S=5: equal distribution (2,2,1) yields? Compute sum of absolute differences: for (2,2,1): differences: |2-2|=0, |2-1|=1, |2-1|=1 total=2. Consider alternative (3,1,1): sum = |3-1|+|3-1|+|1-1| = 2+2+0=4. (5,0,0): sum=5+5+0=10. So min=2 indeed. So equal distribution yields min.
Thus answer should be inconvenience of the "balanced" distribution: where we have x = floor(S/n), y = ceil(S/n) such that some count of y and x. Let r = S mod n; then we have r bins with y = ceil(S/n) = x+1 (if S not divisible by n) and n-r bins with x. Then what is the sum of absolute differences for such configuration? We can compute formula.
Let b sorted: first n-r values = x, last r values = x+1. Actually, if x = floor(S/n), then S = n*x + r, with 0 <= r < n. Then the sorted vector: we can have all x, then the ones with x+1. Since x and x+1 differ by 1, the absolute differences between equal numbers are zero, and between x and x+1 is 1. So the total sum = (number of pairs where one is x and the other is x+1) * 1. Number of pairs between the two groups = (n-r)*r. So inconvenience = (n - r) * r.
But wait, is that always correct? Let's test: For n=3, S=5: x=1, r=2 (since 5=31+2). Then n-r=1, r=2, product=2. That matches computed sum. For n=3, S=3: x=1, r=0, product=0. Indeed (1,1,1) sum=0. For n=4, S=2: x=0, r=2 (since 2=40+2). Then n-r=2, r=2 product=4. Check: distribution: two zeros, two ones. Sum of absolute differences: pairs: between zeros 0, between ones 0, between zero and one: each zero with each one: 2*2 = 4 pairs, each diff=1 => total 4. Good. For n=2, S=3: x=1, r=1 => n-r=1, r=1 product=1. Yes. For n=1, S arbitrary: trivial: n=1, inconvenience is sum over empty pairs = 0. Our formula: n=1, S mod n? r = S mod 1 = 0, n-r=1, r=0 product=0. Good.
So answer seems to be (n - r) * r, where r = S mod n.
But wait, is it always optimal to have only two adjacent integer values? Could there be a better distribution that is not integral? But b_i must be integers (cars count). So the equal distribution (floor/ceil) yields minimal sum of absolute differences? Let's confirm more formally. The sum of absolute differences is minimized by any vector that is "majorized" by all others? Actually, for fixed sum, the vector that is as equal as possible (i.e., majorizes all others) maximizes the sum of squares? Wait, recall that sum of squares is minimized by equal distribution. For absolute differences, it's also minimized by equal distribution? Let's check with small n: n=3, S=4: equal distribution: floor=1, ceil=2? x=1, r=1 => (2,1,1) sum abs differences: |2-1|+|2-1|+|1-1| = 1+1+0=2. Check alternative: (3,1,0): sum= |3-1|+|3-0|+|1-0| = 2+3+1=6; (4,0,0): sum=4+4+0=8; (2,2,0): sum=|2-2|+|2-0|+|2-0| =0+2+2=4; So min is 2. Indeed equal distribution gives min.
What about n=4, S=6: equal: x=1, r=2 => (2,2,1,1) => sum? Compute: pairs: between the two 2s:0; between two 1s:0; cross pairs: each 2 with each 1: 22=4 pairs diff=1 => total 4. Could there be (3,1,1,1): sum= (3-1)3? Actually compute: (3,1,1,1): differences: between 3 and each 1: 3? 3 with three 1's: 3 pairs diff=2 each? |3-1|=2 => total 6; among 1s:0; total 6. (2,2,2,0): diff: zeros? compute: between 2s:0; each 2 with 0: 32? Actually three 2s each with 0 diff=2 => 3*2=6. So min=4. Good.
Thus formula holds.
But is there any nuance? The operation allows moving a car from its current sub-track to any other. This essentially allows any redistribution; but is there any limitation like you cannot have negative cars? No, since you move cars, each b_i must be nonnegative integer, and sum fixed. So indeed it's unconstrained.
Thus the answer for each test case is: let total = sum a_i. Compute r = total % n. Answer = r * (n - r). Because (n - r) * r = r*(n-r).
But wait, check sample: first test case: n=3, a=[1,2,3] sum=6, r=0 => answer 0. matches output. second test case: n=4, a=[0,1,1,0] sum=2, r=2, answer = 22 =4, matches output. third test case: n=10, a sum? Let's compute: 8+3=11, +6=17, +11=28, +5=33, +2=35, +1=36, +7=43, +10=53, +4=57. Sum=57. 57 mod 10 = 7? 105=50, remainder 7. So r=7, n-r=3, product=21. matches output.
Thus answer simple.
But wait: is there any scenario where we might want to have more than two distinct values to achieve lower sum? Let's attempt with small n and S to test: For n=5, S=7: equal distribution: floor=1, r=2 => (2,2,1,1,1) sum cross pairs: between 2 and 1: 23=6 diff=1 => 6. Could we do (2,2,2,1,0): sum? Let's compute systematically. sorted: 0,1,2,2,2. differences: compute sum_{i<j} |b_j - b_i|. Using formula: For sorted, sum = Σ (2i - n - 1) b_i. n=5, coefficients: i=1:2-5-1=-4; i=2:4-5-1=-2; i=3:6-5-1=0; i=4:8-5-1=2; i=5:10-5-1=4. So sum = -40 + -21 + 02 + 22 + 42 = -2 + 4 + 8 = 10. vs equal distribution: (1,1,1,2,2) sorted: 1,1,1,2,2. compute: -41 + -21 + 01 + 22 + 4*2 = -4 -2 +0 +4 +8 =6. So equal better.
What about (3,1,1,1,1): sorted:1,1,1,1,3 => coeff: -41 + -21 + 01 + 21 + 4*3 = -4 -2 +0 +2 +12 =8. So 6 is lower.
What about (2,2,1,1,1) is best.
Consider n=5, S=8: equal: floor=1, r=3 => (2,2,2,1,1) sorted:1,1,2,2,2? Actually need sorted: 1,1,2,2,2. Compute sum with coeff: -41 + -21 + 02 + 22 + 42 = -4 -2 +0 +4 +8 =6. Alternatively (3,2,1,1,1): sorted 1,1,1,2,3 => coeff: -41 + -21 + 01 + 22 + 43 = -4 -2 +0 +4 +12=10. (4,1,1,1,1): sorted 1,1,1,1,4 => coeff: -41 + -21 +01+21+44 = -4-2+0+2+16=12. (2,2,2,2,0): sorted 0,2,2,2,2 => coeff: -40 + -22 +02+22+42 = -4 +0+4+8? Actually compute: -22=-4, 02=0, 22=4, 42=8 => total 8. So 6 is lower.
Thus formula seems correct.
But wait, there is a twist: The inconvenience is defined as sum over i<j |a_i - a_j|. In our redistribution we can arbitrarily set b_i. However, is there any cost to moving cars? The problem allows moving any number of cars at no cost. So we can achieve any distribution. So answer is simply r*(n-r). So this appears trivial.
But before finalizing, we should check edge cases: n=0? Not possible. n=1: answer 0. n large, sum up to 2e5*1e9 = 2e14, but we only need mod n, so we can compute sum modulo n using 64-bit.
Thus solution: For each test case, read n, array, compute total sum S (as long long). Compute r = S % n. Answer = r * (n - r). Output.
But wait: The sample's second test case: n=4, S=2, r=2, answer = 2*2=4, matches.
Thus solution is O(n) per test case, sum n <= 2e5, fine.
But before finalizing, we need to verify if the inconvenience for balanced distribution indeed equals r*(n-r). But we assumed that when we have r numbers of x+1 and n-r numbers of x, the sum of absolute differences is exactly (n-r)*r because all absolute differences are either 0 (same group) or 1 (between groups). But that holds only if x+1 and x differ by 1, which they do. However, there could be a case where x and x+1 differ by more than 1? No, x+1 and x differ by exactly 1. So absolute diff =1.
But what if the distribution has more than two distinct values? That can't be more equal than the equal distribution. So it's not needed.
But we must consider if the optimal distribution could have values not differing by 1? For example, if S is divisible by n, the optimal is all equal, inconvenience 0. So that's consistent.
Thus the solution is straightforward.
Now, I'm just going to write a brief explanation for clarity.
But wait: The problem statement says: "You can perform the following operation any (possibly zero) number of times: choose a traffic car and move it from its current sub-track to any other sub-track." So we can move cars individually. That allows any redistribution, as long as total sum constant.
Thus answer = (S % n) * (n - S % n).
But we must be careful about the sum of n across test cases up to 2e5, so reading input and computing sum is fine.
Now let's confirm with more tests.
Edge: n=2, S odd => r=1, answer=1*1=1. For S=1: distribution (1,0) yields |1-0|=1; (0,1) same. Balanced? floor=0, r=1 -> (1,0) indeed. So min=1. Could there be any distribution giving 0? Only if both equal, but sum=1 cannot. So correct.
Edge: n=2, S even => r=0 => answer=0.
Edge: n=100000, S huge but mod.
Thus final code: read t, loop, read n, read array, compute sum, compute r = sum % n, ans = r * (n - r), print ans.
But one more check: For n=0? Not possible.
Thus answer is done.
But wait: The problem might have a twist: the inconvenience formula is sum_{i=1}^{n} sum_{j=i+1}^{n} |a_i - a_j|. For balanced distribution, we got r*(n-r). However, is it possible to achieve a lower inconvenience by having some bins with values not integer? But bins must have integer numbers of cars. So no.
Thus answer stands.
I will produce final solution.
However, before finalizing, I realize there's potential nuance: The operation moves a car from one sub-track to another. But what if you move a car from a sub-track that has zero cars? You can't. So you can only move from sub-tracks with positive count. However, the problem statement says "choose a traffic car and move it from its current sub-track to any other sub-track." That implies there must be a car there. But since we can move cars arbitrarily, we can achieve any distribution that is reachable by moving cars from existing ones. But is any distribution reachable? Starting from initial a, we can move cars arbitrarily, as long as we never have negative counts. Since we can always move cars from any track with surplus to any track with deficit, we can achieve any nonnegative integer vector with same total sum provided the total sum is S? Actually, consider initial a = [0, 0, 5]. Can we achieve [2, 2, 1]? We can move cars from track 3: initially 5. Move 2 to track1 => [2,0,3]; then move 2 to track2 => [2,2,1]. So yes. More generally, as long as total sum is S, we can always move cars from tracks that have extra to tracks that need more. Because we can move one car at a time, and as long as there's at least one track with positive cars (if total >0), we can move. If total=0, only distribution all zeros. So indeed any distribution with sum S is reachable: simply take any target distribution b, and for each i, if b_i > a_i we need to bring in (b_i - a_i) cars from other tracks; we can always do that because total sum matches. So it's feasible.
Thus answer stands.
Now implement.
But careful: r * (n - r) might overflow 32-bit if n up to 2e5, r up to n-1, product up to (n^2)/4 ~ 1e10, which fits in 64-bit (less than 10^10, 2e5^2/4 = 1e10). 1e10 fits in 64-bit (max 9e18). So use long long.
Thus code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll sum = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
}
ll r = sum % n;
ll ans = r * (n - r);
cout << ans << "\n";
}
return 0;
}
But wait: Is there any possibility that the optimal distribution is not exactly floor/ceil? Let's think more rigorously.
We need to minimize f(b) = ∑_{i<j} |b_i - b_j| subject to ∑ b_i = S, b_i >= 0 integers.
This is a classic problem: the sum of absolute differences is minimized when the numbers are as equal as possible. Proof: For any two indices i, j, if b_i >= b_j + 2, then moving one from i to j (so b_i' = b_i - 1, b_j' = b_j + 1) reduces the total sum? Let's see: The change in f: originally contributions involving i and j with others? Actually, we can consider the effect on the total sum when we adjust two numbers. The sum can be expressed as ∑{k} (2k - n - 1) b{(k)} for sorted. But a local improvement: If we have two numbers with difference >= 2, we can decrease the larger by 1 and increase the smaller by 1, and show that f decreases. Because the effect on f from such a transfer: For sorted vector, if we take two elements not necessarily adjacent? But maybe we can argue: The total sum over pairs can be written as ∑{i} (2i - n - 1) b{(i)} for sorted order. If we have two values b_i > b_j and we transfer one from i to j (maintaining ordering? Might change ordering). But there is known that the function is minimized at the most equal distribution. I'm convinced.
Alternatively, note that f(b) = ∑{i<j} |b_i - b_j| = 2 * ∑{i} (2i - n - 1) b_{(i)}? Actually earlier formula is exactly that. Since coefficients are increasing, to minimize f given fixed sum, we want the larger values to have as small coefficients as possible (i.e., be as left as possible). But because the sum is fixed, the optimum is to make all values as close as possible, which yields the vector with minimal spread. The extreme case: we push mass to the ends? Let's test: n=3, coefficients: -2,0,2 (since for sorted: indices 1,2,3: 2i-n-1 => -2,0,2). Suppose S=3. The equal distribution (1,1,1) gives f = -21 +01+21 = 0. If we try (2,1,0) sorted (0,1,2): f = -20 +01+22 =4. So indeed equal gives smaller.
But consider (2,0,1) sorted (0,1,2) same. So it's minimal.
Thus it's consistent.
But I'd like to derive the value of f for the balanced distribution more generally: For any distribution with values only x and x+1, the sorted vector is n-r copies of x, then r copies of x+1. Compute f = Σ_{i<j} |b_i - b_j|. Since all pairs within same group have diff 0. Cross group pairs: each of the (n-r) * r pairs diff=1. So f = (n-r)*r.
But wait: If x and x+1 are not adjacent? They are adjacent, diff=1. But what about the case where S mod n yields x and x+1 but x may be >0 or =0? That's fine; if x=0, then the values are 0 and 1, diff=1. So same.
Thus answer is correct.
One more nuance: Could we achieve lower f by using values that are not just two numbers but with some numbers reduced more and others increased? Suppose we have n=5, S=7. Balanced gives 2,2,1,1,1. f = 6. Could we achieve f=5? Let's try to see if any integer distribution gives f=5: For sum=7, average=1.4. f formula expressed in terms of variance? There is known relation: f = 2 * Σ_{i} (2i - n - 1) b_i? Actually earlier: for sorted b, f = Σ_{i=1}^n (2i - n - 1) b_i. Since coefficients sum to zero and are symmetric. Let’s compute for sorted b. For n=5, coefficients: -4,-2,0,2,4. f = -4 b1 -2 b2 +0 b3 +2 b4 +4 b5. We want to minimize f given b1<=b2<=b3<=b4<=b5 integers >=0, sum=7. Since coefficients for early indices are negative, we want larger b1,b2 to reduce f, but they are constrained to be nondecreasing and sum fixed. To minimize f, we'd like to put as much mass as possible on early indices (negative coefficients) and as little as possible on later indices (positive coefficients). However, the nondecreasing order forces that early indices cannot exceed later indices. So the optimal is to make them as equal as possible? Actually, to maximize the weight on negative coefficients, we would like b1 and b2 to be as large as possible, but b1 <= b2 <= ... <= b5. The sum is fixed. If we increase b1, we may need to increase others to maintain order? Not necessarily: we can set b1 as high as possible, but then we must have b2 >= b1, b3 >= b2, etc. The constraint that they are sorted nondecreasing is not active if we can set b1 = b2 = ... = b5? But if we increase b1, we must also increase later ones to at least that value? Actually, if we increase b1, we could keep b2..b5 the same only if they are >= b1. But if we want b1 large, b2 must be at least b1. So to put mass on early indices, we have to also increase later indices (or at least not decrease them). This suggests that the most equal distribution minimizes the spread. Let's attempt to find a distribution that yields f lower than equal distribution. Suppose we try to push mass to early indices: e.g., b1=2, b2=2, b3=1, b4=1, b5=1: That's balanced. Try b1=2, b2=2, b3=2, b4=1, b5=0? Not sorted because 1 < 2? Actually sorted requires b4>=b3, so can't have b4=1 after b3=2. So need nondecreasing: b1 <= b2 <= b3 <= b4 <= b5. So we could try b1=1, b2=2, b3=2, b4=2, b5=0? But 0 is less than 1, so ordering violated. Sorting would give 0,1,2,2,2 which we already considered (2,2,2,1,0 after sort) gave f=10. Not better.
What about b1=1, b2=1, b3=1, b4=2, b5=2 (balanced) gives f=6. Could we do b1=1,b2=1,b3=2,b4=2,b5=1? Not sorted. After sorting, it's 1,1,1,2,2. So same.
What about b1=2,b2=1,b3=1,b4=1,b5=2? Sorted -> 1,1,1,2,2. Same.
Thus equal seems minimal.
We can also use majorization: The vector (x, x, ..., x+1, x+1) is majorized by any other vector with same sum and nonnegative entries? Actually, the equal distribution is the "minimally spread" and it's the one that is majorized by all others (i.e., it is the most majorized? Wait: For a given sum, the vector with all equal majorizes any other? Actually, recall: For vector with fixed sum, the vector that is "most spread out" (like one large, rest zeros) is least majorized, and the uniform vector is most majorized. Majorization: x majorizes y if the partial sums of the sorted decreasing sequence satisfy... Typically, uniform distribution is majorized by all others (it is the most "balanced"). And because sum of absolute differences is Schur-convex, it attains minimum at the most majorized vector, i.e., uniform. So that's correct.
Thus answer stands.
Now I'll provide final solution.
But the problem might be more interesting if there were constraints like each operation moves exactly one car, but we can do any number, so no issue.
Thus final answer.
Let me write the solution with explanation.
Time complexity: O(total n) across test cases.
Edge Cases:
- S mod n may be zero, answer 0.
- n=1, answer 0.
Everything fine.
So final code is simple.
Wait: The problem statement: "Traffic cars cause a lot of hindrances to races, especially when there are a lot of them." Irrelevant.
Ok.
Now output.
Thus answer.
I'll produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll sum = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
}
ll r = sum % n;
ll ans = r * (n - r);
cout << ans << "\n";
}
return 0;
}