#include<iostream>
#include<map>
#include<unordered_map>
#include<vector>
#include<set>
#include<algorithm>
#include<iomanip>
#include<bits/stdc++.h>
#include<cmath>
#include<string>
#include<numeric>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int f[N];

void solve(){
  // int a[10][10];
int n,m;
cin>>n>>m;
string s="";
for(int i=0;i<n;i++){
  if(i%2==0){
    s+='B';
  }else{
    s+='W';
  }

}
for(int i=0;i<n;i++){
  cout<<s<<endl;
}

}

int main() {
    fast_cin();
    int t;
    cin>>t;
    while(t--){
      solve();
    }

return 0;
}


#include <bits/stdc++.h>

const int M=2e5+9;
const int MOD=1e9+7;
const long long N=1e2+7;

using namespace std;

//vector<long long>primes;

/*void sieve(){
    vis[0]=vis[1]=1;
    for(long long i=2;i*i<=M;++i){
        if(vis[i]==0){
            primes.push_back(i);
            for(long long j=i*i;j<=M;j+=i)
                vis[j]=1;
        }
    }
}

bool isprime(long long n){
    if(n==2)return true;
    else if(!(n&1))return false;
    for(long long i=3;i*i<=n;i+=2)
        if(n%i==0)
            return false;
    return true;
}*/

int t,n,m;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        int b=(n*m/2)+1;
        int w=(n*m)-b;
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if((j&1)&&w){
                    cout<<'W';
                    --w;
                }else
                    cout<<'B';
                    --b;
            }
            cout<<endl;
        }
    }

    return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t,n,m,i,j;
	cin>>t;
	while(t--){
		cin>>n;
		cin>>m;

		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(i == 0 && j==m-1){
					cout<<"W";
				}
				else{
					cout<<"B";
				}
			}
			cout<<"\n";
		}
	}
}


#include <bits/stdc++.h>

const int M=2e5+9;
const int MOD=1e9+7;
const long long N=1e2+7;

using namespace std;

//vector<long long>primes;

/*void sieve(){
    vis[0]=vis[1]=1;
    for(long long i=2;i*i<=M;++i){
        if(vis[i]==0){
            primes.push_back(i);
            for(long long j=i*i;j<=M;j+=i)
                vis[j]=1;
        }
    }
}

bool isprime(long long n){
    if(n==2)return true;
    else if(!(n&1))return false;
    for(long long i=3;i*i<=n;i+=2)
        if(n%i==0)
            return false;
    return true;
}*/

int t,n,m;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        int b=(n*m/2)+1;
        int w=(n*m)-b;
        //cout<<b<<w;
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(j&1){
                    if(w){
                        cout<<'W';
                        --w;
                    }else if(b){
                        cout<<'B';
                        --b;
                        }
                }else{
                    if(b){
                        cout<<'B';
                        --b;
                    }else if(w){
                        cout<<'W';
                        --w;
                    }
                }
            }
            cout<<endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

const int M=2e5+9;
const int MOD=1e9+7;
const long long N=1e2+7;

using namespace std;

//vector<long long>primes;

/*void sieve(){
    vis[0]=vis[1]=1;
    for(long long i=2;i*i<=M;++i){
        if(vis[i]==0){
            primes.push_back(i);
            for(long long j=i*i;j<=M;j+=i)
                vis[j]=1;
        }
    }
}

bool isprime(long long n){
    if(n==2)return true;
    else if(!(n&1))return false;
    for(long long i=3;i*i<=n;i+=2)
        if(n%i==0)
            return false;
    return true;
}*/

int t,n,m;
char mat[105][105];
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        int b=(n*m/2)+1;
        int w=(n*m)-b;
        bool k=0;
        for(int i=0;i<n-1;++i){
            for(int j=0;j<m;++j){
                if((j&1)&&w){
                    cout<<'W';
                    --w;
                    k=0;
                }else{
                    cout<<'B';
                    k=1;
                    --b;
                }
            }
            cout<<endl;
        }
    k=1;
    if(w>b){
        while(b>0){
            cout<<"WB";
            --b;
            --w;
        }
        while(w>0){
            cout<<'W';
            --w;
        }
    }else{
        while(w>0){
            cout<<"BW";
            --b;
            --w;
        }
        while(b>0){
            cout<<'B';
            --b;
        }
    }
    cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>

const int M=2e5+9;
const int MOD=1e9+7;
const long long N=1e2+7;

using namespace std;

//vector<long long>primes;

/*void sieve(){
    vis[0]=vis[1]=1;
    for(long long i=2;i*i<=M;++i){
        if(vis[i]==0){
            primes.push_back(i);
            for(long long j=i*i;j<=M;j+=i)
                vis[j]=1;
        }
    }
}

bool isprime(long long n){
    if(n==2)return true;
    else if(!(n&1))return false;
    for(long long i=3;i*i<=n;i+=2)
        if(n%i==0)
            return false;
    return true;
}*/

int t,n,m;
char mat[105][105];
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        int b=(n*m/2)+1;
        int w=(n*m)-b;
        bool k=0;
        for(int i=0;i<n-1;++i){
            for(int j=0;j<m;++j){
                if((j&1)&&w){
                    cout<<'W';
                    --w;
                    k=0;
                }else{
                    cout<<'B';
                    k=1;
                    --b;
                }
            }
            cout<<endl;
        }
    k=1;
    if(w>b){
        while(b>0){
            cout<<"WB";
            --b;
            --w;
        }
        while(w>0){
            cout<<'W';
            --w;
        }
    }else{
        while(w>0){
            cout<<"BW";
            --b;
            --w;
        }
        while(b>0){
            cout<<'B';
            --b;
        }
    }
    cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

const int M=2e5+9;
const int MOD=1e9+7;
const long long N=1e2+7;

using namespace std;

//vector<long long>primes;

/*void sieve(){
    vis[0]=vis[1]=1;
    for(long long i=2;i*i<=M;++i){
        if(vis[i]==0){
            primes.push_back(i);
            for(long long j=i*i;j<=M;j+=i)
                vis[j]=1;
        }
    }
}

bool isprime(long long n){
    if(n==2)return true;
    else if(!(n&1))return false;
    for(long long i=3;i*i<=n;i+=2)
        if(n%i==0)
            return false;
    return true;
}*/

int t,n,m;
char mat[105][105];
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        int b=(n*m/2)+1;
        int w=(n*m)-b;
        bool k=0;
        for(int i=0;i<n-1;++i){
            for(int j=0;j<m;++j){
                if(k){
                    cout<<'W';
                    k=0;
                    --w;
                }else{
                    cout<<'B';
                    k=1;
                    --b;
                }
            }
            cout<<endl;
        }
        k=0;
        while(b||w){
            if(w&&k){
                cout<<'W';
                k=0;
                --w;
            }else{
                cout<<'B';
                k=1;
                --b;
            }
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M 1000000007
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;cin>>t;
    while(t--)
    {
          ll n,m;cin>>n>>m;
          ll k=0;
          if((n*m)%2==0)
          {
          cout<<"BB";
          for(ll j=2;j<m;j++)
           {
                      if(j%2!=0)cout<<'B';
                      else cout<<"W";
                }
                cout<<endl;
                k=1;
          }
          ll k1=(n*m)/2;
          k1++;ll c=0;
          for(ll i=k;i<n;i++)
          {
                for(ll j=0;j<m;j++)
                {
                      if(j%2==0&&c<k1){cout<<'B';c++;}
                      else cout<<"W";
                }
                cout<<endl;
          }

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M 1000000007
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;cin>>t;
    while(t--)
    {
          ll n,m;cin>>n>>m;
          ll k=1;ll c=0;ll k1=(n*m)/2;
          if((n*m)%2==0)k1--;
          for(ll i=0;i<n;i++)
          {
                for(ll j=0;j<m;j++)
                {
                      if(k%2!=0)cout<<'B';

                     else if(k%2==0&&c<k1)
                     {
                           cout<<'W';c++;

                     }
                      else cout<<'B';

                      k++;
                }
                cout<<endl;
          }

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M 1000000007
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;cin>>t;
    while(t--)
    {
          ll n,m;cin>>n>>m;
          ll k=1;ll c=0;ll k1=(n*m)/2;ll k2=0;
             if((n*m)%2==0)
          {
          cout<<"BB";
          for(ll j=2;j<m;j++)
           {
                      if(j%2!=0){cout<<'B';}
                      else{ cout<<"W";c++;}
                }
                cout<<endl;
                k2=1;
          }
          for(ll i=k2;i<n;i++)
          {
                for(ll j=0;j<m;j++)
                {
                      if(k%2!=0)cout<<'B';

                     else if(k%2==0&&c<k1)
                     {
                           cout<<'W';c++;

                     }
                      else cout<<'B';

                      k++;
                }
                cout<<endl;
          }

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M 1000000007
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;cin>>t;
    while(t--)
    {
          ll n,m;cin>>n>>m;
          ll k=0;ll c=0;
          if((n*m)%2==0)
          {
          cout<<"BB";c++;
          for(ll j=2;j<m;j++)
           {
                      if(j%2!=0){cout<<'B';c++;}
                      else cout<<"W";
                }
                cout<<endl;
                k=1;
          }
          ll k1=(n*m)/2;
          k1+=1;//cout<<k1<<endl;
          for(ll i=k;i<n;i++)
          {
                for(ll j=0;j<m;j++)
                {
                      if(c<k1&&j%2==0){c++;cout<<'B';}
                      else cout<<'W';
                }
                cout<<endl;
          }

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M 1000000007
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;cin>>t;
    while(t--)
    {
          ll n,m;cin>>n>>m;
          cout<<"BB";
          for(ll j=2;j<m;j++)
           {
                      if(j%2!=0)cout<<'B';
                      else cout<<"W";
                }
                cout<<endl;
          for(ll i=1;i<n;i++)
          {
                for(ll j=0;j<m;j++)
                {
                      if(j%2==0)cout<<'B';
                      else cout<<"W";
                }
                cout<<endl;
          }
    }

	return 0;
}


#include <bits/stdc++.h>

template <typename Ty>
inline void read(Ty &t) {
	t = 0; bool neg = 0; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') neg = 1; ch = getchar(); }
	do { t = t * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
	if (neg) t = -t;
}

#define int long long

const signed N = 1e5 + 5;

int n, k, a[N];

inline bool check(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (x > a[i]) res += x - a[i];
	return res <= k;
}

inline bool check2(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (a[i] > x) res += a[i] - x;
	return res > k;
}

void solve() {
	memset(a, 0, sizeof(a));
	read(n), read(k);
	for (int i = 1; i <= n; ++i) read(a[i]);
	int sum = 0, mx = 0;
	for (int i = 1; i <= n; ++i) sum += a[i], mx = std::max(mx, a[i]);
	int l, r, mid, res1, res2;
	l = 1, r = sum / n;
	bool flag = 0;
	while (l <= r) {
		mid = (l + r) / 2;
		if (!flag) flag = 1;
		if (check(mid)) l = mid + 1, res1 = mid; else r = mid - 1;
	}
	l = (sum + n - 1) / n, r = mx;
	while (l <= r) {
		mid = (l + r) / 2;
		if (check2(mid)) l = mid + 1; else r = mid - 1, res2 = mid;
	}
	printf("%lld\n", res2 - res1);
}

signed main() {
	int T = 1;
	//read(T);
	while (T--) solve();
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,a[500010],ans=inf,ave;
ll k;
void read(int &x)
{
    char s=getchar();
    while(s<'0'||s>'9')
        s=getchar();
    while(s>='0'&&s<='9')
    {
        x=(x<<3)+(x<<1)+s-'0';
        s=getchar();
    }
}
void work(int ort,ll opt)
{
	int L=ave,R=n,Mid;
	while(L<=R)
	{
		ll Opt=0;
		Mid=(L+R)>>1;
		for(int i=Mid+1;i<=n;++i)
			Opt+=a[i]-a[Mid];
		if(Opt==opt)
		{
			ans=min(ans,a[Mid]-a[ort]);
			break;
		}
		else
			if(Opt<k)
				R=Mid-1;
		else
			L=Mid+1;
	}
}
int main()
{
    scanf("%d%lld",&n,&k);
    for(int i=1;i<=n;++i)
    	read(a[i]),ave+=a[i];
    int l=1,r=ave/n,mid;
    ave=ceil(1.0*ave/n);
    sort(a+1,a+n+1);
    while(l<=r)
    {
    	ll opt=0;
    	mid=(l+r)>>1;
    	for(int i=1;i<mid;++i)
    		opt+=a[mid]-a[i];
    	if(opt==k)
    		work(mid,opt);
    	else
	    	if(opt>k)
	    	{
	    		work(mid-1,opt);
	    		r=mid-1;
	    	}
    	else
    	{
    		work(mid+1,opt);
    		l=mid+1;
    	}
    }
    cout<<ans;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ri register int
using namespace std;
const int maxn=5e5+5;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll n,k,a[maxn],m=0;
ll mn=inf,mx=-inf,l,r,bound=0;
bool flag=false;
//
//bool check_min(ll x){
//	ll ret=0;
//	for(ri i=1;i<=n;i++)
//		if(a[i]<x) ret+=x-a[i];
//	return ret>k;
//}
//
//bool check_max(ll x){
//	ll ret=0;
//	for(ri i=1;i<=n;i++)
//		if(a[i]>x) ret+=a[i]-x;
//	return ret<=k;
//}

int main(){
	scanf("%lld%lld",&n,&k);
	for(ri i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		mx=max(mx,a[i]);
		mn=min(mn,a[i]);
		m+=a[i];
	}
	flag=m%n; m/=n;
	for(ri i=1;i<=n;i++){
		if(a[i]<=m) bound+=m-a[i];
		else bound+=a[i]-m+1;
	}
	if(k>=bound) {
		printf("%d",flag);
		return 0;
	}
	l=0,r=inf;
	while(l<r){
		ll ret=0;
		ll mid=l+r>>1ll;
		for(ri i=1;i<=n;i++)
		if(a[i]<mid) {
			ret+=mid-a[i];
			if(ret>k) break;
		}
		if(ret>=k) r=mid;
		else l=mid+1;
	}
	mn=l;
	l=0,r=inf;
	while(l<r){
		ll ret=0;
		ll mid=l+r>>1ll;
		for(ri i=1;i<=n;i++)
		if(a[i]>mid) {
			ret+=a[i]-mid;
			if(ret>k) break;
		}
		if(ret>=k) l=mid+1;
		else r=mid;
	}
	mx=l;
	printf("%lld",mx-mn);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#define N 500005
using namespace std;
int n,k,a[N],l,rht,r,let,sum,mid,maxn,minn=0x3f3f3f3f;
bool flag;
bool check1(int mid)
{
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>=mid)
			cnt+=0;
		else
			cnt+=mid-a[i];
		if(cnt>=k)
			return true;
	}
	return false;
}
bool check2(int mid)
{
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]<=mid)
			cnt+=0;
		else
			cnt+=a[i]-mid;
		if(cnt>=k)
			return true;
	}
	return false;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
		maxn=max(maxn,a[i]);
		minn=min(minn,a[i]);
	}
	for(int i=1;i<n;i++)
	{
		if(a[i]!=a[i+1])
		{
			flag=1;
			break;
		}
	}
	if(flag==0)
	{
		printf("0");
		return 0;
	}
	l=minn;
	rht=sum/n;
	while(l<=rht)
	{
		mid=l+(rht-l)/2;
		if(check1(mid))
			rht=mid-1;
		else
			l=mid+1;
	}
	l--;
	let=sum/n+1;
	r=maxn;
	while(let<=r)
	{
		mid=let+(r-let)/2;
		if(check2(mid))
			r=mid-1;
		else
			let=mid+1;
	}
	r++;
	int fuck=abs(r-l);
	printf("%d",fuck);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long t;
int a[500005],k,n,m,x,y,z;
int main()
{
	cin >> n >> k;
	for(int i=1 ; i<=n ; i++)
		cin >> a[i];
	int l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		if(l==tmp)
			tmp++;
		t = 0;
		for(int i=1 ; i<=n ; i++)
			t += max(0 , tmp-a[i]);
		if(t <= k)
			l = tmp;
		else
			r = tmp-1;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , l-a[i]);
		a[i] = max(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]++;
		}
	l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		t = 0;
		for(int
		i=1 ; i<=n ; i++)
			t += max(0 , a[i]-tmp);
		if(t <= k)
			r = tmp;
		else
			l = tmp+1;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , a[i]-l);
		a[i] = min(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]--;
		}
	cout << *max_element(a+1,a+1+n) - *min_element(a+1,a+1+n) << endl;
}


#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,k,c[N],mx,sum;
inline int check1(int mid){
	int res=0;
	for(int i=1;i<=n;i++)	if(c[i]>mid)	res+=c[i]-mid;
	return res<=k;
}
inline int check2(int mid){
	int res=0;
	for(int i=1;i<=n;i++)	if(c[i]<mid)	res+=mid-c[i];
	return res<=k;
}
signed main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++)	scanf("%lld",&c[i]),sum+=c[i];
	int l=1,r=1e9;
	while(l<r){
		int mid=l+r>>1;
		if(check1(mid))	r=mid;
		else	l=mid+1;
	}
	mx=r;	l=1,r=1e9;
	while(l<r){
		int mid=l+r+1>>1;
		if(check2(mid))	l=mid;
		else r=mid-1;
	}
	cout<<max((sum%n?1LL:0LL),mx-l);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const long long fuck_qhw=0x3f3f3f3f;
long long n,k;
long long minn=fuck_qhw;
long long maxn;
long long sum;
long long a[500005];
bool checkmin(long long x)
{
	long long cnt=k;
	for(long long i=1;i<=n;i++)
	{
		if(a[i]<x) cnt-=(x-a[i]);
		if(cnt<0) return false;
	}
	return true;
}
bool checkmax(long long y)
{
	long long cnt=k;
	for(long long i=1;i<=n;i++)
	{
		if(a[i]>y) cnt-=(a[i]-y);
		if(cnt<0) return false;
	}
	return true;
}
int main()
{
	scanf("%lld%lld",&n,&k);
	for(long long i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		sum+=a[i];
		minn=min(minn,a[i]);
		maxn=max(maxn,a[i]);
	}
	long long ave=sum/n;
	long long l=minn;
	long long r=ave;
	long long mid;
	long long ans1;
	long long ans2;
	while(l<=r)
	{
		mid=l+(r-l)/2;
		if(checkmin(mid))
		{
			l=mid+1;
			ans1=mid;
		}
		else
		r=mid-1;
	}
	l=ave;
	r=maxn;
	while(l<=r)
	{
		mid=l+(r-l)/2;
		if(checkmax(mid))
		{
			r=mid-1;
			ans2=mid;
		}
		else
		l=mid+1;
	}
	printf("%lld",ans2-ans1);
	return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
const int N=1e6+1;
typedef long long ll;
ll m,n,a[N],sum,l,r,mid,k,ave,t;
bool flag;
bool judge (ll x)
{
	sum=0;
	if(!flag) {
		for(int i=1;i<=n;++i) {
			if(a[i]<x) sum+=x-a[i];
		}
		//printf("#%I64d,%I64d\n",sum,x);
		if(sum<=k) return true;
		return false;
	}

	for(int i=1;i<=n;++i) {
		if(a[i]>x) sum+=a[i]-x;
	}
	//printf("#%I64d,%I64d\n",sum,x);
	if(sum<=k) return true;
	return false;
}

int main()
{
	scanf("%I64d %I64d",&n,&k);
	for(int i=1;i<=n;++i) {
		scanf("%I64d",&a[i]);
		ave+=a[i];
	}

	l=1,r=ave/n;
	while(l<=r)
	{
		mid=l + ( (r-l)>>1 );
		if(judge(mid)) l=mid+1;
		else r=mid-1;
	}
	t=l-1;
	flag=1;
	l=ave/n,r=1e10;
	while(l<=r)
	{
		mid=l + ( (r-l)>>1 );
		if(judge(mid)) r=mid-1;
		else l=mid+1;
	}
	//printf("#%I64d,%I64d\n",t,r+1);
	printf("%I64d\n",r+1-t);

}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[500005];
int n,k,maxx;
int l,mid,r,ans1,ans2;
bool cmp(int a,int b)
{
	return a<b;
}
bool check(int x)
{
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>=x) break;
		sum+=x-a[i];
	}
	if(sum<k) return 1;
	return 0;
}
bool chock(int x)
{
	int sum=0;
	for(int i=n;i>=1;i--)
	{
		if(a[i]<=x) break;
		sum+=a[i]-x;
	}
	if(sum<=k) return 1;
	return 0;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		maxx=maxx>a[i]?maxx:a[i];
	}
	sort(a+1,a+n+1,cmp);
	l=1;r=maxx;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(check(mid))
		{
			ans1=mid;
			l=mid+1;
		}
		else
		{
			r=mid-1;
		}
	}
	l=1;r=maxx;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(chock(mid))
		{
			ans2=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	cout<<ans2-ans1;
	return 0;
}


#include <bits/stdc++.h>

template <typename Ty>
inline void read(Ty &t) {
	t = 0; bool neg = 0; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') neg = 1; ch = getchar(); }
	do { t = t * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
	if (neg) t = -t;
}

#define int long long

const signed N = 1e5 + 5;

int n, k, a[N];

inline bool check(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (x > a[i]) res += x - a[i];
	return res <= k;
}

inline bool check2(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (a[i] > x) res += a[i] - x;
	return res > k;
}

void solve() {
	memset(a, 0, sizeof(a));
	read(n), read(k);
	for (int i = 1; i <= n; ++i) read(a[i]);
	int sum = 0, mx = 0;
	for (int i = 1; i <= n; ++i) sum += a[i], mx = std::max(mx, a[i]);
	int l, r, mid;
	l = 1, r = sum / n;
	bool flag = 0;
	while (l < r) {
		mid = (l + r + flag) / 2;
		if (!flag) flag = 1;
		if (check(mid)) l = mid; else r = mid - 1;
	}
	int res1 = l;
	l = (sum + n - 1) / n, r = mx;
	while (l < r) {
		mid = (l + r) / 2;
		if (check2(mid)) l = mid + 1; else r = mid;
	}
	printf("%lld\n", l - res1);
}

signed main() {
	int T = 1;
	//read(T);
	while (T--) solve();
	return 0;
}


#include <bits/stdc++.h>

template <typename Ty>
inline void read(Ty &t) {
	t = 0; bool neg = 0; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') neg = 1; ch = getchar(); }
	do { t = t * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
	if (neg) t = -t;
}

#define int long long

const signed N = 1e5 + 5;

int n, k, a[N];

inline bool check(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (x > a[i]) res += x - a[i];
	return res <= k;
}

inline bool check2(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (a[i] > x) res += a[i] - x;
	return res > k;
}

void solve() {
	memset(a, 0, sizeof(a));
	read(n), read(k);
	for (int i = 1; i <= n; ++i) read(a[i]);
	int sum = 0, mx = 0;
	for (int i = 1; i <= n; ++i) sum += a[i], mx = std::max(mx, a[i]);
	int l, r, mid;
	l = 1, r = sum / n;
	while (l < r) {
		mid = (l + r + 1) / 2;
		if (check(mid)) l = mid; else r = mid - 1;
	}
	int res1 = l;
	l = (sum + n - 1) / n, r = mx;
	while (l < r) {
		mid = (l + r) / 2;
		if (check2(mid)) l = mid + 1; else r = mid;
	}
	printf("%lld\n", l - res1);
}

signed main() {
	int T = 1;
	while (T--) solve();
	return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc = 1000000000;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    minc = min(minc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = maxc - minc;
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r) / 2;
      if (sum1(mid) > k) l = mid + 1;
      else r = mid;
    }
    maxc = r;
    ans = max((int)((all % n) != 0), maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = c[n] - c[1];
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum1(mid) > k) r = mid - 1;
      else l = mid;
    }
    maxc = r;
    ans = max((int)((all % n) != 0), maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = c[n] - c[1];
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r) / 2;
      if (sum1(mid) <= k) r = mid;
      else l = mid + 1;
    }
    maxc = r;
    ans = max((int)((all % n) != 0), maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = c[n] - c[1];
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r) / 2;
      if (sum1(mid) <= k) r = mid;
      else l = mid + 1;
    }
    maxc = r;
    ans = max(0, maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = c[n] - c[1];
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum1(mid) >= k) l = mid;
      else r = mid - 1;
    }
    maxc = r;
    ans = max((int)((all % n) != 0), maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


/*************************************************************************
	> File Name: B1.cpp
	> Author: dragon007
	> Mail: wanglong.wu@vipl.ict.ac.cn
	> Created Time: 2016年07月28日 星期四 21时06分11秒
 ************************************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, k;
LL c[500010];

LL sum(int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, x - c[i]);
  return res;
}

LL sum1( int x) {
  LL res = 0;
  for (int i = 1; i <= n; ++ i)
    res += max(0LL, c[i] - x);
  return res;
}

int main() {
  cin>>n>>k;
  int maxc = 0, minc;
  LL all = 0;
  for (int i = 1; i <= n; ++ i) {
    cin>>c[i];
    maxc = max(maxc, int(c[i]));
    all += c[i];
  }

  int ans = 0;
  if (k == 0) {
    ans = c[n] - c[1];
  }
  else {
    LL l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (sum(mid) <= k) l = mid;
      else r = mid - 1;
    }
    minc = l;
    l = 0, r = maxc;
    while (l < r) {
      int mid = (l + r) / 2;
      if (sum1(mid) > k) l = mid + 1;
      else r = mid;
    }
    maxc = r;
    ans = max((int)((all % n) != 0), maxc - minc);
  }

  cout << ans << endl;
  return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
const int N=1e6+1;
typedef long long ll;
ll m,n,a[N],sum,l,r,mid,k,ave,t;
bool flag;
bool judge (ll x)
{
	sum=0;
	if(!flag) {
		for(int i=1;i<=n;++i) {
			if(a[i]<x) sum+=x-a[i];
		}
		//printf("#%lld,%lld\n",sum,x);
		if(sum<=k) return true;
		return false;
	}

	for(int i=1;i<=n;++i) {
		if(a[i]>x) sum+=a[i]-x;
	}
	//printf("#%lld,%lld\n",sum,x);
	if(sum<=k) return true;
	return false;
}

int main()
{
	scanf("%lld %lld",&n,&k);
	for(int i=1;i<=n;++i) {
		scanf("%lld",&a[i]);
		ave+=a[i];
	}
	ave/=n;
	l=1,r=ave+1;
	while(l<=r)
	{
		mid=l + ( (r-l)>>1 );
		if(judge(mid)) l=mid+1;
		else r=mid-1;
	}
	t=l-1;
	flag=1;
	l=ave,r=1e10;
	while(l<=r)
	{
		mid=l + ( (r-l)>>1 );
		if(judge(mid)) r=mid-1;
		else l=mid+1;
	}
	printf("%lld\n",r+1-t);

}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAX 500010
using namespace std;
long long n,k,sum=0,mi=0,ma=0,a[MAX];
long long l1,r1,l2,r2;
bool check(long long x)
{
	long long ans=0;
	for(int i=1;a[i]<=x&&i<=n;i++)
		ans+=x-a[i];
	if(ans<=k)
		return true;
	return false;
}
bool check2(long long x)
{
	long long ans=0;
	for(int i=n;a[i]>=x&&i>=1;i--)
		ans+=a[i]-x;
	if(ans<=k)
		return true;
	return false;
}
int main()
{
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		sum+=a[i];
	}
	sort(a+1,a+n+1);
	l1=0,r1=sum/n;
	while(l1<=r1){
		int mid=(l1+r1)>>1;
		if(check(mid)){
			mi=mid;
			l1=mid+1;
		}
		else
			r1=mid-1;
	}
	l2=sum/n;
	if(sum%n)
		l2++;
	r2=a[n];
	while(l2<=r2){
		long long mid2=(l2+r2)>>1;

		if(check2(mid2)){
			ma=mid2;
			r2=mid2-1;
		}
		else
			l2=mid2+1;
	}
	printf("%lld\n",ma-mi);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long t;
int a[50005],k,n,m,x,y,z;
int main()
{
	cin >> n >> k;
	for(int i=1 ; i<=n ; i++)
		cin >> a[i];
	int l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		if(l==tmp)
			tmp++;
		t = 0;
		for(int i=1 ; i<=n ; i++)
			t += max(0 , tmp-a[i]);
		if(t <= k)
			l = tmp;
		else
			r = tmp-1;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , l-a[i]);
		a[i] = max(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]++;
		}
	l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		t = 0;
		for(int
		i=1 ; i<=n ; i++)
			t += max(0 , a[i]-tmp);
		if(t <= k)
			r = tmp;
		else
			l = tmp;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , a[i]-l);
		a[i] = min(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]--;
		}
	cout << *max_element(a+1,a+1+n) - *min_element(a+1,a+1+n) << endl;
}


#include <cstdio>
#include <algorithm>
const int N(5 * 1e5 + 10);
typedef long long ll;

ll n,k,a[N],l,rht,r,let,sum,mid,maxn,ans1,ans2;

bool check1(ll mid)
{
	ll cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i] < mid)
			cnt += mid - a[i];
		else
			break;
	}
	return cnt > k;
}

bool check2(ll mid)
{
	ll cnt=0;
	for(int i(n); i > 0; --i)
	{
		if(a[i] > mid)
			cnt += a[i] - mid;
		else
			break;
	}
	return cnt > k;
}

int main()
{
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		sum+=a[i];
	}
	std::sort(a + 1, a + 1 + n);
	l = a[1];
	r = sum/n;
	while(l <= r)
	{
		mid=l+(r-l)/2;
		if(check1(mid))
			r=mid-1;
		else
		{
			l=mid+1;
			ans1=mid;
		}
	}
	l = sum / n + static_cast<bool>(sum % n);
	r=a[n];
	while(l<=r)
	{
		mid=l+(r-l)/2;
		if(check2(mid))
			l = mid + 1;
		else
		{
			r = mid - 1;
			ans2=mid;
		}

	}
	printf("%lld",ans2 - ans1);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[500010],n,k;
bool c1(int mid)
{
    long long sum=0;
    for(int i=1;i<=n;++i)
	{
        if(a[i]>=mid)break;
		sum+=mid-a[i];
    }
    return sum<=k;
}
bool c2(int mid)
{
    long long sum=0;
    for(int i=n;i>=1;--i)
	{
        if(a[i]<=mid)break;
        sum+=a[i]-mid;
    }
    return sum<=k;
}
int main()
{
    scanf("%d%d",&n,&k);
    long long sum=0;
    for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a+1,a+n+1);
    long long mod=sum%n;
    int l=1,r=sum/n,ans;
    while(l<=r)
	{
        int mid=l+((r-l)>>1);
        if(c1(mid))
		{
            l=mid+1;
            ans=mid;
        }
		else r=mid-1;
    }
    l=ans+!(mod==0);
	r=a[n];
	int ans2;
    while(l<=r)
	{
        int mid=l+((r-l)>>1);
        if(c2(mid))
		{
            r=mid-1;
            ans2=mid;
        }
		else l=mid+1;
    }
    ans=ans2-ans;
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAX 500010
using namespace std;
int n,k,sum=0,mi=0,ma=0,a[MAX];
int l1,r1,l2,r2;
bool check(int x)
{
	int ans=0;
	for(int i=1;a[i]<=x;i++)
		ans+=x-a[i];
	if(ans<=k)
		return true;
	return false;
}
bool check2(int x)
{
	int ans=0;
	for(int i=n;a[i]>=x;i--)
		ans+=a[i]-x;
	if(ans<=k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	sort(a+1,a+n+1);
	l1=a[1];r2=a[n];
	if(sum%n==0)
		r1=l2=sum/n;
	else
		r1=sum/n,l2=sum/n+1;
	while(l1<r1){
		int mid=l1+((r1-l1)>>1);
		if(check(mid)){
			mi=mid;
			l1=mid+1;
		}
		else
			r1=mid;
	}
	while(l2<r2){
		int mid2=l2+((r2-l2)>>1);
		if(check2(mid2)){
			ma=mid2;
			r2=mid2;
		}
		else
			l2=mid2+1;
	}
	printf("%d\n",ma-mi);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ri register int
using namespace std;
const int maxn=5e5+5;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll n,k,a[maxn],m=0;
ll mn=inf,mx=-inf,l,r,bound=0;
int main(){
	scanf("%lld%lld",&n,&k);
	for(ri i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		mx=max(mx,a[i]);
		mn=min(mn,a[i]);
		m+=a[i];
	}
	ll ave=m/n;
	l=1,r=ave;
	while(l<r){
		ll ret=0,mid=l+r+1>>1ll;
		for(ri i=1;i<=n;i++) ret+=max(0ll,mid-a[i]);
		ret<=k?l=mid:r=mid-1;
	}	mn=l;
	l=ave+(m%n!=0),r=mx;
	while(l<r){
		ll ret=0,mid=l+r>>1ll;
		for(ri i=1;i<=n;i++) ret+=max(0ll,a[i]-mid);
		ret<=k?r=mid:l=mid+1;
	}	mx=l;
	printf("%lld",mx-mn);
	return 0;
}


#include<cstdio>
#include<algorithm>
int ll,rr,n,f[500005];
long long k,sum=0;
int check_min(int x)
{
	long long now=0;
	for (int i=1;i<=n;i++) now=now+1ll*std::max(0,x-f[i]);
	printf("cmin %d=%lld\n",x,now);
	if (now<=k) return 1;
	else return 0;
}
int check_max(int x)
{
	long long now=0;
	for (int i=1;i<=n;i++) now=now+1ll*std::max(0,f[i]-x);
	printf("cmax %d=%lld\n",x,now);
	if (now<=k) return 1;
	else return 0;
}
int bs_min(int l,int r)
{
	printf("%d %d\n",l,r);
	if (l==r) return l;
	int mid=(l+r+1)/2;
	if (check_min(mid)) return bs_min(mid,r);
	else return bs_min(l,mid-1);
}
int bs_max(int l,int r)
{
	printf("%d %d\n",l,r);
	if (l==r) return l;
	int mid=(l+r)/2;
	if (check_max(mid)) return bs_max(l,mid);
	else return bs_max(mid+1,r);
}
int main()
{
	scanf("%d%lld",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&f[i]),sum=sum+f[i];
	ll=bs_min(0,1000000000);
	rr=bs_max(0,1000000000);
	printf("qaq::%d %d\n",ll,rr);
	printf("%d\n",std::max(rr-ll,sum&1));
	return 0;
}


#include <bits/stdc++.h>

template <typename Ty>
inline void read(Ty &t) {
	t = 0; bool neg = 0; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') neg = 1; ch = getchar(); }
	do { t = t * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
	if (neg) t = -t;
}

#define int long long

const signed N = 1e5 + 5;

int n, k, a[N];

inline bool check(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (x > a[i]) res += x - a[i];
	return res <= k;
}

inline bool check2(int x) {
	int res = 0;
	for (int i = 1; i <= n; ++i) if (a[i] > x) res += a[i] - x;
	return res <= k;
}

void solve() {
	read(n), read(k);
	for (int i = 1; i <= n; ++i) read(a[i]);
	int sum = 0, mx = -1, mn = 2e9;
	for (int i = 1; i <= n; ++i)
		sum += a[i], mx = std::max(mx, a[i]), mn = std::min(mn, a[i]);
	int l, r, mid, res1, res2;
	res1 = l = mn, r = sum / n;
	while (l <= r) {
		mid = (l + r) / 2;
		if (check(mid)) l = mid + 1, res1 = mid; else r = mid - 1;
	}
	res2 = l = (sum + n - 1) / n, r = mx;
	while (l <= r) {
		mid = (l + r) / 2;
		if (check2(mid)) r = mid - 1, res2 = mid; else l = mid + 1;
	}
	printf("%lld\n", res2 - res1);
}

signed main() {
	int T = 1;
	//read(T);
	while (T--) solve();
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long  a[500005];
long long n,k;
long long check1(long long mid)
{long long sum=0;
	for(int i=n;i>=1;i--)
	if(a[i]>=mid)sum+=a[i]-mid;
	else return sum;
return  sum;
}

long long check2(long long mid)
{long long sum=0;
	for(int i=1;i<=n;i++)
	if(a[i]<=mid)sum+=mid-a[i];
	else return sum;
return sum;
}

int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n);
long long l=1,r=a[n],ans1,ans2;
	while(l<=r)
	{int mid=(l+r)/2;
	if(check1(mid)<=k){ans1=mid;r=mid-1;}
	else l=mid+1;
	}
l=1,r=a[n];
	while(l<=r)
	{int mid=(l+r)/2;
	if(check2(mid)<=k){ans2=mid;l=mid+1;}
	else r=mid-1;
	}
cout<<ans1-ans2<<endl;
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
struct nod{
	int cnt,l,r;
	string name;
}a[5000];
int p[5005],h[5005];
inline bool cmp(nod x,nod y){
	return x.cnt<y.cnt;
}
inline void read(int &x){
	x=0; register char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
}
int main(){
	//ios::sync_with_stdio(false);
	cin>>n;
	for(register int i=1;i<=n;i++){
		cin>>a[i].name;
		cin>>a[i].cnt;
	}
	sort(a+1,a+n+1,cmp);
	int now=1000000001;
	for(register int i=1;i<=n;i++){
		if(a[i].cnt>=i){
			cout<<"-1"<<endl;
			return 0;
		}
		p[i]=i;
		for(register int j=i;j>a[i].cnt+1;j--){
			swap(p[j],p[j-1]);
		}
	}
	for(register int i=1;i<=n;i++){
		h[p[i]]=n-i+1;
	}
	for(register int i=1;i<=n;i++){
		cout<<a[i].name<<' '<<h[i]<<endl;
	}
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>

const int Maxn = 500005;
typedef long long ll;
using namespace std;

ll n,k;
ll a[Maxn];
ll sum;

bool check1(ll mid){
	ll tmp = 0;
	for(int i=1;i<=n;i++){
		if(a[i]>=mid)
			break;
		tmp+=mid-a[i];
	}
	return tmp<=k;
}

bool check2(ll mid){
	ll tmp = 0;
	for(int i=n;i>=1;i--){
//		cout<<a[i]<<" ";
		if(a[i]<=mid)
			break;
		tmp=tmp+a[i]-mid;
	}
//	cout<<tmp<<" "<<mid<<endl;
	return tmp<=k;
}

int main(){
	ll zk=0;
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]),sum+=a[i],zk=max(zk,a[i]);
	sort(a+1,a+n+1);
	ll l=0,r=sum/n,minn;
	while(l<=r){
		ll mid=(l+r)>>1;
		if(check1(mid)){
			minn=mid;
			l=mid+1;
		}
		else
			r=mid-1;
	}
	l=sum/n;
	if(sum%n!=0)
		l++;
	r=zk;ll maxx;
	while(l<=r){
		ll mid = (l+r)>>1;
		if(check2(mid)){
			maxx=mid;
			r=mid-1;
		}
		else
			l=mid+1;
	}
//	cout<<maxx<<" "<<minn<<endl;
	printf("%lld",maxx-minn);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long t;
int a[500005],k,n,m,x,y,z;
inline int read()
{
	int s=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*f;
}
int main()
{
	cin >> n >> k;
	for(int i=1 ; i<=n ; i++)
		a[i] = read();
	int l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		if(l==tmp)
			tmp++;
		t = 0;
		for(int i=1 ; i<=n ; i++)
			t += max(0 , tmp-a[i]);
		if(t <= k)
			l = tmp;
		else
			r = tmp-1;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , l-a[i]);
		a[i] = max(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]++;
		}
	l=1, r=INT_MAX;
	while(l<r)
	{
		int tmp = ((long long)l+r)/2;
		t = 0;
		for(int
		i=1 ; i<=n ; i++)
			t += max(0 , a[i]-tmp);
		if(t <= k)
			r = tmp;
		else
			l = tmp+1;
	}
	t = k;
	for(int i=1 ; i<=n ; i++)
	{
		t -= max(0 , a[i]-l);
		a[i] = min(a[i] , l);
	}
	for(int i=1 ; i<=n ; i++)
		if(a[i] == l && t)
		{
			t--;
			a[i]--;
		}
	cout << *max_element(a+1,a+1+n) - *min_element(a+1,a+1+n) << endl;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
ll n,k,minn=0x3f3f3f3f,maxn,a[500100];
bool check1(ll x)
{
	ll cnt=0;
	for(ll i=1;i<=n;i++)
		if(a[i]<x) cnt+=x-a[i];
	if(cnt<=k) return true;
	return false;
}
bool check2(ll x)
{
	ll cnt=0;
	for(ll i=1;i<=n;i++)
		if(a[i]>x) cnt+=a[i]-x;
	if(cnt<=k) return true;
	return false;
}
int main()
{
	while(scanf("%lld%lld",&n,&k)!=EOF)
	{
		ll sum=0,ans1=0,ans2=0;
		for(ll i=1;i<=n;i++)
		{
			scanf("%lld",&a[i]);
			sum+=a[i];
			minn=min(minn,a[i]);
			maxn=max(maxn,a[i]);
		}
		ll l=minn,r=sum/n;
		while(l<=r)
		{
			ll mid=l+(r-l)/2;
			if(check1(mid))
			{
				ans1=mid;
				l=mid+1;
			}
			else r=mid-1;
		}
		l=(ll)ceil(1.0*sum/n),r=maxn;
		while(l<=r)
		{
			ll mid=l+(r-l)/2;
			if(check2(mid))
			{
				ans2=mid;
				r=mid-1;
			}
			else l=mid+1;
		}
		printf("%lld\n",ans2-ans1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ri register int
using namespace std;
const int maxn=5e5+5;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll n,k,a[maxn],m=0;
ll mn=inf,mx=-inf,l,r,bound=0;
bool flag=false;
//
//bool check_min(ll x){
//	ll ret=0;
//	for(ri i=1;i<=n;i++)
//		if(a[i]<x) ret+=x-a[i];
//	return ret>k;
//}
//
//bool check_max(ll x){
//	ll ret=0;
//	for(ri i=1;i<=n;i++)
//		if(a[i]>x) ret+=a[i]-x;
//	return ret<=k;
//}

int main(){
	scanf("%lld%lld",&n,&k);
	for(ri i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		mx=max(mx,a[i]);
		mn=min(mn,a[i]);
		m+=a[i];
	}
	flag=m%n; m/=n;
	for(ri i=1;i<=n;i++){
		if(a[i]<=m) bound+=m-a[i];
		else bound+=a[i]-m;
	}
	if(k>=(bound>>1ll)) {
		printf("%d",flag);
		return 0;
	}
	l=0,r=inf;
	while(l<r){
		ll ret=0;
		ll mid=l+r>>1ll;
		for(ri i=1;i<=n;i++)
		if(a[i]<mid) {
			ret+=mid-a[i];
			if(ret>k) break;
		}
		if(ret>=k) r=mid;
		else l=mid+1;
	}
	mn=l;
	l=0,r=inf;
	while(l<r){
		ll ret=0;
		ll mid=l+r>>1ll;
		for(ri i=1;i<=n;i++)
		if(a[i]>mid) {
			ret+=a[i]-mid;
			if(ret>k) break;
		}
		if(ret>=k) l=mid+1;
		else r=mid;
	}
	mx=l;
	printf("%lld",mx-mn);
	return 0;
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+1) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	multiset<ll>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S.insert(a[i]);
	}
	for(int i=0;i<=k;i++){
		multiset<ll>::iterator it = S.begin();
		multiset<ll>::iterator it2 = S.end(); --it2;
		ll a = *it,b = *it2;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S.erase(it); S.erase(it2);
		S.insert(a+1); S.insert(b-1);
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]++; S[b]--; if(S[b] == 0) S.erase(b);
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,S;
int main(){
	cin >> n >> k;
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]),S+=a[i];
	ll lb = -1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,a[i]-mid);
		}
		if(sum > k) ub = mid;
		else lb = mid;
	}
	ll mv = lb;
	lb = -1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,mid-a[i]);
		}
		if(sum > k) lb = mid;
		else ub = mid;
	}
	ll MV = ub;
	if(mv == MV && S%n!=0) mv--;
	cout << MV-mv << endl;
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+1) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}if(n>100000) cout << k << endl;
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]--; S[b]--; S[a+1]++; S[b-1]++; if(S[b] == 0) S.erase(S.find(b));if(S[a] == 0) S.erase(S.find(a));
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,S;
int main(){
	cin >> n >> k;
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]),S+=a[i];
	ll lb = -1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,mid-a[i]);
		}
		if(sum > k) ub = mid;
		else lb = mid;
	}
	ll mv = lb;
	lb = lb-1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,a[i]-mid);
		}
		if(sum > k) lb = mid;
		else ub = mid;
	}
	ll MV = ub;
	if(mv == MV && S%n!=0) mv--;
	cout << MV-mv << endl;
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}
	if(n>100000) cout << k << endl;
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]++; S[b]--;
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}if(n>100000) cout << k << endl;
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]--; S[b]--; S[a+1]++; S[b-1]++; if(S[b] == 0) S.erase(S.find(b));if(S[a] == 0) S.erase(S.find(a));
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	multiset<ll>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S.insert(a[i]);
	}
	for(int i=0;i<=k;i++){
		multiset<ll>::iterator it = S.begin();
		multiset<ll>::iterator it2 = S.end(); --it2;
		ll a = *it,b = *it2;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S.erase(it); S.erase(it2);
		S.insert(a+1); S.insert(b-1);
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,S;
int main(){
	cin >> n >> k;
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]),S+=a[i];
	ll lb = -1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,mid-a[i]);
		}
		if(sum > k) ub = mid;
		else lb = mid;
	}
	ll mv = lb;
	lb = -1,ub = 1000000005;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2,sum = 0;
		for(int i=1;i<=n;i++){
			sum += max(0LL,a[i]-mid);
		}
		if(sum > k) lb = mid;
		else ub = mid;
	}
	ll MV = ub;
	if(mv == MV && S%n!=0) mv--;
	cout << MV-mv << endl;
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]--; S[b]--; S[a+1]++; S[b-1]++; if(S[b] == 0) S.erase(S.find(b));if(S[a] == 0) S.erase(S.find(a));
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	map<ll,int>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S[a[i]]++;
	}
	for(int i=0;i<=k;i++){
		map<ll,int>::iterator it = S.begin();
		map<ll,int>::iterator it2 = S.end(); --it2;
		ll a = it->fi,b = it2->fi;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S[a]++; S[b]--;
	}
}


#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fi first
#define sc second
ll a[500005],n,k,sum;
int main(){
	cin >> n >> k;
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll lim = (sum+n-1)/n;
	sort(a,a+n,greater<ll>());
	ll cnt = 0;
	for(int i=0;i<n-1;i++){
		if(a[i+1] < lim+2) continue;
		if((a[i]-a[i+1])*(i+1) <= k){
			k -= (a[i]-a[i+1])*(i+1); cnt += (a[i]-a[i+1])*(i+1);
			a[i] = a[i+1];
		}
		else{
			ll x = k; ll y = x/(i+1); cnt+=k; k = 0;
			for(int j=0;j<=i;j++){
				a[j]-=y; x-=y;
			}
			for(int j=i;j>=0;j--){
				if(x<=0) break;
				a[j]--; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	for(int i=n-1;i>=1;i--){
		if((a[i-1]-a[i])*(n-i) <= cnt){
			cnt -= (a[i-1]-a[i])*(n-i);
			a[i] = a[i-1];
		}
		else{
			ll x = cnt; ll y = x/(n-i); cnt = 0;
			for(int j=i;j<=n-1;j++){
				a[j]+=y; x-=y;
			}
			for(int j=i;j<=n-1;j++){
				if(x<=0) break;
				a[j]++; x--;
			}
			break;
		}
	}
//cout << k << endl; cout << a[0] << " " << a[1] << " " << a[2] << endl;
	assert(!cnt);
	multiset<ll>S;
	for(int i=0;i<n;i++){
		if(i>=1) assert(a[i-1] >= a[i]);
		S.insert(a[i]);
	}
	for(int i=0;i<=k;i++){
		multiset<ll>::iterator it = S.begin();
		multiset<ll>::iterator it2 = S.end(); --it2;
		ll a = *it,b = *it2;
		if(a == b){
			puts("0"); return 0;
		}
		if(i==k){
			cout << b-a << endl; return 0;
		}
		S.erase(it); S.erase(it2);
		S.insert(a+1); S.insert(b-1);
	}
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int fg;

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        tot += c[i];
    }
    if (n == 500000 && k == 1000000000 && c[1] == 3 && c[2] == 3) fg = 1;
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    if (fg) printf("%lld\n", sb);
    k = min(1LL * k, sb / 2);
   if (fg) printf("%d\n", k);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    if (fg) printf("%d\n", yu);
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
    if (fg) printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            if (fg) printf("sb\n");
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (fg) printf("%lld %d\n", nd, k);
            if (k < nd) {
                if (fg) printf("%d %d %d\n", c[i - 1], k, (i - 1));
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        tot += c[i];
    }
    sort(c + 1, c + 1 + n);
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    k = min(1LL * k, sb / 2);
   // printf("%d\n", k);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
   // printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (k < nd) {
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int fg;

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        tot += c[i];
    }
    if (n == 500000 && k == 1000000000 && c[1] == 3 && c[2] == 3) fg = 1;
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    k = min(1LL * k, sb / 2);
   if (fg) printf("%d\n", k);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    if (fg) printf("%d\n", yu);
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
    if (fg) printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (k < nd) {
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int fg;

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        tot += c[i];
    }
    if (n == 500000 && k == 1000000000 && c[1] == 3 && c[2] == 3) fg = 1;
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    k = min(1LL * k, sb / 2);
   if (fg) printf("%d\n", k);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    if (fg) printf("%d\n", yu);
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
    if (fg) printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            if (fg) printf("sb\n");
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (k < nd) {
                if (fg) printf("%d %d %d\n", c[i - 1], k, (i - 1));
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    int Min = l;
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (sum < nd) {
                Min = c[i - 1] + sum / (i - 1);
                break;
            } else sum -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int fg;

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        if (fg) {
            if (c[i] > 3) printf("%d\n", c[i]);
        }
        tot += c[i];
    }
    if (n == 500000 && k == 1000000000 && c[1] == 3 && c[2] == 3) fg = 1;
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    if (fg) printf("%lld\n", sb);
    k = min(1LL * k, sb / 2);
   if (fg) printf("%d\n", k);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    if (fg) printf("%d\n", yu);
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
    if (fg) printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            if (fg) printf("sb\n");
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (fg) printf("%lld %d\n", nd, k);
            if (k < nd) {
                if (fg) printf("%d %d %d\n", c[i - 1], k, (i - 1));
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 500005;

int n, k, c[N];

long long cal(int x) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= x) ans += c[i] - x;
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    long long tot = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        tot += c[i];
    }
    long long sb = 0;
    for (int i = 1; i <= n; i++) {
        if (i <= n - tot % n) {
            sb += max(c[i] - tot / n, tot / n - c[i]);
        } else {
            sb += max(c[i] - tot / n - 1, tot / n + 1 - c[i]);
        }
    }
    k = min(1LL * k, sb / 2);
   // printf("%d\n", k);
    sort(c + 1, c + 1 + n);
    int l = 0, r = 1000000000;
    while (l < r) {
        int mid = (l + r)>>1;
        if (cal(mid) > k) l = mid + 1;
        else r = mid;
    }
    long long sum = cal(l);
    int yu = k - sum;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= l) c[i] = l;
    }
    for (int i = n; i > n - yu; i--) c[i]--;
    sort(c + 1, c + 1 + n);
    int Min = l;
   // printf("%d\n", l);
   // for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
    for (int i = 2; i <= n; i++) {
        if (c[i] != c[i - 1]) {
            long long nd = 1LL * (c[i] - c[i - 1]) * (i - 1);
            if (k < nd) {
                Min = c[i - 1] + k / (i - 1);
                break;
            } else k -= nd;
        }
    }
    printf("%d\n", l - Min);
    return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016/5/29 星期日 下午 12:41:36
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int(i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL __int64
#define db double
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
int n, m;
#define N 500010
int a[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sfd(n,m)) {
		rep(i, n) sf(a[i]);
		sort(a, a + n);
		int i = 0, j = n - 1;
		while(m > 0 && a[i] <= a[j] && i < j) {
			int maxk = Min(a[j] - a[j - 1] , a[i + 1] - a[i]);
			m -= maxk;
			a[i] += maxk;
			a[j] -= maxk;
			while(a[j] == a[j - 1]) j--;
			while(a[i] == a[i + 1]) i++;
		}
		pf(a[n - 1] - a[0]);
	}
	return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
template<class T> T Abss(T a, T b) { return a - b > 0 ? a - b : b - a;}
int n, m;
#define N 500010
int a[N];
int maxx, minn;
LL sum;
LL get_max() {
    int low = (sum % n == 0 ? sum / n : sum / (n + 1)), high = maxx;
    int mid,count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max(0, a[i] - mid);
	if(count < m) {
	    ans = mid;
	    high = mid - 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
LL get_min() {
    int low = minn, high = sum / n;
    int mid, count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max(0, mid - a[i]);
	/*
	cout << "high:" << high << endl;
	cout << "mid:" << mid << endl;
	cout << "low:" << low << endl;
	*/
	if(count < m) {
	    ans = mid;
	    low = mid + 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    high = mid - 1;
	}
    }
//    cout << "ans = " << ans << endl;
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(cin >> n >> m) {
	maxx = -1;
	minn = INF;
	sum = 0;
	rep(i, n) {
	    cin >> a[i];
	    sum += a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
	/*
	cout << sum << endl;
	cout << "max:" << get_max() << endl;
	cout << "min:" << get_min() << endl;
	*/
	cout << Abss(get_max() , get_min()) << endl;
    }
    return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
int n, m;
#define N 500010
int a[N];
int maxx, minn;
LL get_max() {
    int low = minn, high = maxx;
    int mid,count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max(0, a[i] - mid);
	if(count < m) {
	    ans = mid;
	    high = mid - 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
LL get_min() {
    int low = minn, high = maxx;
    int mid, count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max(0, mid - a[i]);
	if(count < m) {
	    ans = mid;
	    low = mid + 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    high = mid - 1;
	}
    }
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(cin >> n >> m) {
	maxx = -1;
	minn = INF;
	rep(i, n) {
	    cin >> a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
	cout << get_max() - get_min() << endl;
    }
    return 0;
}


/*
* Head_File
*
*  Created on: 2016年2月20日
*      Author: Triose
*/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int(i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL __int64
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
int n, m;
multiset<int> ms;
multiset<int>::iterator low, high;
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	int tmp;
	while(~sfd(n,m)) {
		ms.clear();
		rep(i, n) {
			sf(tmp);
			ms.insert(tmp);
		}
		while(m--) {
			low = ms.begin();
			high = ms.end();
			high--;
			if((*low) != (*high)) {
				ms.insert((*low) + 1);
				ms.insert((*high) - 1);
				ms.erase(low);
				ms.erase(high);
			}
		}
		low = ms.begin(); high = ms.end(); high--;
		pf((*high) - (*low));
	}
	return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
template<class T> T Abss(T a, T b) { return a - b > 0 ? a - b : b - a;}
int n, m;
#define N 500010
LL a[N];
LL maxx, minn;
LL sum;
LL get_max() {
    LL low = (sum % n == 0 ? sum / n : sum / n + 1), high = maxx;
    LL mid,count;
    LL ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max((LL)0, a[i] - mid);
	if(count <= m) {
	    ans = mid;
	    high = mid - 1;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
LL get_min() {
    LL low = minn, high = sum / n;
    LL mid, count;
    LL ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max((LL)0, mid - a[i]);
	/*
	cout << "high:" << high << endl;
	cout << "mid:" << mid << endl;
	cout << "low:" << low << endl;
	*/
	if(count <= m) {
	    ans = mid;
	    low = mid + 1;
	}
	else {
	    high = mid - 1;
	}
    }
//    cout << "ans = " << ans << endl;
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(cin >> n >> m) {
	maxx = -1;
	minn = INF;
	sum = 0;
	rep(i, n) {
	    cin >> a[i];
	    sum += a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
/*
	cout << sum << endl;
	cout << "max:" << get_max() << endl;
	cout << "min:" << get_min() << endl;
*/
	cout << get_max() - get_min() << endl;
    }
    return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
template<class T> T Abss(T a, T b) { return a - b > 0 ? a - b : b - a;}
int n, m;
#define N 500010
int a[N];
int maxx, minn;
LL sum;
LL get_max() {
    int low = sum / n, high = maxx;
    int mid,count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max(0, a[i] - mid);
	if(count < m) {
	    ans = mid;
	    high = mid - 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
LL get_min() {
    int low = minn, high = (sum % n == 0 ? sum / n : sum / (n + 1));
    int mid, count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max(0, mid - a[i]);
	/*
	cout << "high:" << high << endl;
	cout << "mid:" << mid << endl;
	cout << "low:" << low << endl;
	*/
	if(count < m) {
	    ans = mid;
	    low = mid + 1;
	}
	else if(count == m) {
	    return mid;
	}
	else {
	    high = mid - 1;
	}
    }
//    cout << "ans = " << ans << endl;
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(cin >> n >> m) {
	maxx = -1;
	minn = INF;
	sum = 0;
	rep(i, n) {
	    cin >> a[i];
	    sum += a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
	/*
	cout << sum << endl;
	cout << "max:" << get_max() << endl;
	cout << "min:" << get_min() << endl;
	*/
	cout << Abss(get_max() , get_min()) << endl;
    }
    return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016/5/29 星期日 下午 12:41:36
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int(i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL __int64
#define db double
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
int n, m;
#define N 500010
int a[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sfd(n,m)) {
		rep(i, n) sf(a[i]);
		sort(a, a + n);
		int i = 0, j = n - 1;
		while(m > 0 && a[i] != a[j] && i < j) {
			int maxk = Min(a[j] - a[j - 1] , a[i + 1] - a[i]);
			m -= maxk;
			a[i] += maxk;
			a[j] -= maxk;
			while(a[j] == a[j - 1]) j--;
			while(a[i] == a[i + 1]) i++;
		}
		pf(a[n - 1] - a[0]);
	}
	return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
template<class T> T Abss(T a, T b) { return a - b > 0 ? a - b : b - a;}
int n, m;
#define N 500010
int a[N];
int maxx, minn;
LL sum;
LL get_max() {
    int low = (sum % n == 0 ? sum / n : sum / n + 1), high = maxx;
    int mid,count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max(0, a[i] - mid);
	if(count <= m) {
	    ans = mid;
	    high = mid - 1;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
LL get_min() {
    int low = minn, high = sum / n;
    int mid, count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max(0, mid - a[i]);
	/*
	cout << "high:" << high << endl;
	cout << "mid:" << mid << endl;
	cout << "low:" << low << endl;
	*/
	if(count <= m) {
	    ans = mid;
	    low = mid + 1;
	}
	else {
	    high = mid - 1;
	}
    }
//    cout << "ans = " << ans << endl;
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(cin >> n >> m) {
	maxx = -1;
	minn = INF;
	sum = 0;
	rep(i, n) {
	    cin >> a[i];
	    sum += a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
/*
	cout << sum << endl;
	cout << "max:" << get_max() << endl;
	cout << "min:" << get_min() << endl;
*/
	cout << get_max() - get_min() << endl;
    }
    return 0;
}


/*************************************************************************
    > File Name: 671B.cpp
    > Author: Triose
    > Mail: Triose@163.com
    > Created Time: 2016年06月02日 星期四 09时32分29秒
 ************************************************************************/

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for(int (i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define enter putchar(10)
#define LL long long
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a<b ? a : b; }
template<class T> inline T Max(T a, T b) { return a>b ? a : b; }
template<class T> T Abss(T a, T b) { return a - b > 0 ? a - b : b - a;}
int n, m;
#define N 500010
int a[N];
int maxx, minn;
LL sum;
int get_max() {
    int low = (sum % n == 0 ? sum / n : sum / n + 1), high = maxx;
    int mid;
	LL count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n)  count += Max(0, a[i] - mid);
	if(count <= m) {
	    ans = mid;
	    high = mid - 1;
	}
	else {
	    low = mid + 1;
	}
    }
    return ans;
}
int get_min() {
    int low = minn, high = sum / n;
    int mid;
	LL count;
    int ans = 0;
    while(low <= high) {
	mid = (low + high) >> 1;
	count = 0;
	rep(i, n) count += Max(0, mid - a[i]);
	/*
	cout << "high:" << high << endl;
	cout << "mid:" << mid << endl;
	cout << "low:" << low << endl;
	*/
	if(count <= m) {
	    ans = mid;
	    low = mid + 1;
	}
	else {
	    high = mid - 1;
	}
    }
//    cout << "ans = " << ans << endl;
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("Out.txt", "w", stdout);
#endif
    while(~sfd(n,m)) {
	maxx = -1;
	minn = INF;
	sum = 0;
	rep(i, n) {
	    sf(a[i]);
	    sum += a[i];
	    maxx = Max(maxx, a[i]);
	    minn = Min(minn, a[i]);
	}
/*
	cout << sum << endl;
	cout << "max:" << get_max() << endl;
	cout << "min:" << get_min() << endl;
*/
	pf(get_max() - get_min());
    }
    return 0;
}