题目链接:
http://codeforces.com/problemset/problem/758/A
In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury.
Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).
You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.
The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom.
The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen.
In the only line print the integer S — the minimum number of burles which are had to spend.
5 0 1 2 3 4
10
5 1 1 0 1 1
1
3 1 3 1
4
1 12
0
题目大意:
第一行输入一个数n,接下来输入n个数,题目要求计算把这n个数都变为 相等的数 ,至少共需要加多少。
解题思路:
先求出这n个数中的最大值max,然后计算把每个数都变为最大值共需要加多少。
代码:
#include<iostream>
using namespace std;
int main()
{
int a[105], n;
cin >> n;
int max = 0;
for (int i = 0; i <= n - 1; i++) //求n个数中的最大值
{
cin >> a[i];
if (a[i] > max)
max = a[i];
}
int sum = 0;
for (int i = 0; i <= n - 1;i++) //求把每个数都变为最大值需要加多少
sum = sum + max - a[i];
cout << sum << endl;
return 0;
}