B - Equal Candies CodeForces - 1676B

这是一个关于如何将不同数量的糖果均匀分配给朋友的问题,你需要从每个盒子里吃掉一些糖果,使得所有盒子最终糖果数相同,求最小的吃糖总数。示例中展示了如何计算这个总数,并给出了一个具体例子的解答过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There are nn boxes with different quantities of candies in each of them. The ii-th box has a_ia
i

candies inside.

You also have nn friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don’t want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.

What’s the minimum total number of candies you have to eat to satisfy the requirements?

Input
The first line contains an integer tt (1 \leq t \leq 10001≤t≤1000) — the number of test cases.

The first line of each test case contains an integer nn (1 \leq n \leq 501≤n≤50) — the number of boxes you have.

The second line of each test case contains nn integers a_1, a_2, \dots, a_na
1

,a
2

,…,a
n

(1 \leq a_i \leq 10^71≤a
i

≤10
7
) — the quantity of candies in each box.

Output
For each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.

Sample 1
Inputcopy Outputcopy
5
5
1 2 3 4 5
6
1000 1000 5 1000 1000 1000
10
1 2 3 5 1 2 7 9 13 5
3
8 8 8
1
10000000
10
4975
38
0
0
Note
For the first test case, you can eat 11 candy from the second box, 22 candies from the third box, 33 candies from the fourth box and 44 candies from the fifth box. Now the boxes have [1, 1, 1, 1, 1][1,1,1,1,1] candies in them and you ate 0 + 1 + 2 + 3 + 4 = 100+1+2+3+4=10 candies in total so the answer is 1010.

For the second test case, the best answer is obtained by making all boxes contain 55 candies in them, thus eating 995 + 995 + 0 + 995 + 995 + 995 = 4975995+995+0+995+995+995=4975 candies in total.

#include<iostream>
using namespace std;
int main(void) {
	int n;
	cin >> n;
	int m;
	int a[1005];
	for (int i = 0; i < n; i++) {
		cin >> m;
		int minv = 1e8;
		int sum = 0;
		for (int j = 0; j < m; j++) {
			cin >> a[j];
			minv = min(minv, a[j]);
		}
		for (int j = 0; j < m; j++) {
			sum += a[j] - minv;
		}
		cout << sum << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值