As we all know, Honk has nn pools, numbered as 11 ~ nn . There is a_iai liters water in the ii-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 kk 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 nn and kk, which indicate the number of pools and the number of days to operate the pool.
The second line of each test case contains nn integers, and the ii-th number represent a_iai indicating the initial amount of water in the ii-th pool.
1 ≤ n ≤ 500000 1≤n≤500000, 1 ≤ k ≤ 10^9 1≤k≤10^9, 1 ≤ ai ≤ 10^9 1≤ai≤10^9.
Output
For each test case, print one line containing the answer described above.
样例输入
4 100
1 1 10 10
样例输出
1
样例输入
4 3
2 2 2 2
样例输出
0
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 50001
int main(){
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
int a[maxn];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
while(m--){
a[n-1]--;
//手写一个排序算法代码,如果直接用sort,sort里的代码有些用不到,效率低下,运行超时
for(int i=n-2;i>=0;i--){
if(a[i+1]<a[i]){
int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp;
}else break;
}
a[0]++;
for(int i=1;i<n;i++){
if(a[i-1]>a[i]){
int temp = a[i]; a[i] = a[i-1]; a[i-1] = temp;
}else break;
}
}
printf("%d\n",a[n-1]-a[0]);
}
return 0;
}