B. Game of Ball Passing(前缀和,思维)

目录

原文

思路:

代码:


原文

Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.

The game involves nn players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.

Find the minimum possible amount of balls that were involved in the game.

Input

There are several test cases in the input data. The first line contains a single integer t (1≤t≤5⋅104) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains one integer n(2≤n≤105) — the number of players.

The second line of the test case contains a sequence of integers a1,a2,…,an (0≤ai≤109), where aiai is the number of passes delivered by the i-th player.

It is guaranteed that the sum of n over all test cases doesn't exceed 105.

Output

For each test case print a single integer — the answer to the problem.

Example

input

4
4
2 3 3 2
3
1 5 2
2
0 0
4
1000000000 1000000000 1000000000 1000000000

output

1
2
0
1

Note

In the first test case, with the only ball, the game can go like this:

2→1→3→4→1→2→3→4→2→3→2.

In the second test case, there is no possible way to play the game with only one ball. One possible way to play with two balls:

2→1→2→3→2→1.

2→3→2→1

In the third example, there were no passes, so 00 balls are possible.

思路:

是能想到,老是遇到前缀和

1,开始时,我按当前数和之前的前缀和的差值算,一以内不管,但是3,1,3就不对了,而且,管不到整体.

2,求最大:影响球的差值,      求和:作为可承受的极限

3,三种情况:max=0,不用球;sum-max>=max,可承受,1个球;max-(sum-max),需要球.

代码:


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj = 1e5+100;
int a[maxj],sum[maxj];
int32_t main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int mx=0,sum=0;
    for(int i=1;i<=n;++i){
        cin>>a[i];   
        mx=max(mx,a[i]);
        sum+=a[i];
    }
    if(mx==0)cout<<0;
    else if(sum-mx>=mx)cout<<1;//能否满足最大,能其余小的打打就结束了
    else cout<<mx-(sum-mx);//满足不了,差的补上
    cout<<'\n';


    }
    return 0 ;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值