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?
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.
Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.
5 5 1 11 2 8
4
4 1 8 8 8
6
2 7 6
0
优先队列。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
int n;
int arr[110];
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n) != EOF)
{
priority_queue<int> pq;
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
for(int i=1;i<n;i++)
{
if(arr[i] >= arr[0])
pq.push(arr[i]);
}
int cnt = 0;
while(!pq.empty())
{
int cur = pq.top();
pq.pop();
if(cur < arr[0]) continue;
cur --;
arr[0] ++;
cnt ++;
if(cur >= arr[0]) pq.push(cur);
}
printf("%d\n",cnt);
}
}