description
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
题意:
有n个人,每个人有Ti个硬币,有个罗宾汉,每天会从最有钱的人那里偷一个硬币给最穷的人,问你k天后最有钱的人比最穷的人多多少钱。
分析:
利用二分法分别求出k天后最穷的人的钱数与最富的人的钱数,最后将结果相减即可得出答案
说明:
菜鸡表示不会这题。。。
代码:
#include<iomanip>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;long long int T[500005];
int main()
{
long long int n, k;
long long int sum = 0;
long long int Max = 0;
std::ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> T[i];
sum += T[i];
Max = max(Max, T[i]);
}
int left = 0;
int right = sum / n;
while (left != right)
{
int mid = (left + right) / 2 + 1;
long long int total = 0;
for (int i = 0; i < n; i++) {
if (T[i] < mid)
total += mid - T[i];
}
if (total > k)
right = mid - 1;
else
left = mid;
}
int ans_1 = left;left = (sum + n - 1) / n;
right = Max;
while (left != right)
{
int mid = (left + right) / 2;
long long int total = 0;
for (int i = 0; i < n; i++) {
if (T[i] > mid)
total += T[i] - mid;
}
if (total > k)
left = mid + 1;
else
right = mid;
}
int ans_2 = left;
cout << ans_2 - ans_1 << endl;
return 0;
}