As we all know, Honk has nnn pools, numbered as 111 ~ nnn . There is aia_iai liters water in the iii-th pool. Every day, Honk will perform the following operations in sequence.
-
Find the pool with the most water (If there are more than one, choose one at random) and take one liter of water.
-
Find the pool with the least water (If there are more than one, choose one at random) and pour one liter of water into the pool.
-
Go home and rest (Waiting for the next day).
Please calculate the difference between the amount of water in the pool with the most water and the amount of water in the pool with the least water after the kkk days.
Input
The input consists of multiple test cases. The input is terminated by the end of file.The number of data sets will not exceed 40
The first line of each test case contains two integers nnn and kkk, which indicate the number of pools and the number of days to operate the pool.
The second line of each test case contains nnn integers, and the iii-th number represent aia_iai indicating the initial amount of water in the iii-th pool.
1≤n≤5000001 \le n \le 5000001≤n≤500000, 1≤k≤1091 \le k \le 10^91≤k≤109, 1≤ai≤1091 \le a_i \le 10^91≤ai≤109.
Output
For each test case, print one line containing the answer described above.
样例输入1
4 100 1 1 10 10
样例输出1
1
样例输入2
4 3 2 2 2 2
样例输出2
0
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[500005],b[500005];
int main(){
int n,k;
while(scanf("%d %d",&n,&k)!=EOF){
int pj=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pj+=a[i];
}
if(n==1){
printf("0\n");
}
else{
sort(a+1,a+n+1);
for(int i=1;i<=n-1;i++){
b[i]=a[i+1]-a[i];
}
int flag=0;
int flagq,flagh,x,y;
int temp=0;
for(int i=1;i<=n-1;i++){
x=k-temp;
temp=temp+i*b[i];
if(k<temp){
flagq=i-1;
break;
}
if(i==n-1){
flag=1;
}
}
temp=0;
for(int i=1;i<=n-1;i++){
y=k-temp;
temp=temp+i*b[n-i];
if(k<temp){
flagh=i-1;
break;
}
if(i==n-1){
flag=1;
}
}
if(flag==1){
if(pj%n==0){
printf("0\n");
}
else{
printf("1\n");
}
}
if(flag==0){
int mina,maxa;
mina=a[flagq+1]+x/(flagq+1);
maxa=a[n-flagh]-y/(flagh+1);
if(mina>=maxa){
if(pj%n==0){
printf("0\n");
}
else{
printf("1\n");
}
}
else{
printf("%d\n",maxa-mina);
}
}
}
}
}