### 前置知识
贪心。
### 正解
设水量和为 $sum$。
$$
sum = \sum_{i = 1}^n a_i
$$
则可以计算出最后每桶中水的数量 $per = \lfloor(\frac{sum}{n})\rfloor$,采取贪心思路,对于任意 $a_i > per$,我们都需要加上 $a_i - per$,因为多余的要么转移要么抛弃。
### 代码
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int N = 2e5;
int T, n, a[N];
int main(void) {
cin.tie(0) -> ios::sync_with_stdio(false);
cin >> T;
while (T--) {
ll sum = 0, ans = 0;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i], sum += a[i];
int per = sum / n;
for (int i = 1; i <= n; ++i)
if (a[i] > per)
ans += (a[i] - per);
cout << ans << '\n';
}
return 0;
}
```