题目链接:B. Game of Ball Passing
题解:先找出数组中最大数,通过用剩下数的和进行比较,设剩余和为x,那么他能匹配x+1
1.sum=0,需要0个球
2.sum+1>=mx,需要1个球
3.其他情况,需要a[n-1]-sum个球
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
long long a[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long mx=-1;
long long sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
mx=max(mx,a[i]);
}
sort(a,a+n);
sum-=a[n-1];
if(a[n-1]==0)
{
cout<<0<<endl;
continue;
}
if(sum+1>=a[n-1])
cout<<1<<endl;
else
cout<<a[n-1]-sum<<endl;
}
}