knightnemo commited on
Commit
4a6fb7f
·
verified ·
1 Parent(s): 7bd010c

Upload code_segments/segment_283.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. code_segments/segment_283.txt +33 -0
code_segments/segment_283.txt ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In Sherwood, we judge a man not by his wealth, but by his merit.
2
+
3
+ Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood!
4
+
5
+ There are $n$ people living in the town. Just now, the wealth of the $i$-th person was $a_i$ gold. But guess what? The richest person has found an extra pot of gold!
6
+
7
+ More formally, find an $a_j=max(a_1, a_2, \dots, a_n)$, change $a_j$ to $a_j+x$, where $x$ is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.
8
+
9
+ A person is unhappy if their wealth is strictly less than half of the average wealth$^{\text{∗}}$.
10
+
11
+ If strictly more than half of the total population $n$ are unhappy, Robin Hood will appear by popular demand.
12
+
13
+ Determine the minimum value of $x$ for Robin Hood to appear, or output $-1$ if it is impossible.
14
+
15
+ $^{\text{∗}}$The average wealth is defined as the total wealth divided by the total population $n$, that is, $\frac{\sum a_i}{n}$, the result is a real number.
16
+
17
+ The first line of input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
18
+
19
+ The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot10^5$) — the total population.
20
+
21
+ The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$) — the wealth of each person.
22
+
23
+ It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
24
+
25
+ For each test case, output one integer — the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output $-1$ instead.
26
+
27
+ In the first test case, it is impossible for a single person to be unhappy.
28
+
29
+ In the second test case, there is always $1$ happy person (the richest).
30
+
31
+ In the third test case, no additional gold are required, so the answer is $0$.
32
+
33
+ In the fourth test case, after adding $15$ gold, the average wealth becomes $\frac{25}{4}$, and half of this average is $\frac{25}{8}$, result