# Codeforces 672D Robin Hood

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007
const int maxn = 500009;
LL n,k;
LL a[maxn];

void init()
{
cin>>n>>k;
rep(i,1,n) scanf("%I64d",&a[i]);
}

int check( LL x , int type )
{
LL t1 = 0;
LL t2 = 0;
rep(i,1,n)
if ( type == 0 )
{
if ( a[i] <= x )
{
t1 += x - a[i];
t2++;
}
}
else
{
if ( a[i] >= x )
{
t1 += a[i] - x;
t2++;
}
}
t2+=t1;
if ( t1 > k ) return -1;
if ( k >= t2 ) return 1;
return 0;
}

void solve()
{
LL S = 0;
LL m1,m2;
LL m,l,r;
LL ml,mr;
LL temp;
ml = mr = a[1];

rep(i,1,n)
{
S += a[i];
if ( a[i] < ml ) ml = a[i];
if ( a[i] > mr ) mr = a[i];
}
m1 = m2 = -1;
l = ml;
r = S/n;

while( l < r )
{
m = ( l + r )>>1;
temp = check(m , 0);
if (temp == -1) r = m - 1;
else if ( temp == 1 ) l = m + 1;
else
{
m1 = m;
break;
}
}
if (m1==-1) m1 = l;

l = (S+n-1)/n;
r = mr;
while( l < r )
{
m = ( l + r ) >> 1;
temp = check(m , 1);
if (temp == -1) l = m + 1;
else if ( temp == 1 ) r = m - 1;
else
{
m2 = m;
break;
}
}
if (m2==-1) m2 = l;
printf("%I64d\n",m2-m1);
}
int main()
{
init();
solve();
return 0;
}


#### Codeforces 671B Robin Hood (二分搜索)

2017-01-18 16:05:26

#### codeforces 672D 二分

2016-05-17 15:49:56

#### CodeForces - 672D Robin Hood （二分）好题

2016-05-21 11:04:25

#### Codeforces 671B Robin Hood 【思维】

2016-06-16 22:35:58

#### Codeforces 872 B Maximum of Maximums of Minimums

2017-10-16 13:26:02

#### Codeforces 672D Robin Hood【思维+二分】这题思路有点劲啊

2017-02-05 16:23:17

#### CodeForces 672D Robin Hood（二分）

2016-05-14 20:40:31

#### codeforces672D（经典二分）

2017-03-19 11:12:11

#### codefores 352 D. Robin Hood

2016-05-13 21:57:58

#### CodeForces - 672D _Robin Hood_二分

2018-01-21 12:03:39