这是一道非常经典的贪心题目,遇到过很多次。题解在上面的 tutorial 链接讨论区,比较关键的是证明。首先找到整个数列的最大值 $x$,设整个数列的和是 $sum$,如果 $x \ge sum - x$,那么可以让其他所有元素依次消耗 $x$。如果 $x < sum - x$,那么可以这样思考:设 $y = sum - x$,把 $y$ 中的元素按顺序排列,因为每个人把球传出,对应的 $a_i$ 便会递减 $1$,所以我们可以依次对 $y$ 中的每个人递减 $1$,可以一直这样下去,甚至直到剩下一个或零个人,并且整个过程是连续的,设剩下的元素和为 $z$,那么 $z < y$,并且容易证明 $z < x$(假设 $p - q = z$,如果 $p - q \ge x$,那么有 $p \ge q + x$,这和 $x$ 是最大值矛盾),所以有 $z < x < y$,由于 $y$ 到 $z$ 的过程是连续的,所以一定存在某个时刻恰好等于 $x$,此时就转化成了第一种情况。
// Date: Wed Nov 29 21:36:17 2023
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nonempty(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#else
#define debug1
#define debug2
#define debug3
#endif
const int N = 100010;
int t, n;
ll a[N], sum;
int main(void) {
#ifdef _DEBUG
freopen("1649b.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n;
sum = 0;
ll ma = -1;
For1(i, 1, n) {
cin >> a[i];
sum += a[i];
ma = max(ma, a[i]);
}
if (!sum)
cout << "0\n";
else if (2 * ma <= sum)
cout << 1 << '\n';
else {
cout << 2 * ma - sum << '\n';
}
}
return 0;
}