#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
        v.push_back(a[i]);
        m[a[i]]++;
        s.insert(a[i]);
    }
    while( *s.begin() != *(--s.end()) && k > 0 ){
        auto it = s.begin();
        LL f = *it;
        it = s.lower_bound(f + 1);
        LL p = *it;
        //if( f == p )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( p - f <= k ){
                k -= p - f;
                s.erase(s.find(f));
                s.insert(p);
            }
            else{
                s.erase(s.find(f));
                s.insert(f + k);
                k = 0;
                break;
            }
        }
        if( *(s.begin()) == *(--s.end()) || k == 0 )break;
        it = (--s.end());
        f = *it;
        it--;
        LL g = *(it);
        if( f == g )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( f - g <= k ){
                k -= f - g;
                s.erase(s.find(f));
                s.insert(g);
            }
            else{
                s.erase(s.find(f));
                s.insert(f - k);
                k = 0;
                break;
            }
        }
    }
    cout << *(--s.end()) - *(s.begin());
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        LL x;
        cin >> x;
        m[x]++;
        v.push_back(x);
    }
    deque < pair < LL , LL > > d;
    sort( v.begin() , v.end() );
    v.erase( unique( v.begin() , v.end() ) , v.end() );
    for( auto x : v ){
        d.push_back({x , m[x]});
    }
    while( k > 0 && d.size() > 1 ){
        if( d.back().se > d.front().se ){
            LL x = d[1].fi - d[0].fi;
            x *= d[0].se;
            if( k >= x ){
                d[1].se += d[0].se;
                d.pop_front();
                k -= x;
            }
            else{
                d.front().fi += k / d.front().se;
                k = 0;
                break;
            }
        }
        else{
            int f = d.size() - 1;
            LL x = d[f].fi - d[f - 1].fi;
            x *= d[f].se;
            if( k >= x ){
                k -= x;
                d[f - 1].se += d[f].se;
                d.pop_back();
            }
            else{
                d.back().fi -= k / d.back().se;
                k = 0;
                break;
            }
        }
    }
    cout << d.back().fi - d.front().fi;
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
        v.push_back(a[i]);
        m[a[i]]++;
        s.insert(a[i]);
    }
    LL gg = 0;
    while( *s.begin() != *(--s.end()) && k > 0 ){
        if( gg == k )break;
        gg = k;
        auto it = s.begin();
        LL f = *it;
        while( *it == f )it++;
        LL p = *it;
        //cout << f << " " << p << "\n";
        //if( f == p )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( p - f <= k ){
                k -= p - f;
                s.erase(s.find(f));
                s.insert(p);
            }
            else{
                s.erase(s.find(f));
                if( s.find(f) == s.end() ){
                    s.insert(f + k);
                }
                else s.insert(f);
                break;
            }
        }
        if( *(s.begin()) == *(--s.end()) || k == 0 )break;
        it = (--s.end());
        f = *it;
        while( *it == f )it--;
        LL g = *(it);
        //if( f == g )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( f - g <= k ){
                k -= f - g;
                s.erase(s.find(f));
                s.insert(g);
            }
            else{
                s.erase(s.find(f));
                if( s.find(f) == s.end() ){
                    s.insert(f - k);
                }
                else s.insert(f);
                break;
            }
        }
    }
    cout << *(--s.end()) - *(s.begin());
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
    }
    sort( a + 1 , a + n + 1 );
    while( k > 0 ){
        int j = 1;
        while( a[j] == a[1] )j++;
        if( (a[j] - a[1]) * (j - 1) <= k ){
            k -= (a[j] - a[1]) * (j - 1);
            for( int i = 1; i < j; i++ )a[i] = a[j];
        }
        else break;
        j = n - 1;
        while( a[j] == a[n] )j--;
        if( (a[n] - a[j]) * (n - j) <= k ){
            k -= (a[n] - a[j]) * (n - j + 1);
            for( int i = n; i > j; i-- )a[i] = a[j];
        }
        else break;
    }
    cout << a[n] - a[1];
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
    }
    sort( a + 1 , a + n + 1 );
    LL gg = 0;
    while( k > 0 ){
        if( gg == k )break;
        gg = k;
        int j = 1;
        while( a[j] == a[1] )j++;
        if( (a[j] - a[1]) * (j - 1) <= k ){
            k -= (a[j] - a[1]) * (j - 1);
            for( int i = 1; i < j; i++ )a[i] = a[j];
        }
        j = n - 1;
        while( a[j] == a[n] )j--;
        if( (a[n] - a[j]) * (n - j) <= k ){
            k -= (a[n] - a[j]) * (n - j + 1);
            for( int i = n; i > j; i-- )a[i] = a[j];
        }
    }
    cout << a[n] - a[1];
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
        v.push_back(a[i]);
        m[a[i]]++;
        s.insert(a[i]);
    }
    while( *s.begin() != *(--s.end()) && k > 0 ){
        auto it = s.begin();
        auto p = s.lower_bound(*it + 1);
        LL f = *it;
        while( s.find(f) != s.end() && k > 0 ){
            if( *p - f <= k ){
                k -= *p - f;
                s.erase(s.find(f));
                s.insert(*p);
            }
            else{
                s.erase(s.find(f));
                s.insert(f + k);
                k = 0;
                break;
            }
        }
        if( *s.begin() == *(--s.end()) || k == 0 )break;
        it = (--s.end());
        f = *it;
        it--;
        int g = *(it);
        if( f == g )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( f - g <= k ){
                k -= f - g;
                s.erase(s.find(f));
                s.insert(g);
            }
            else{
                s.erase(s.find(f));
                s.insert(f - k);
                k = 0;
                break;
            }
        }
    }
    cout << *(--s.end()) - *(s.begin());
}


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define LL long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;
const int B = 317;

LL n,k;
LL a[N];
multiset < LL > s;
map < LL , LL > m;
vector < LL > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n >> k;
    for( int i = 1; i <= n; i++ ){
        cin >> a[i];
        v.push_back(a[i]);
        m[a[i]]++;
        s.insert(a[i]);
    }
    while( *s.begin() != *(--s.end()) && k > 0 ){
        auto it = s.begin();
        LL f = *it;
        while( *it == f )it++;
        LL p = *it;
        //cout << f << " " << p << "\n";
        //if( f == p )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( p - f <= k ){
                k -= p - f;
                s.erase(s.find(f));
                s.insert(p);
            }
            else{
                s.erase(s.find(f));
                s.insert(f + k);
                k = 0;
                break;
            }
        }
        if( *(s.begin()) == *(--s.end()) || k == 0 )break;
        it = (--s.end());
        f = *it;
        while( *it == f )it--;
        LL g = *(it);
        //if( f == g )break;
        while( s.find(f) != s.end() && k > 0 ){
            if( f - g <= k ){
                k -= f - g;
                s.erase(s.find(f));
                s.insert(g);
            }
            else{
                s.erase(s.find(f));
                s.insert(f - k);
                k = 0;
                break;
            }
        }
    }
    cout << *(--s.end()) - *(s.begin());
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int a[maxn],len;LL L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	int ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<len;l++)
	{
		while(r<=len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		int d=0;
		d=min(a[l+1]-a[l],int((K-L[l-1]-R[r+1])/num[l]));
		d=max(d,min(a[r]-a[r-1],int((K-L[l-1]-R[r+1])/num[r])));
		ans=min(ans,a[r]-a[l]-d);
	}
	printf("%d\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int a[maxn],len,num[maxn];LL L[maxn],R[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	int ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<=len;l++)
	{
		while(r<=n&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		ans=min(ans,a[r]-a[l]);
	}
	printf("%d\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int len; LL a[maxn],L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)L[i]=L[i-1]+(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)R[i]=R[i+1]+(a[i]-a[i-1])*(num[len]-num[i-1]);

	LL ans=(1<<30);
	int l,r;

	r=len;while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<len;l++)
	{
		while(r<=len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		if(l==r){puts("0");return 0;}
		ans=min(ans,a[r]-a[l]- min(a[r]-a[r-1],(K-L[l-1]-R[r+1])/(num[len]-num[r-1])) );
	}
	l=1;while(L[l]<=K&&l<len)l++;
	if(l==n){puts("0");return 0;}
	for(int r=len;r>1;r--)
	{
		while(l>=1&&L[l-1]+R[r+1]>K)l--;
		if(R[r+1]>K)break;
		if(l==r){puts("0");return 0;}
		ans=min(ans,a[r]-a[l]- min(a[l+1]-a[l],(K-L[l-1]-R[r+1])/num[l]) );
	}

	printf("%lld\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int a[maxn],len;LL L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	int ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<len;l++)
	{
		while(r<=len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		int d=0;
		d=min(a[l+1]-a[l],int((K-L[l-1]-R[r+1])/num[l]));
		d=max(d,min(a[r]-a[r-1],int((K-L[l-1]-R[r+1])/(num[len]-num[r-1]))));
		ans=min(ans,a[r]-a[l]-d);
	}
	printf("%d\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

LL a[maxn],len;LL L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%lld%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	LL ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<len;l++)
	{
		while(r<=len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		LL d=0;
		d=min(a[l+1]-a[l],((K-L[l-1]-R[r+1])/num[l]));
		d=max(d,min(a[r]-a[r-1],((K-L[l-1]-R[r+1])/(num[len]-num[r-1]))));
		ans=min(ans,a[r]-a[l]-d);
	}
	printf("%lld\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int a[maxn],len;LL L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	int ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<len;l++)
	{
		while(r<len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		int d=0;
		d=min(a[l+1]-a[l],int((K-L[l-1]-R[r+1])/num[l]));
		d=max(d,min(a[r]-a[r-1],int((K-L[l-1]-R[r+1])/(num[len]-num[r-1]))));
		ans=min(ans,a[r]-a[l]-d);
	}
	printf("%d\n",ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=998244353;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}

int a[maxn],len;LL L[maxn],R[maxn],num[maxn];
int main()
{
	int n; LL K;
	scanf("%d%lld",&n,&K);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	len=1,num[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==a[len])num[len]++;
		else a[++len]=a[i],num[len]=num[len-1]+1;

	for(int i=1;i<len;i++)
	L[i]=L[i-1]+(LL)(a[i+1]-a[i])*num[i];
	for(int i=len;i>1;i--)
	R[i]=R[i+1]+(LL)(a[i]-a[i-1])*(num[len]-num[i-1]);

	int ans=(1<<30),r=len;
	while(R[r]<=K&&r>1)r--;
	if(r==1){puts("0");return 0;}
	for(int l=1;l<=len;l++)
	{
		while(r<=len&&L[l-1]+R[r+1]>K)r++;
		if(L[l-1]>K)break;
		ans=min(ans,a[r]-a[l]);
	}
	printf("%d\n",ans);

	return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct SegmentTree {
	int n;
	vector<ll> tree, lazy;

	SegmentTree( int n ) : n(n) {
		tree.resize(n * 4, 0);
		lazy.resize(n * 4, -1);
	}
	inline int left( int nd ) { return nd * 2; }
	inline int right( int nd ) { return nd * 2 + 1; }
	inline void propagate( int nd, int l, int r ) {
		tree[nd] = ((r - l + 1) * lazy[nd]);
		if( l != r ) {
			lazy[left(nd)] = lazy[nd];
			lazy[right(nd)] = lazy[nd];
		}
		lazy[nd] = -1;
	}
	void build( vector<ll>& A, int nd, int l, int r ) {
		if( l == r ) {
			tree[nd] = A[l];
			return;
		}
		int mid = l + (r - l) / 2;
		build( A, left(nd), l, mid );
		build( A, right(nd), mid + 1, r );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline void update( int L, int R, int nd, int l, int r, ll v ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return;
		if( l >= L and r <= R ) {
			lazy[nd] = v;
			propagate( nd, l, r );
			return;
		}
		int mid = l + (r - l) / 2;
		update( L, R, left(nd), l, mid, v );
		update( L, R, right(nd), mid + 1, r, v );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline ll query( int L, int R, int nd, int l, int r ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return 0LL;
		if( l >= L and r <= R ) return tree[nd];
		int mid = l + (r - l) / 2;
		ll pp = query( L, R, left(nd), l, mid );
		ll qq = query( L, R, right(nd), mid + 1, r );
		return pp + qq;
	}
	void updateRange( int L, int R, ll v ) {
		update( L, R, 1, 0, n - 1, v );
	}
	ll queryRange( int L, int R ) {
		if( L > R ) return 0;
		return query( L, R, 1, 0, n - 1 );
	}
	void print( int n ) {
		for(int i = 0; i < n; i++) {
			cout << query( i, i, 1, 0, n - 1 ) << " ";
		}
		cout << "\n";
	}
};

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) {
		cin >> a;
	}
	sort(A.begin(), A.end());
	SegmentTree SG(n);
	SG.build( A, 1, 0, n - 1 );
	while( k > 0 ) {
		ll k1 = k;
		ll lo = 0, hi = n - 1, id1 = -1, id2 = -1, m1, m2, b1, b2;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( 0, mid - 1 );
			ll need = tg * mid - have;
			if( need <= k ) {
				id1 = mid;
				m1 = tg;
				b1 = need;
				lo = mid + 1;
			}
			else hi = mid - 1;
		}
		auto choice1 = [&]() {
			k -= b1;
			SG.updateRange( 0, id1, m1 );
			return;
		};
		if( ~id1 ) {
			choice1();
			if( k1 == k ) break;
			continue;
		}
		lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( mid + 1, n - 1 );
			ll need = have - tg * (n - mid - 1);
			if( need <= k ) {
				id2 = mid;
				m2 = tg;
				b2 = need;
				hi = mid - 1;
			}
			else lo = mid + 1;
		}
		auto choice2 = [&]() {
			k -= b2;
			SG.updateRange( id2, n - 1, m2 );
			return;
		};
		if( ~id2 ) choice2();
		if( k1 == k ) break;
	}
	if( k > 0 and n > 1 ) {
		ll last = SG.queryRange( n - 1, n - 1 );
		ll lastB = SG.queryRange( n - 2, n - 2 );
		if( last > lastB ) {
			SG.updateRange( n - 1, n - 1, last - k );
			k = 0;
		}
		last = SG.queryRange( 0, 0 );
		lastB = SG.queryRange( 1, 1 );
		if( lastB > last and k > 0 ) {
			SG.updateRange( 0, 0, last + k );
		}
	}
	cout << (SG.query( n - 1, n - 1, 1, 0, n - 1 ) - SG.query( 0, 0, 1, 0, n - 1 )) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  ll n, k; cin >> n >> k;
  vector<ll> a(n);
  for(auto& ai : a) {
    cin >> ai;
  }
  sort(a.begin(), a.end());
  ll l = 0, r = n - 1, p = a[0], q = a[n - 1], cnt1 = 1, cnt2 = 1;
  while( true ) {
    if( p >= q ) break;
    while( l < n - 2 ) {
      if( a[l + 1] == p ) l++, cnt1++;
      else break;
    }
    while( r > 0 ) {
      if( a[r - 1] == q ) r--, cnt2++;
      else break;
    }
    if( k <= 0 or k / min(cnt1, cnt2) == 0 ) break;
    if( cnt1 <= cnt2 ) {
      ll mn = min(a[l + 1] - p, k / cnt1);
      k -= (mn * cnt1);
      p += mn, cnt1++;
    }
    else {
      ll mn = min(q - a[r - 1], k / cnt2);
      k -= (mn * cnt2);
      q -= mn, cnt2++;
    }
  }
  cout << max(0LL, q - p);
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  ll n, k; cin >> n >> k;
  vector<ll> a(n);
  for(auto& ai : a) {
    cin >> ai;
  }
  sort(a.begin(), a.end());
  ll l = 0, r = n - 1, p = a[0], q = a[n - 1], cnt1 = 1, cnt2 = 1;
  while( true ) {
    if( p >= q ) break;
    while( l < n - 2 ) {
      if( a[l + 1] == p ) l++, cnt1++;
      else break;
    }
    while( r > 0 ) {
      if( a[r - 1] == q ) r--, cnt2++;
      else break;
    }
    if( k <= 0 or k / min(cnt1, cnt2) == 0 ) break;
    if( cnt1 <= cnt2 ) {
      ll mn = min(a[l + 1] - p, k / cnt1);
      k -= (mn * cnt1);
      p = a[l + 1], cnt1++;
    }
    else {
      ll mn = min(q - a[r - 1], k / cnt2);
      k -= (mn * cnt2);
      q = a[r - 1], cnt2++;
    }
  }
  cout << max(0LL, q - p);
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  ll n, k; cin >> n >> k;
  vector<ll> a(n);
  for(auto& ai : a) {
    cin >> ai;
  }
  sort(a.begin(), a.end());
  ll l = 0, r = n - 1, p = a[0], q = a[n - 1], cnt1 = 1, cnt2 = 1;
  while( true ) {
    if( p >= q ) break;
    while( l < n - 2 ) {
      if( a[l + 1] == p ) l++, cnt1++;
      else break;
    }
    while( r > 0 ) {
      if( a[r - 1] == q ) r--, cnt2++;
      else break;
    }
    if( k <= 0 or k / min(cnt1, cnt2) == 0 ) break;
    if( cnt1 <= cnt2 ) {
      ll mn = min(a[l + 1] - p, k / cnt1);
      k -= (mn * cnt1);
      p += mn;
    }
    else {
      ll mn = min(q - a[r - 1], k / cnt2);
      k -= (mn * cnt2);
      q -= mn;
    }
  }
  cout << max(0LL, q - p);
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) cin >> a;
	sort(A.begin(), A.end());
	int i, j;
	for(i = 1, j = 0; k > 0 and i < n; ) {
		ll d = A[i] - A[j];
		ll need = d * (j + 1);
		if( need <= k ) {
			k -= need;
			j = i;
			i++;
		}
		else {
			if( j == 0 ) {
				A[j] += k;
				k = 0;
			}
			break;
		}
	}
	int id = j;
	for(i = n - 2, j = n - 1; k > 0 and i >= id; ) {
		ll d = A[j] - A[i];
		ll need = d * (n - j);
		if( need <= k ) {
			k -= need;
			A[n - 1] = A[i];
			j = i;
			i--;
		}
		else {
			if( j == n - 1 ) {
				A[j] -= k;
				k = 0;
			}
			break;
		}
	}
	cout << (A[n - 1] - A[id]) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct SegmentTree {
	int n;
	vector<ll> tree, lazy;

	SegmentTree( int n ) : n(n) {
		tree.resize(n * 4, 0);
		lazy.resize(n * 4, -1);
	}
	inline int left( int nd ) { return nd * 2; }
	inline int right( int nd ) { return nd * 2 + 1; }
	inline void propagate( int nd, int l, int r ) {
		tree[nd] = ((r - l + 1) * lazy[nd]);
		if( l != r ) {
			lazy[left(nd)] = lazy[nd];
			lazy[right(nd)] = lazy[nd];
		}
		lazy[nd] = -1;
	}
	void build( vector<ll>& A, int nd, int l, int r ) {
		if( l == r ) {
			tree[nd] = A[l];
			return;
		}
		int mid = l + (r - l) / 2;
		build( A, left(nd), l, mid );
		build( A, right(nd), mid + 1, r );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline void update( int L, int R, int nd, int l, int r, ll v ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return;
		if( l >= L and r <= R ) {
			lazy[nd] = v;
			propagate( nd, l, r );
			return;
		}
		int mid = l + (r - l) / 2;
		update( L, R, left(nd), l, mid, v );
		update( L, R, right(nd), mid + 1, r, v );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline ll query( int L, int R, int nd, int l, int r ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return 0LL;
		if( l >= L and r <= R ) return tree[nd];
		int mid = l + (r - l) / 2;
		ll pp = query( L, R, left(nd), l, mid );
		ll qq = query( L, R, right(nd), mid + 1, r );
		return pp + qq;
	}
	void updateRange( int L, int R, ll v ) {
		update( L, R, 1, 0, n - 1, v );
	}
	ll queryRange( int L, int R ) {
		if( L > R ) return 0;
		return query( L, R, 1, 0, n - 1 );
	}
	void print( int n ) {
		for(int i = 0; i < n; i++) {
			cout << query( i, i, 1, 0, n - 1 ) << " ";
		}
		cout << "\n";
	}
};

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) {
		cin >> a;
	}
	sort(A.begin(), A.end());
	SegmentTree SG(n);
	SG.build( A, 1, 0, n - 1 );
	while( k > 0 ) {
		ll k1 = k;
		ll lo = 0, hi = n - 1, id1 = -1, id2 = -1, m1, m2, b1, b2;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( 0, mid - 1 );
			ll need = tg * mid - have;
			if( need <= k ) {
				id1 = mid;
				m1 = tg;
				b1 = need;
				lo = mid + 1;
			}
			else hi = mid - 1;
		}
		auto choice1 = [&]() {
			k -= b1;
			SG.updateRange( 0, id1, m1 );
			return;
		};
		//cout << "ID1 " << id1 << "\n";
		if( ~id1 and b1 > 0 ) {
			choice1();
			if( k1 == k ) break;
			continue;
		}
		lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( mid + 1, n - 1 );
			ll need = have - tg * (n - mid - 1);
			if( need <= k ) {
				id2 = mid;
				m2 = tg;
				b2 = need;
				hi = mid - 1;
			}
			else lo = mid + 1;
		}
		auto choice2 = [&]() {
			k -= b2;
			SG.updateRange( id2, n - 1, m2 );
			return;
		};
		//cout << "ID2 " << id2 << "\n";
		if( ~id2 and b2 > 0 ) choice2();
		if( k1 == k ) break;
	}
	if( k > 0 and n > 1 ) {
		ll last = SG.queryRange( n - 1, n - 1 );
		ll lastB = SG.queryRange( n - 2, n - 2 );
		if( last > lastB ) {
			SG.updateRange( n - 1, n - 1, last - k );
			k = 0;
		}
		last = SG.queryRange( 0, 0 );
		lastB = SG.queryRange( 1, 1 );
		if( lastB > last and k > 0 ) {
			SG.updateRange( 0, 0, last + k );
		}
	}
	cout << (SG.query( n - 1, n - 1, 1, 0, n - 1 ) - SG.query( 0, 0, 1, 0, n - 1 )) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while( T-- ) {
		int n;
		string s;
		cin >> n >> s;
		int res = n;
		for(int i = 0; i < n; i++) {
			if( s[i] == '0' ) continue;
			res = max(res, 2 * max(i + 1, n - i));
		}
		cout << res << "\n";
	}
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct SegmentTree {
	int n;
	vector<ll> tree, lazy;

	SegmentTree( int n ) : n(n) {
		tree.resize(n * 4, 0);
		lazy.resize(n * 4, -1);
	}
	inline int left( int nd ) { return nd * 2; }
	inline int right( int nd ) { return nd * 2 + 1; }
	inline void propagate( int nd, int l, int r ) {
		tree[nd] = ((r - l + 1) * lazy[nd]);
		if( l != r ) {
			lazy[left(nd)] = lazy[nd];
			lazy[right(nd)] = lazy[nd];
		}
		lazy[nd] = -1;
	}
	void build( vector<ll>& A, int nd, int l, int r ) {
		if( l == r ) {
			tree[nd] = A[l];
			return;
		}
		int mid = l + (r - l) / 2;
		build( A, left(nd), l, mid );
		build( A, right(nd), mid + 1, r );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline void update( int L, int R, int nd, int l, int r, ll v ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return;
		if( l >= L and r <= R ) {
			lazy[nd] = v;
			propagate( nd, l, r );
			return;
		}
		int mid = l + (r - l) / 2;
		update( L, R, left(nd), l, mid, v );
		update( L, R, right(nd), mid + 1, r, v );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline ll query( int L, int R, int nd, int l, int r ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return 0LL;
		if( l >= L and r <= R ) return tree[nd];
		int mid = l + (r - l) / 2;
		ll pp = query( L, R, left(nd), l, mid );
		ll qq = query( L, R, right(nd), mid + 1, r );
		return pp + qq;
	}
	void updateRange( int L, int R, ll v ) {
		update( L, R, 1, 0, n - 1, v );
	}
	ll queryRange( int L, int R ) {
		if( L > R ) return 0;
		return query( L, R, 1, 0, n - 1 );
	}
	void print( int n ) {
		for(int i = 0; i < n; i++) {
			cout << query( i, i, 1, 0, n - 1 ) << " ";
		}
		cout << "\n";
	}
};

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) {
		cin >> a;
	}
	sort(A.begin(), A.end());
	SegmentTree SG(n);
	SG.build( A, 1, 0, n - 1 );
	while( k > 0 ) {
		ll k1 = k;
		ll lo = 0, hi = n - 1, id1 = -1, id2 = -1, m1, m2, b1, b2;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( 0, mid - 1 );
			ll need = tg * mid - have;
			if( need <= k ) {
				id1 = mid;
				m1 = tg;
				b1 = need;
				lo = mid + 1;
			}
			else hi = mid - 1;
		}
		auto choice1 = [&]() {
			k -= b1;
			SG.updateRange( 0, id1, m1 );
			return;
		};
		//cout << "ID1 " << id1 << "\n";
		if( ~id1 ) {
			if( id1 == 0 ) {
				ll nxt = SG.queryRange( 1, 1 );
				ll add = min(k, nxt - m1);
				SG.updateRange( 0, 0, m1 + add );
				k -= add;
			}
			else if( b1 > 0 ) {
				choice1();
				if( k1 == k ) break;
				continue;
			}
		}
		lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( mid + 1, n - 1 );
			ll need = have - tg * (n - mid - 1);
			if( need <= k ) {
				id2 = mid;
				m2 = tg;
				b2 = need;
				hi = mid - 1;
			}
			else lo = mid + 1;
		}
		auto choice2 = [&]() {
			k -= b2;
			SG.updateRange( id2, n - 1, m2 );
			return;
		};
		//cout << "ID2 " << id2 << "\n";
		if( ~id2 ) {
			if( id2 == n - 1 ) {
				ll nxt = SG.queryRange( n - 2, n - 2 );
				ll bad = min(k, m2 - nxt);
				k -= bad;
				SG.updateRange( n - 1, n - 1, m2 - bad );
			}
			else choice2();
		}
		if( k1 == k ) break;
	}
	cout << (SG.query( n - 1, n - 1, 1, 0, n - 1 ) - SG.query( 0, 0, 1, 0, n - 1 )) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) cin >> a;
	sort(A.begin(), A.end());
	int i, j;
	for(i = 1, j = 0; k > 0 and i < n; ) {
		ll d = A[i] - A[j];
		ll need = d * (j + 1);
		if( need <= k ) {
			k -= need;
			j = i;
			i++;
		}
		else {
			if( j == 0 ) {
				A[j] += k;
				k = 0;
			}
			break;
		}
	}
	int id = j;
	for(i = n - 2, j = n - 1; k > 0 and i >= id; ) {
		ll d = A[j] - A[i];
		ll need = d * (n - j);
		if( need <= k ) {
			k -= need;
			j = i;
			i--;
		}
		else {
			if( j == n - 1 ) {
				A[j] -= k;
				k = 0;
			}
			break;
		}
	}
	cout << (A[j] - A[id]) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct SegmentTree {
	vector<ll> tree, lazy;

	SegmentTree( int n ) {
		tree.resize(n * 4, 0);
		lazy.resize(n * 4, -1);
	}
	inline int left( int nd ) { return nd * 2; }
	inline int right( int nd ) { return nd * 2 + 1; }
	inline void propagate( int nd, int l, int r ) {
		tree[nd] = ((r - l + 1) * lazy[nd]);
		if( l != r ) {
			lazy[left(nd)] = lazy[nd];
			lazy[right(nd)] = lazy[nd];
		}
		lazy[nd] = -1;
	}
	void build( vector<ll>& A, int nd, int l, int r ) {
		if( l == r ) {
			tree[nd] = A[l];
			return;
		}
		int mid = l + (r - l) / 2;
		build( A, left(nd), l, mid );
		build( A, right(nd), mid + 1, r );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	void update( int L, int R, int nd, int l, int r, ll v ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return;
		if( l >= L and r <= R ) {
			lazy[nd] = v;
			propagate( nd, l, r );
			return;
		}
		int mid = l + (r - l) / 2;
		update( L, R, left(nd), l, mid, v );
		update( L, R, right(nd), mid + 1, r, v );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	ll query( int L, int R, int nd, int l, int r ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return 0LL;
		if( l >= L and r <= R ) return tree[nd];
		int mid = l + (r - l) / 2;
		ll pp = query( L, R, left(nd), l, mid );
		ll qq = query( L, R, right(nd), mid + 1, r );
		return pp + qq;
	}
	void print( int n ) {
		for(int i = 0; i < n; i++) {
			cout << query( i, i, 1, 0, n - 1 ) << " ";
		}
		cout << "\n";
	}
};

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) {
		cin >> a;
	}
	sort(A.begin(), A.end());
	SegmentTree SG(n);
	SG.build( A, 1, 0, n - 1 );
	while( k > 0 ) {
		ll k1 = k;
		int id1 = -1, id2 = -1;
		ll m1 = 0, m2 = 0, h1 = 0, h2 = 0;
		int lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll have = SG.query( 0, mid, 1, 0, n - 1 );
			ll tmp = SG.query( mid, mid, 1, 0, n - 1 );
			ll need = tmp * (mid - 0 + 1);
			//cout << have << " " << need << ":::\n";
			need -= have;
			if( need <= k ) {
				id1 = mid;
				m1 = tmp, h1 = have;
				lo = mid + 1;
			}
			else hi = mid - 1;
		}
		lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll have = SG.query( mid, n - 1, 1, 0, n - 1 );
			ll tmp = SG.query( mid, mid, 1, 0, n - 1 );
			ll need = tmp * (n - mid);
			//cout << mid << " : " << have << " " << need << ":::\n";
			need = have - need;
			if( need <= k ) {
				id2 = mid;
				m2 = tmp, h2 = have;
				hi = mid - 1;
			}
			else lo = mid + 1;
		}
		//cout << id1 << " " << id2 << "\n";
		if( id1 != -1 and id2 == -1 ) {
			SG.update( 0, id1, 1, 0, n - 1, m1 );
			k -= ((m1 * (id1 + 1)) - h1);
		}
		else if( id1 == -1 and id2 != -1 ) {
			SG.update( id2, n - 1, 1, 0, n - 1, m2 );
			k -= (h2 - (m2 * (n - id2)));
		}
		else if( id1 != -1 and id2 != -1 ) {
			ll d1 = SG.query( n - 1, n - 1, 1, 0, n - 1 ) - m1;
			ll d2 = m2 - SG.query( 0, 0, 1, 0, n - 1 );
			//cout << ":: " << d1 << " " << d2 << "\n";
			if( d1 < d2 ) {
				SG.update( 0, id1, 1, 0, n - 1, m1 );
				k -= ((m1 * (id1 + 1)) - h1);
			}
			else {
				SG.update( id2, n - 1, 1, 0, n - 1, m2 );
				k -= (h2 - (m2 * (n - id2)));
			}
		}
		else break;
		//SG.print(n);
		//cout << k << "::::::\n";
		if( k1 == k ) break;
	}
	cout << (SG.query( n - 1, n - 1, 1, 0, n - 1 ) - SG.query( 0, 0, 1, 0, n - 1 )) << "\n";
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct SegmentTree {
	int n;
	vector<ll> tree, lazy;

	SegmentTree( int n ) : n(n) {
		tree.resize(n * 4, 0);
		lazy.resize(n * 4, -1);
	}
	inline int left( int nd ) { return nd * 2; }
	inline int right( int nd ) { return nd * 2 + 1; }
	inline void propagate( int nd, int l, int r ) {
		tree[nd] = ((r - l + 1) * lazy[nd]);
		if( l != r ) {
			lazy[left(nd)] = lazy[nd];
			lazy[right(nd)] = lazy[nd];
		}
		lazy[nd] = -1;
	}
	void build( vector<ll>& A, int nd, int l, int r ) {
		if( l == r ) {
			tree[nd] = A[l];
			return;
		}
		int mid = l + (r - l) / 2;
		build( A, left(nd), l, mid );
		build( A, right(nd), mid + 1, r );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline void update( int L, int R, int nd, int l, int r, ll v ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return;
		if( l >= L and r <= R ) {
			lazy[nd] = v;
			propagate( nd, l, r );
			return;
		}
		int mid = l + (r - l) / 2;
		update( L, R, left(nd), l, mid, v );
		update( L, R, right(nd), mid + 1, r, v );
		tree[nd] = tree[left(nd)] + tree[right(nd)];
	}
	inline ll query( int L, int R, int nd, int l, int r ) {
		if( ~lazy[nd] ) propagate( nd, l, r );
		if( l > R or r < L or L > R ) return 0LL;
		if( l >= L and r <= R ) return tree[nd];
		int mid = l + (r - l) / 2;
		ll pp = query( L, R, left(nd), l, mid );
		ll qq = query( L, R, right(nd), mid + 1, r );
		return pp + qq;
	}
	void updateRange( int L, int R, ll v ) {
		update( L, R, 1, 0, n - 1, v );
	}
	ll queryRange( int L, int R ) {
		if( L > R ) return 0;
		return query( L, R, 1, 0, n - 1 );
	}
	void print( int n ) {
		for(int i = 0; i < n; i++) {
			cout << query( i, i, 1, 0, n - 1 ) << " ";
		}
		cout << "\n";
	}
};

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	ll n, k;
	cin >> n >> k;
	vector<ll> A(n);
	for(auto& a : A) {
		cin >> a;
	}
	sort(A.begin(), A.end());
	SegmentTree SG(n);
	SG.build( A, 1, 0, n - 1 );
	while( k > 0 ) {
		ll k1 = k;
		ll lo = 0, hi = n - 1, id1 = -1, id2 = -1, m1, m2, b1, b2;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( 0, mid - 1 );
			ll need = tg * mid - have;
			if( need <= k ) {
				id1 = mid;
				m1 = tg;
				b1 = need;
				lo = mid + 1;
			}
			else hi = mid - 1;
		}
		lo = 0, hi = n - 1;
		while( lo <= hi ) {
			int mid = lo + (hi - lo) / 2;
			ll tg = SG.queryRange( mid, mid );
			ll have = SG.queryRange( mid + 1, n - 1 );
			ll need = have - tg * (n - mid - 1);
			if( need <= k ) {
				id2 = mid;
				m2 = tg;
				b2 = need;
				hi = mid - 1;
			}
			else lo = mid + 1;
		}

		auto choice1 = [&]() {
			k -= b1;
			SG.updateRange( 0, id1, m1 );
			return;
		};
		auto choice2 = [&]() {
			k -= b2;
			SG.updateRange( id2, n - 1, m2 );
			return;
		};
		if( ~id1 and ~id2 ) {
			ll d1 = SG.queryRange( n - 1, n - 1 ) - m1;
			ll d2 = m2 - SG.queryRange( 0, 0 );
			if( d1 < d2 ) choice1();
			else          choice2();
		}
		else if( ~id1 ) choice1();
		else if( ~id2 ) choice2();
		if( k1 == k ) break;
	}
	if( k > 0 and n > 1 ) {
		ll last = SG.queryRange( n - 1, n - 1 );
		ll lastB = SG.queryRange( n - 2, n - 2 );
		if( last > lastB ) {
			SG.updateRange( n - 1, n - 1, last - k );
			k = 0;
		}
		last = SG.queryRange( 0, 0 );
		lastB = SG.queryRange( 1, 1 );
		if( lastB > last and k > 0 ) {
			SG.updateRange( 0, 0, last + k );
		}
	}
	cout << (SG.query( n - 1, n - 1, 1, 0, n - 1 ) - SG.query( 0, 0, 1, 0, n - 1 )) << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1;
        while(true){
            if(i == j || a[i] == a[j]) return 0;
            while(i < n && a[i] == a[i + 1]) i ++;
            while(j >= 0 && a[j] == a[j - 1]) j --;

            int leftnum = i + 1, rightnum = n - j;
            long long leftcost = (long long)(a[i + 1] - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - a[j - 1]) * rightnum;
            if(k >= max(leftcost, rightcost)){
                if(leftnum <= rightnum) k -= leftcost, a[i] = a[i + 1];
                else k -= rightcost, a[j] = a[j - 1];
            }
            else if(leftnum <= rightnum){
                if(k >= leftcost) k -= leftcost, a[i] = a[i + 1];
                else return a[j] - a[i] - k / leftnum;
            }
            else{
                if(k >= rightcost) k -= rightcost, a[j] = a[j - 1];
                else return a[j] - a[i] - k / rightnum;
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1;
        while(true){
            if(i == j || a[i] == a[j]) return 0;
            while(i < n && a[i] == a[i + 1]) i ++;
            while(j >= 0 && a[j] == a[j - 1]) j --;

            int leftnum = i + 1, rightnum = n - j;
            long long leftcost = (long long)(a[i + 1] - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - a[j - 1]) * rightnum;
            if(k >= max(leftcost, rightcost)){
                if(leftnum <= rightnum) k -= leftcost, a[i] = a[i + 1];
                else k -= rightcost, a[j] = a[j - 1];
            }
            else if(k >= leftcost) k -= leftcost, a[i] = a[i + 1];
            else if(k >= rightcost) k -= rightcost, a[j] = a[j - 1];
            else{
                if(leftnum <= rightnum) return a[j] - a[i] - k / leftnum;
                else return a[j] - a[i] - k / rightnum;
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1;
        while(true){
            if(i == j || a[i] == a[j]) return 0;
            while(i < n && a[i] == a[i + 1]) i ++;
            while(j >= 0 && a[j] == a[j - 1]) j --;

            int leftnum = i + 1, rightnum = n - j;
            long long leftcost = (long long)(a[i + 1] - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - a[j - 1]) * rightnum;
            if(k >= min(leftcost, rightcost)){
                if(leftnum <= rightnum && k >= leftcost) k -= leftcost, a[i] = a[i + 1];
                else if(leftnum > rightnum && k >= rightcost) k -= rightcost, a[j] = a[j - 1];
                else if(leftnum <= rightnum) k -= leftnum, a[i] ++;
                else k -= rightnum, a[j] --;
            }
            else{
                if(leftnum <= rightnum) return a[j] - a[i] - k / leftnum;
                else return a[j] - a[i] - k / rightnum;
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1, res = a.back() - a[0];
        while(true){
            if(i == j) return 0;

            int leftnum = i + 1, rightnum = n - j;
            int leftnext = a[i + 1], rightnext = a[j - 1];
            long long leftcost = (long long)(leftnext - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - rightnext) * rightnum;
            if(k >= min(leftcost, rightcost)){
                if(leftcost <= rightcost) k -= leftcost, i ++;
                else k -= rightcost, j --;
            }
            else{
                if(leftcost <= rightcost) return a[j] - a[i] - k / leftnum;
                else return a[j] - k / rightnum - a[i];
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

//    3 3
//    7 12 8
//    ans: 2

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1, res = a.back() - a[0];
        while(true){
            if(i == j) return 0;

            int leftnum = i + 1, rightnum = n - j;
            int leftnext = a[i + 1], rightnext = a[j - 1];
            long long leftcost = (long long)(leftnext - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - rightnext) * rightnum;
            if(k >= min(leftcost, rightcost)){
                if(leftcost <= rightcost) k -= leftcost, i ++;
                else k -= rightcost, j --;
            }
            else{
                if(leftcost < rightcost) return a[j] - a[i] - k / leftnum;
                else return a[j] - k / rightnum - a[i];
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

//    3 3
//    7 12 8
//    ans: 2

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1, res = a.back() - a[0];
        while(true){
            if(i == j) return 0;

            int leftnum = i + 1, rightnum = n - j;
            int leftnext = a[i + 1], rightnext = a[j - 1];
            long long leftcost = (long long)(leftnext - a[i]) * leftnum;
            long long rightcost = (long long)(a[j] - rightnext) * rightnum;
            if(k >= min(leftcost, rightcost)){
                if(leftcost <= rightcost) k -= leftcost, i ++;
                else k -= rightcost, j --;
            }
            else{
                if(leftcost <= rightcost) return a[j] - a[i] - k / leftcost;
                else return a[j] - k / rightcost - a[i];
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{

private:
    int n;

public:
    int minDiff(vector<int>& a, long long k){

        n = a.size();
        sort(a.begin(), a.end());

        int i = 0, j = n - 1;
        while(true){
            if(i == j || a[i] == a[j]) return 0;
            while(i < n && a[i] == a[i + 1]) i ++;
            while(j >= 0 && a[j] == a[j - 1]) j --;

            int leftnum = i + 1, rightnum = n - j;
            if(k >= min(leftnum, rightnum)){
                if(leftnum <= rightnum) k -= leftnum, a[i] ++;
                else k -= rightnum, a[j] --;
            }
            else{
                return a[j] - a[i];
            }
        }
        return 0;
    }
};

int main() {

    int n;
    long long k;
    scanf("%d%lld", &n, &k);

    vector<int> a(n);
    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
    printf("%d\n", Solution().minDiff(a, k));

    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

int k;
deque<int>q;

long long solve(int k){
    sort(q.begin(), q.end());
    int mn = q.front(), mx = q.back();
    int mn_cnt=0, mx_cnt=0;
    while(true){
        while(q.size() && q.front() == mn){
            q.pop_front();
            mn_cnt += 1;
        }
        while(q.size() && q.back() == mx){
            q.pop_back();
            mx_cnt += 1;
        }
        if(q.empty())break;
        if(mn_cnt < mx_cnt){
            long long need = 1LL*(q.front()-mn)*mn_cnt;
            if(need<=k){
                mn = q.front();
                k -= need;
                continue;
            }
            else{
                mn = mn + k/mn_cnt;
                return mx-mn;
            }
        }
        else{
            long long need = 1LL*(mx - q.back())*mx_cnt;
            if(need<=k){
                mx = q.back();
                k -= need;
                continue;
            }
            else{
                mx = mx - k/mx_cnt;
                return mx-mn;
            }
        }
    }
    if(mn_cnt < mx_cnt){
        long long need = 1LL*(mx-mn)*mn_cnt;
        if (k > need) return 0;
        else {
            mn = mn + k/mn_cnt;
            return mx-mn;
        }
    }
    else{
       long long need = 1LL*(mx-mn)*mx_cnt;
       if(k > need) return 0;
       else{
           mx = mx - k/mx_cnt;
           return mx-mn;
       }
    }
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        q.push_back(temp);
    }
    printf("%lld\n", solve(k));
    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

#define FRONT 0

map<int,int>cnt;
vector<int>v;

int minimize_diff(int i, int j, int k){
    //printf("%d %d %d\n", i ,j ,k);

    if(k==0)return v[j]-v[i];
    if(v[i]==v[j] || i==j)return 0;

    int need_inc = (v[i+1]-v[i])*cnt[v[i]];
    int need_dec = (v[j]-v[j-1])*cnt[v[j]];

    need_inc = need_inc <= k ? need_inc : INT_MAX;
    need_dec = need_dec <= k ? need_dec : INT_MAX;

    //printf("inc %d dec %d\n", need_inc, need_dec);

    if(min(need_dec, need_inc)==INT_MAX){
        int ret = INT_MAX;
        int mn = v[i] + k/cnt[v[i]];
        ret = min(ret, v[j]-mn);
        int mx = v[j] - k/cnt[v[j]];
        ret = min(ret, mx-v[i]);
        return ret;
    }

    if(need_inc < need_dec){
        cnt[v[i+1]] += cnt[v[i]];
        return minimize_diff(i+1, j, k-need_inc);
    }
    else{
        cnt[v[j-1]] += cnt[v[j]];
        return minimize_diff(i, j-1, k-need_dec);
    }
}

int solve(int k){
    sort(v.begin(), v.end());
    int len = v.size();
    return minimize_diff(0, len-1, k);
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, k, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        if(cnt.find(temp)==cnt.end()){
            cnt[temp] = 1;
            v.push_back(temp);
        }
        else
            cnt[temp] += 1;
    }
    printf("%d\n", solve(k));
    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

long long k;
deque<int>q;

long long solve(int k){
    sort(q.begin(), q.end());
    int mn = q.front(), mx = q.back();
    int mn_cnt=0, mx_cnt=0;
    while(true){
        while(q.size() && q.front() == mn){
            q.pop_front();
            mn_cnt += 1;
        }
        while(q.size() && q.back() == mx){
            q.pop_back();
            mx_cnt += 1;
        }
        if(q.empty())break;
        if(mn_cnt < mx_cnt){
            long long need = 1LL*(q.front()-mn)*mn_cnt;
            if(need<=k){
                mn = q.front();
                k -= need;
                continue;
            }
            else{
                mn = mn + k/mn_cnt;
                return mx-mn;
            }
        }
        else{
            long long need = 1LL*(mx - q.back())*mx_cnt;
            if(need<=k){
                mx = q.back();
                k -= need;
                continue;
            }
            else{
                mx = mx - k/mx_cnt;
                return mx-mn;
            }
        }
    }
    if(mn_cnt < mx_cnt){
        long long need = 1LL*(mx-mn)*mn_cnt;
        if (k > need) return 0;
        else {
            mn = mn + k/mn_cnt;
            return mx-mn;
        }
    }
    else{
       long long need = 1LL*(mx-mn)*mx_cnt;
       if(k > need) return 0;
       else{
           mx = mx - k/mx_cnt;
           return mx-mn;
       }
    }
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        q.push_back(temp);
    }
    printf("%lld\n", solve(k));
    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

long long k;
deque<int>q;

long long solve(){
    sort(q.begin(), q.end());
    int mn = q.front(), mx = q.back();
    int mn_cnt=0, mx_cnt=0;
    while(true){
        while(q.size() && q.front() == mn){
            q.pop_front();
            mn_cnt += 1;
        }
        while(q.size() && q.back() == mx){
            q.pop_back();
            mx_cnt += 1;
        }
        if(q.empty())break;
        if(mn_cnt < mx_cnt){
            long long need = 1LL*(q.front()-mn)*mn_cnt;
            if(need<=k){
                mn = q.front();
                k -= need;
                continue;
            }
            else{
                mn = mn + k/mn_cnt;
                return mx-mn;
            }
        }
        else{
            long long need = 1LL*(mx - q.back())*mx_cnt;
            if(need<=k){
                mx = q.back();
                k -= need;
                continue;
            }
            else{
                mx = mx - k/mx_cnt;
                return mx-mn;
            }
        }
    }
    if(mn_cnt < mx_cnt){
        long long need = 1LL*(mx-mn)*mn_cnt;
        if (k > need) return 0;
        else {
            mn = mn + k/mn_cnt;
            return mx-mn;
        }
    }
    else{
       long long need = 1LL*(mx-mn)*mx_cnt;
       if(k > need) return 0;
       else{
           mx = mx - k/mx_cnt;
           return mx-mn;
       }
    }
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, temp;
    scanf("%d %lld", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        q.push_back(temp);
    }
    printf("%lld\n", solve());
    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

int k;
deque<int>q;

int solve(int k){
    sort(q.begin(), q.end());
    int mn = q.front(), mx = q.back();
    int mn_cnt=0, mx_cnt=0;
    while(true){
        while(q.size() && q.front() == mn){
            q.pop_front();
            mn_cnt += 1;
        }
        while(q.size() && q.back() == mx){
            q.pop_back();
            mx_cnt += 1;
        }
        if(q.empty())break;
        if(mn_cnt < mx_cnt){
            long long need = (long long)(q.front()-mn)*mn_cnt;
            if(need<=k){
                mn = q.front();
                k -= need;
                continue;
            }
            else{
                mn = mn + k/mn_cnt;
                return mx-mn;
            }
        }
        else{
            long long need = (long long) (mx - q.back())*mx_cnt;
            if(need<=k){
                mx = q.back();
                k -= need;
                continue;
            }
            else{
                mx = mx - k/mx_cnt;
                return mx-mn;
            }
        }
    }
    if(mn_cnt < mx_cnt){
        long long need = (long long)(mx-mn)*mn_cnt;
        if (k > need) return 0;
        else {
            mn = mn + k/mn_cnt;
            return mx-mn;
        }
    }
    else{
       long long need = (long long)(mx-mn)*mx_cnt;
       if(k > need) return 0;
       else{
           mx = mx - k/mx_cnt;
           return mx-mn;
       }
    }
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        q.push_back(temp);
    }
    printf("%d\n", solve(k));
    return 0;
}


    #include<bits/stdc++.h>
    using namespace std;

    int main() {
        ios::sync_with_stdio(0);
        cin.tie(0);

        int n;
        long long k;
        cin>>n>>k;

        deque<int> a;
        for (int i=1; i<=n; i++) {
            int x;
            cin>>x;
            a.push_back(x);
        }

        sort(a.begin(), a.end());

        int mn = a.front(), mx = a.back();
        int mncnt = 0, mxcnt = 0;

        while (true) {
            while (a.size() && a.front() == mn) a.pop_front(), mncnt++;
            while (a.size() && a.back() == mx)  a.pop_back(), mxcnt++;

            if (a.size() == 0)  break;
    //        cout<<mn<<" "<<mx<<" "<<mncnt<<" "<<mxcnt<<" "<<k<<endl;
            if (mncnt < mxcnt) {
                int d = a.front() - mn;
                assert(d > 0);
                long long req = 1LL*d*mncnt;
                if (k >= req) {
                    k-=req;
                    mn = a.front();
                    continue;
                }
                else {
                    mn = mn + k/mncnt;
                    cout<<mx-mn<<endl;
                    return 0;
                }
            }
            else {
                int d = mx - a.back();
                assert(d > 0);
                long long req = 1LL*d*mxcnt;
                if (k >= req) {
                    k -= req;
                    mx = a.back();
                    continue;
                }
                else {
                    mx = mx - k/mxcnt;
                    cout<<mx-mn<<endl;
                    return 0;
                }
            }
        }

        if (mncnt < mxcnt) {
            long long req = 1LL*(mx-mn)*mncnt;
            if (k > req)    cout<<0<<endl;
            else {
                mn = mn + k/mncnt;
                cout<<mx-mn<<endl;
            }
        }
        else {
            long long req = 1LL*(mx-mn)*mxcnt;
            if (k > req)    cout<<0<<endl;
            else {
                mx = mx - k/mxcnt;
                cout<<mx-mn<<endl;
            }
        }
    }


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

#define FRONT 0

map<int,int>cnt;
vector<int>v;

int minimize_diff(int i, int j, int k){
    //printf("%d %d %d\n", i ,j ,k);

    if(k==0)return v[j]-v[i];
    if(v[i]==v[j] || i==j)return 0;

    int need_inc = (v[i+1]-v[i])*cnt[v[i]];
    int need_dec = (v[j]-v[j-1])*cnt[v[j]];

    need_inc = need_inc <= k ? need_inc : INT_MAX;
    need_dec = need_dec <= k ? need_dec : INT_MAX;

    //printf("inc %d dec %d\n", need_inc, need_dec);

    if(min(need_dec, need_inc)==INT_MAX)
        return v[j]-v[i];
    if(need_inc < need_dec){
        cnt[v[i+1]] += cnt[v[i]];
        return minimize_diff(i+1, j, k-need_inc);
    }
    else{
        cnt[v[j-1]] += cnt[v[j]];
        return minimize_diff(i, j-1, k-need_dec);
    }
}

int solve(int k){
    sort(v.begin(), v.end());
    int len = v.size();
    return minimize_diff(0, len-1, k);
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, k, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        if(cnt.find(temp)==cnt.end()){
            cnt[temp] = 1;
            v.push_back(temp);
        }
        else
            cnt[temp] += 1;
    }
    printf("%d\n", solve(k));
    return 0;
}


/*
 * File : MinimizingDifference.cpp
 * Created by mursalin on 2019-10-17
 * Distributed under terms of the MIT license.
 */

#include <bits/stdc++.h>

using namespace std;

int k;
deque<int>q;

int solve(int k){
    sort(q.begin(), q.end());
    int mn = q.front(), mx = q.back();
    int mn_cnt=0, mx_cnt=0;
    while(true){
        while(q.size() && q.front() == mn){
            q.pop_front();
            mn_cnt += 1;
        }
        while(q.size() && q.back() == mx){
            q.pop_back();
            mx_cnt += 1;
        }
        if(q.empty())break;
        if(mn_cnt < mx_cnt){
            long long need = (long long)(q.front()-mn)*mn_cnt;
            if(need<=k){
                mn = q.front();
                k -= need;
                continue;
            }
            else{
                mn = mn + k/mn_cnt;
                return mx-mn;
            }
        }
        else{
            long long need = (long long) (mx - q.back())*mx_cnt;
            if(need<=k){
                mx = q.back();
                k -= need;
                continue;
            }
            else{
                mx = mx - k/mx_cnt;
                return mx-mn;
            }
        }
    }
    if(mn_cnt < mx_cnt){
        long long need = 1LL*(mx-mn)*mn_cnt;
        if (k > need) return 0;
        else {
            mn = mn + k/mn_cnt;
            return mx-mn;
        }
    }
    else{
       long long need = 1LL*(mx-mn)*mx_cnt;
       if(k > need) return 0;
       else{
           mx = mx - k/mx_cnt;
           return mx-mn;
       }
    }
}

int main(int argc, char **argv){
    //freopen("input.txt", "r", stdin);
    int n, temp;
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; i++){
        scanf("%d", &temp);
        q.push_back(temp);
    }
    printf("%d\n", solve(k));
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N;
	long long int k;
	cin >> N >> k;
	vector<long long int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	long long int l = 0,r = N-1;
	while(k){
		if(A[l] == A[r])
			break;
		long long int l2 = smallest(A,l);
		long long int r2 = largest(A,r);
		// cout << l2 <<" "<< r2 <<endl;
		// if((l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]) <= k ){
		// 	k-= (l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]);
		// 	l = l2+1;
		// 	r = r2-1;
		// }
		if((l2+1)*(A[l2+1]-A[l]) <= (N-r2)*(A[r]-A[r2-1]) && (l2+1)*(A[l2+1]-A[l]) <= k){
			k-= (l2+1)*(A[l2+1]-A[l]);
			l = l2+1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) <= (l2+1)*(A[l2+1]-A[l]) && (N-r2)*(A[r]-A[r2-1]) <= k){
			k-=(N-r2)*(A[r]-A[r2-1]);
			r = r2-1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) >= k && (l2+1)*(A[l2+1]-A[l]) >= k){
			int diff = max(k/(N-r2), k/(l2+1));
			k = 0;
			A[l2]+=diff;
			l = l2;
		}
		else
			break;

	}
	cout << A[r]-A[l]<<endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N,k;
	cin >> N >> k;
	vector<int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	int l = 0,r = N-1;
	while(k){
		if(A[l] == A[r])
			break;
		int l2 = smallest(A,l);
		int r2 = largest(A,r);
		// cout << l2 <<" "<< r2 <<endl;
		if((l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]) <= k ){
			k-= (l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]);
			l = l2+1;
			r = r2-1;
		}
		else if((l2+1)*(A[l2+1]-A[l]) < (N-r2)*(A[r]-A[r2-1]) && (l2+1)*(A[l2+1]-A[l]) <= k){
			k-= (l2+1)*(A[l2+1]-A[l]);
			l = l2+1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) < (l2+1)*(A[l2+1]-A[l]) && (N-r2)*(A[r]-A[r2-1]) <= k){
			k-=(N-r2)*(A[r]-A[r2-1]);
			r = r2-1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) >= k && (l2+1)*(A[l2+1]-A[l]) >= k){
			int diff = max(k/(N-r2), k/(l2+1));
			k = 0;
			A[l2]+=diff;
			l = l2;
		}
		else
			break;

	}
	cout << A[r]-A[l]<<endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N;
	long long int k;
	cin >> N >> k;
	vector<long long int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	long long int l = 0,r = N-1;
	while(k){
		if(A[l] == A[r])
			break;
		long long int l2 = smallest(A,l);
		long long int r2 = largest(A,r);
		// cout << l2 <<" "<< r2 <<" " << (l2+1)*(A[l2+1]-A[l])<<endl;
		// if((l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]) <= k ){
		// 	k-= (l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]);
		// 	l = l2+1;
		// 	r = r2-1;
		// }
		if((l2+1) <= (N-r2) && (l2+1)*(A[l2+1]-A[l]) <= k){
			k-= (l2+1)*(A[l2+1]-A[l]);
			l = l2+1;
		}
		else if((N-r2) <= (l2+1) && (N-r2)*(A[r]-A[r2-1]) <= k){
			k-=(N-r2)*(A[r]-A[r2-1]);
			r = r2-1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) >= k && (l2+1)*(A[l2+1]-A[l]) >= k){
			int diff = max(k/(N-r2), k/(l2+1));
			k = 0;
			A[l2]+=diff;
			l = l2;
		}
		else
			break;

	}
	cout << A[r]-A[l]<<endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N;
	long long int k;
	cin >> N >> k;
	vector<long long int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	int l = 0,r = N-1;
	while(k){
		if(A[l] == A[r])
			break;
		int l2 = smallest(A,l);
		int r2 = largest(A,r);
		// cout << l2 <<" "<< r2 <<endl;
		// if((l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]) <= k ){
		// 	k-= (l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]);
		// 	l = l2+1;
		// 	r = r2-1;
		// }
		if((l2+1)*(A[l2+1]-A[l]) <= (N-r2)*(A[r]-A[r2-1]) && (l2+1)*(A[l2+1]-A[l]) <= k){
			k-= (l2+1)*(A[l2+1]-A[l]);
			l = l2+1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) <= (l2+1)*(A[l2+1]-A[l]) && (N-r2)*(A[r]-A[r2-1]) <= k){
			k-=(N-r2)*(A[r]-A[r2-1]);
			r = r2-1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) >= k && (l2+1)*(A[l2+1]-A[l]) >= k){
			int diff = max(k/(N-r2), k/(l2+1));
			k = 0;
			A[l2]+=diff;
			l = l2;
		}
		else
			break;

	}
	cout << A[r]-A[l]<<endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N,k;
	cin >> N >> k;
	vector<int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	int l = 0,r = N-1;
	while(k){
		if(A[l] == A[r])
			break;
		int l2 = smallest(A,l);
		int r2 = largest(A,r);
		// cout << l2 <<" "<< r2 <<endl;
		// if((l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]) <= k ){
		// 	k-= (l2+1)*(A[l2+1]-A[l]) + (N-r2)*(A[r]-A[r2-1]);
		// 	l = l2+1;
		// 	r = r2-1;
		// }
		if((l2+1)*(A[l2+1]-A[l]) <= (N-r2)*(A[r]-A[r2-1]) && (l2+1)*(A[l2+1]-A[l]) <= k){
			k-= (l2+1)*(A[l2+1]-A[l]);
			l = l2+1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) <= (l2+1)*(A[l2+1]-A[l]) && (N-r2)*(A[r]-A[r2-1]) <= k){
			k-=(N-r2)*(A[r]-A[r2-1]);
			r = r2-1;
		}
		else if((N-r2)*(A[r]-A[r2-1]) >= k && (l2+1)*(A[l2+1]-A[l]) >= k){
			int diff = max(k/(N-r2), k/(l2+1));
			k = 0;
			A[l2]+=diff;
			l = l2;
		}
		else
			break;

	}
	cout << A[r]-A[l]<<endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <numeric>
#include <iomanip>
#include <deque>
#include<climits>
using namespace std;

typedef pair<int,int> pii;
typedef long long int lli;

int smallest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start+1 ; i < A.size(); i++){
		if(A[start] < A[i]){
			count = i;
			break;
		}
	}
	return count-1;
}

int largest(vector<long long int> &A,int start){
	int count = 0;
	for(int i = start-1 ; i >= 0; i--){
		if(A[start] > A[i]){
			count = i;
			break;
		}
	}
	return count+1;
}

int main() {
	int N;
	long long int k;
	cin >> N >> k;
	vector<long long int> A(N);
	for(int i = 0; i < N; i++)
		cin >> A[i];
	sort(A.begin(),A.end());
	for(int i = 1 ; i<=N/2 && k;i++){
		long long int diff = (A[i]-A[i-1] + A[N-i]-A[N-i-1])*i;
		if(k>diff){
			k-=diff;
		}
		else{
			cout << A[N-i]-A[i-1] - k/i <<endl;
			return 0;
		}
	}
	cout << 0<<endl;
	return 0;
}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		if(k - s > (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1))
			s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		else
		{
			ll ss = (k - s)/(i + 1);
			cout<<min(1ll*0,a[n - i - 1] - a[i] - ss)<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		if(k - s > (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1))
			s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		else
		{
			ll ss = (k - s)/(i + 1);
			cout<<min(1ll 0,a[n - i - 1] - a[i] - ss)<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		if(k - s > (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1))
			s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		else
		{
			ll ss = (k - s)/(i + 1);
			cout<<min(0,a[n - i - 1] - a[i] - ss)<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		if(k - s > (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1))
			s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		else
		{
			ll ss = (k - s)/(i + 1);
			cout<<a[n - i - 1] - a[i] - ss<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		if(s > k)
		{
			cout<<a[n - i - 1] - a[i]<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


/*    ____
     /    \
     \Hera/
	  \  /
	   \/
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define INT_MAXi 10e15
#define ll long long int
#define rep(a,b,c) for(int i = a; i < b; i+=c)
#define pb push_back
#define mp make_pair

bool sorta(pair<int,int> a,pair<int,int>b)
{
	if(a.first != b.first) return (a.first<b.first);
	return (a.second < b.second);
}

int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	rep(0,n,1)
	{
		cin>>a[i];
	}
	sort(a,a + n);
	ll s = 0;
	rep(0,n/2 + 1,1)
	{
		if(k - s > (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1))
			s += (a[i + 1] - a[i] + a[n - i - 1] - a[n - i - 2])*(i + 1);
		else
		{
			ll ss = (k - s)/(i + 1);
			cout<<max(1ll*0,a[n - i - 1] - a[i] - ss)<<endl;
			return 0;
		}
	}
	cout<<0<<endl;
	return 0;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=1e18;
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
       // cout<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1<pos2)
        {
            low=mid1+1;
        }
        else
        {
                high=mid2-1;
        }
    }
    cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=a[n-1];
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
        //cout<<low<<' '<<high<<' '<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1>=1e18)
        {
            high=mid1-1;
            continue;
        }
        if(pos2>=1e18)
        {
            high=mid2-1;
            continue;
        }

        if(pos1<pos2)
        {
            high=mid2-1;
        }
        else if(pos1>pos2)
        {
            low=mid1+1;
        }
        else
        {
            if(mid1==low)
                low=mid1+1;
            else if(mid2==high)
                high=mid2-1;
            else
                high=mid2-1;
        }

    }
    cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=a[n-1];
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
        //cout<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1>=1e18)
        {
            high=mid1-1;
            continue;
        }
        if(pos2>=1e18)
        {
            high=mid2-1;
            continue;
        }

        if(pos1<pos2)
        {
            high=mid2-1;
        }
        else if(pos1>pos2)
        {
            low=mid1+1;
        }
        else
        {
            high=mid2-1;
        }

    }
    cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=a[n-1];
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
       // cout<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1<pos2)
        {
            high=mid2-1;
        }
        else
        {
            low=mid1+1;
        }
    }
    cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=a[n-1];
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
       // cout<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1>=1e18)
        {
            high=mid1-1;
        }

        if(pos1<pos2)
        {
            high=mid2-1;
        }
        else
        {
            low=mid1+1;
        }
    }
    cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define M 1000000007
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll n,k,a[N];
ll mx;
bool possible(ll mid,ll baki)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
        {
            baki-=(a[i]-mid);
        }
    }
    return baki>=0;
}

ll pos(ll mn)
{
    ll now=0;
    now=abs(mn-a[0]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<mn)
        {
            now+=mn-a[i];
        }
    }
    if(now>k) return 1e18;
    //cout<<"co"<<endl;
    ll low=mn,high=mx;
    ll ans=-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(possible(mid,k-now))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }
    if(ans==-1) return 1e18;
    else return ans-mn;
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    mx=a[n-1];
    //cout<<pos(4);
    ll low=a[0],high=a[n-1];
    ll ans=1e18;
    while(low<=high)
    {
        ll mid1=(2*low+high)/3,mid2=(low+2*high)/3;
        ll pos1=pos(mid1),pos2=pos(mid2);
        ans=min(ans,pos1);
        ans=min(ans,pos2);
       // cout<<mid1<<' '<<mid2<<' '<<pos1<<' '<<pos2<<endl;
        if(pos1>=1e18)
        {
            high=mid1-1;
            continue;
        }
        if(pos2>=1e18)
        {
            high=mid2-1;
            continue;
        }

        if(pos1<pos2)
        {
            high=mid2-1;
        }
        else
        {
            low=mid1+1;
        }
    }
    cout<<ans;

}


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
from math import factorial as f
from fractions import gcd as g

N = int(raw_input ())
l = [int(i) for i in raw_input ().split()]
ret = 0
for i in range(N):
    ret += l[i] / 3
    l [i] %= 3
left = l[0]
for i in range(1, N):
    if l[i] == 0: continue
    elif l [i] == 1: left += 1
    else:
        if left > 0:
            ret += 1
            left -= 1
        else:
            left += 2
print ret


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
from math import factorial as f
from fractions import gcd as g

N = int(raw_input ())
l = [int(i) for i in raw_input ().split()]
ret = l[0] / 3
left = l [0] % 3
for i in range(1, N):
    x = left * 2
    if l [i] >= x:
        ret += left
        left = 0
        l [i] -= x
    else:
        ret += l [i] / 2
        left -= l [i] / 2
        l [i] %= 2
    ret += l [i] / 3
    left += (l [i] % 3)
print ret


n = int(raw_input())
a = [int(i) for i in raw_input().split(' ')]
ans = 0
b = 0
for i in a:
    p = min(b , i // 2)
    b -= p
    q = (i - p * 2) // 3
    b += i - p * 2 - q * 3
    ans += p + q
print ans


from sys import stdin
from itertools import repeat
def main(mn=min):
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    ans = u = 0
    for x in a:
        t = mn(u, x / 2)
        ans += t
        u -= t
        x -= t * 2
        ans += x / 3
        u += x % 3
    print ans
main()


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0

i = n - 1
j = n - 2
nonz = 0

for i in range(n):
    res += arr[i] // 6 * 2
    arr[i] = arr[i] % 6
    if arr[i] in (5,3):
        res += 1
        arr[i] -= 3

while i >= 0:
    if arr[i] < 2:
        i -= 1
        continue
    if arr[i] == 2:
        j = min(j,i - 1)
        while arr[j] in (2,0) and j >= nonz:
            j -= 1
        if j >= nonz:
            res += 1
            arr[i] = 0
            arr[j] -= 1
        else:
            while arr[nonz] == 0:
                nonz += 1
            arr[i] -= 2
            arr[nonz] -= 1
            if arr[nonz] >= 0:
                res += 1
    elif arr[i] == 4:
        j = min(j,i - 1)
        for e in range(2):
            while arr[j] in (2,0) and j >= nonz:
                j -= 1
            if j >= nonz:
                res += 1
                arr[i] = 0
                arr[j] -= 1
            else:
                while arr[nonz] == 0:
                    nonz += 1
                arr[i] -= 2
                arr[nonz] -= 1
                if arr[nonz] >= 0:
                    res += 1
print(res)


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0

i = n - 1
j = n - 2
nonz = 0

for i in range(n):
    res += arr[i] // 6 * 2
    arr[i] = arr[i] % 6
    if arr[i] == 5:
        res += 1
        arr[i] -= 3

while i >= 0:
    if arr[i] < 2:
        i -= 1
        continue
    if arr[i] == 3:
        res += 1
        arr[i] -= 3
    elif arr[i] in (2,5):
        res += arr[i] // 3
        j = min(j,i - 1)
        while arr[j] in (2,0) and j >= nonz:
            j -= 1
        if j >= nonz:
            res += 1
            arr[i] = 0
            arr[j] -= 1
        else:
            while arr[nonz] == 0:
                nonz += 1
            arr[i] -= 2
            arr[nonz] -= 1
            if arr[nonz] >= 0:
                res += 1
    elif arr[i] == 4:
        j = min(j,i - 1)
        for e in range(2):
            while arr[j] in (2,0) and j >= nonz:
                j -= 1
            if j >= nonz:
                res += 1
                arr[i] = 0
                arr[j] -= 1
            else:
                while arr[nonz] == 0:
                    nonz += 1
                arr[i] -= 2
                arr[nonz] -= 1
                if arr[nonz] >= 0:
                    res += 1
print(res)


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0

i = n - 1
j = n - 2
nonz = 0
while i >= 0:
    if arr[i] < 2:
        i -= 1
        continue
    res += arr[i] // 6
    arr[i] = arr[i] % 6
    if arr[i] == 3:
        res += 1
        arr[i] -= 3
    elif arr[i] in (2,5):
        res += arr[i] // 3
        j = min(j,i - 1)
        while arr[j] in (2,0) and j >= nonz:
            j -= 1
        if j >= nonz:
            res += 1
            arr[i] = 0
            arr[j] -= 1
        else:
            while arr[nonz] == 0:
                nonz += 1
            arr[i] -= 2
            arr[nonz] -= 1
            if arr[nonz] >= 0:
                res += 1
    elif arr[i] == 4:
        j = min(j,i - 1)
        for e in range(2):
            while arr[j] in (2,0) and j >= nonz:
                j -= 1
            if j >= nonz:
                res += 1
                arr[i] = 0
                arr[j] -= 1
            else:
                while arr[nonz] == 0:
                    nonz += 1
                arr[i] -= 2
                arr[nonz] -= 1
                if arr[nonz] >= 0:
                    res += 1
print(res)


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0
cnt = 0
for i in range(n):
    if arr[i] % 2 == 1:
        cnt += 1
        arr[i] -= 1

    x = min(2 * cnt, arr[i])
    arr[i] -= x
    cnt -= x // 2

    res += arr[i] // 3
    cnt += arr[i] % 3
    res += x // 2

print(res)


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0

i = n - 1
j = n - 2
nonz = 0
while i >= 0:
    if arr[i] < 2:
        i -= 1
        continue
    res += arr[i] // 6 * 2
    arr[i] = arr[i] % 6
    if arr[i] == 3:
        res += 1
        arr[i] -= 3
    elif arr[i] in (2,5):
        res += arr[i] // 3
        j = min(j,i - 1)
        while arr[j] in (2,0) and j >= nonz:
            j -= 1
        if j >= nonz:
            res += 1
            arr[i] = 0
            arr[j] -= 1
        else:
            while arr[nonz] == 0:
                nonz += 1
            arr[i] -= 2
            arr[nonz] -= 1
            if arr[nonz] >= 0:
                res += 1
    elif arr[i] == 4:
        j = min(j,i - 1)
        for e in range(2):
            while arr[j] in (2,0) and j >= nonz:
                j -= 1
            if j >= nonz:
                res += 1
                arr[i] = 0
                arr[j] -= 1
            else:
                while arr[nonz] == 0:
                    nonz += 1
                arr[i] -= 2
                arr[nonz] -= 1
                if arr[nonz] >= 0:
                    res += 1
print(res)


import sys,math
from fractions import gcd
from bisect import bisect_left
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = [int(x) for x in input().split()]

res = 0

i = n - 1
j = n - 2
nonz = 0

for i in range(n):
    res += arr[i] // 6 * 2
    arr[i] = arr[i] % 6
    if arr[i] in (5,3):
        res += 1
        arr[i] -= 3

while i >= 0:
    if arr[i] < 2:
        i -= 1
        continue
    if arr[i] == 2:
        j = min(j,i - 1)
        while arr[j] in (2,0) and j >= nonz:
            j -= 1
        if j >= nonz:
            res += 1
            arr[i] = 0
            arr[j] -= 1
        else:
            while arr[nonz] == 0:
                nonz += 1
            arr[i] -= 2
            arr[nonz] -= 1
            if arr[nonz] >= 0:
                res += 1
    elif arr[i] == 4:
        j = min(j,i - 1)
        for e in range(2):
            while arr[j] in (2,0) and j >= nonz:
                j -= 1
            if j >= nonz:
                res += 1
                arr[i] = 0
                arr[j] -= 1
                if arr[j] == 3:
                    arr[j] -= 3
                    res += 1
            else:
                while arr[nonz] == 0:
                    nonz += 1
                arr[i] -= 2
                arr[nonz] -= 1
                if arr[nonz] >= 0:
                    res += 1
print(res)


n, ans, last = int(raw_input()), 0, 0
a = list(map(int, raw_input().split()))
for i in range(len(a)):
    while last > 0 and a[i] >= 2:
        ans += 1
        a[i] -= 2
        last -= 1
    ans += a[i] // 3
    last += a[i] % 3
print(ans)


def input_s():
    return raw_input()

def input_i():
    return int(raw_input())

def input_2_i():
    arr = map(int, raw_input().split())
    return arr[0], arr[1]

def input_3_i():
    arr = map(int, raw_input().split())
    return arr[0], arr[1], arr[2]

def input_i_array():
    return map(int, raw_input().split())

def get_presum(n, arr):
    presum = [arr[0]]
    for i in range(1, n):
        presum.append(presum[i - 1] + arr[i])
    return presum

def calc(n, a):
    dp = [[0, 0] for _ in range(n)]
    remain = [[0, 0] for _ in range(n)]
    dp[0] = [0, a[0] / 3]
    remain[0] = [a[0], a[0] % 3]
    for i in range(1, n):
        dp[i][0] = max(dp[i - 1])
        if dp[i - 1][0] > dp[i - 1][1]:
            remain[i][0] = remain[i - 1][0] + a[i]
        elif dp[i - 1][0] < dp[i - 1][1]:
            remain[i][0] = remain[i - 1][1] + a[i]
        else:
            remain[i][0] = max(remain[i - 1]) + a[i]

        cnt_cost = [
            min(a[i] / 2, remain[i - 1][0]),
            min(a[i] / 2, remain[i - 1][1])
        ]

        extras = [
            (a[i] - cnt_cost[0] * 2) / 3,
            (a[i] - cnt_cost[1] * 2) / 3
        ]

        if dp[i - 1][0] + cnt_cost[0] + extras[0] > dp[i - 1][1] + cnt_cost[1] + extras[1]:
            dp[i][1] = dp[i - 1][0] + cnt_cost[0] + extras[0]
            remain[i][1] = remain[i - 1][0] - cnt_cost[0] + (a[i] - cnt_cost[0] * 2 - extras[0] * 3)
        elif dp[i - 1][0] + cnt_cost[0] + extras[0] < dp[i - 1][1] + cnt_cost[1] + extras[1]:
            dp[i][1] = dp[i - 1][1] + cnt_cost[1] + extras[1]
            remain[i][1] = remain[i - 1][1] - cnt_cost[1] + (a[i] - cnt_cost[1] * 2 - extras[1] * 3)
        else:
            dp[i][1] = dp[i - 1][1] + cnt_cost[1] + extras[1]
            if remain[i - 1][0] > remain[i - 1][1]:
                remain[i][1] = remain[i - 1][0] - cnt_cost[0] + (a[i] - cnt_cost[0] * 2 - extras[0] * 3)
            else:
                remain[i][1] = remain[i - 1][1] - cnt_cost[1] + (a[i] - cnt_cost[1] * 2 - extras[1] * 3)

    """
    for i in range(n):
        print '{}({}) '.format(dp[i][0], remain[i][0]),

    print ''

    for i in range(n):
        print '{}({}) '.format(dp[i][1], remain[i][1]),

    print ''
    """
    return max(dp[n - 1])

"""
5
1 3 3 5 3
"""

n = input_i()
arr = input_i_array()

print calc(n, arr)