Bear and Elections
Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.
There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get ai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.
Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn’t have many candies and wonders - how many citizens does he have to bribe?
Input
The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.
The second line contains n space-separated integers a1, a2, …, an (1 ≤ ai ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.
Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.
Output
Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.
Examples
Input
5
5 1 11 2 8
Output
4
Input
4
1 8 8 8
Output
6
Input
2
7 6
Output
本题题意:本题意思就是第一行输入一个数字A,第二行输入A个数,要求出至少需要减掉多少次其它的数,才能使得第一个数最大。
本题思路:本题可以用一个优先队列解决,将除第一个数以外的其它数放入优先队列,每次弹出一个最大的值减一,再将相减过的值放入优先队列,重复之前的步骤,直到第一个数最大为止。
AC代码如下:
#include<iostream>
#include<queue>
#include<functional>
using namespace std;
int a[105];
int main()
{
int sum = 0;
priority_queue<int, vector<int>, less<int>>que;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 1; i < n; i++)
{
que.push(a[i]);
}
int m;
int m1 = a[0];
while(m1<=que.top() )
{
m = que.top();
que.pop();
//cout << m << "*";
m1+= 1;
m = m - 1;
que.push(m);
sum += 1;
}
cout << sum << endl;
return 0;
}