-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path441-robin-hood-in-town.cpp
More file actions
51 lines (50 loc) · 1.23 KB
/
Copy path441-robin-hood-in-town.cpp
File metadata and controls
51 lines (50 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Robin Hood in Town [2014C]
* Problem: https://codeforces.com/problemset/problem/2014/C
* Verdict: ACCEPTED Solved: 2025-04-29
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 171 ms Memory: 0 KB
* Tags: binary search, greedy, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/2014/submission/317750078
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
ll s = 0;
for(auto&i:a)cin>>i,s+=i;
int mx=*max_element(a,a+n);
ll lo=0,hi=1e15,mid,ans=-1;
while(lo<=hi){
mid=(lo+hi)>>1;
int cnt = 0;
bool skip1=0;
for(int i = 0; i < n; i++){
if(!skip1&&a[i]==mx){
skip1=1;
continue;
}
if(2 * a[i] < (s + mid + n-1) / n){
cnt++;
}
}
if(cnt > (n/2)){
ans = mid;
hi = mid - 1;
}else lo = mid + 1;
}
cout << ans << '\n';
}
return 0;
}