B. Robin Hood
Time limit per test
1 second
Memory limit per test
Megabytes
Input
Standard input
Output
Standard output
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and he wits to steal the money from rich, and return it to the poor.
There is n citizens in Kekoland, with each person having CI coins. Each day, Robin Hood'll take exactly 1 coin from the richest person in the city and he would give it to the poorest perso N (poorest person right after taking richest ' s 1 coin). In case the choice are not unique, he would select one among them at random. Sadly, Robin Hood is-old and want-retire in K days. He decided to spend these last days with helping poor people.
After taking he money is taken by Robin Hood richest person could become poorest person as well, and it might even happen That Robin Hood would give his money back. For example if all people has same number of coins, then next day they would has 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-integers n and K (1≤n≤500 000, 0≤k≤109)-the number of citizens in Kekol And and the number of days left till Robin Hood ' s retirement.
The second line contains n integers, the i-th of them are 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
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
[1, 1, 4, 2]
[2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3-1 = 2
In second sample wealth would remain the same for each person.
Test instructions
by K times the maximum value minus one to the minimum number, ask the last maximum number and the minimum number of difference between the minimum is how much.
Ideas:
Two points to find the smallest possible number of maximum value, the maximum possible number of minimum, then you can make the difference is minimal.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <
algorithm> #include <cmath> #include <set> using namespace std;
const int n=5e5+9;
typedef long Long LL;
ll sum;
int a[n];
int n,k,pre;
BOOL Ismin (int x) {int t=k;
int Mid=lower_bound (A,A+N,X)-A;
for (int i=0;i<mid;i++) {t-= (x-a[i]);
if (t<0) return 0;
} return 1;
} bool Ismax (int x) {int t=k;
int Mid=upper_bound (A,A+N,X)-A;
for (int i=mid;i<n;i++) {t-= (a[i]-x);
if (t<0) return 0;
} return 1;
} int main () {//freopen ("F.txt", "R", stdin);
scanf ("%d%d", &n,&k);
sum=0;
for (int i=0;i<n;i++) scanf ("%d", &a[i]), sum+=a[i];
Sort (a,a+n);
if (A[0]==a[n-1]) {printf ("0"); return 0;}
pre=sum/n;
int l=pre,r=a[n-1];
if (sum%n) l++;
while (l<r) {int m=l+ (R-L)/2;
if (Ismax (m)) r=m;
else l=m+1;
} int maxn=l;
L=a[0],r=pre; while (l<r) {int m=l+ (r-l+1)/2;
if (Ismin (m)) l=m;
else r=m-1;
} printf ("%d\n", maxn-l);
return 0; }