/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <utility>
#include <vector>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
using namespace std;

//用于减少代码量的宏;
#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=(b);--i)
#define rrepf(i,a,b) fdt(i,(a)-1,b)
#define rep(i,n) repf(i,0,n)
#define rrep(i,n) rrepf(i,n,0)
#define for_each(e,s) for (__typeof__((s).begin()) e=(s).begin();e!=(s).end();++e)
#define for_nonempty_subsets(subset,set) for (int subset=set;subset;subset=(subset-1)&(set))
#define for_in_charset(i,charset) for (cstr i=(charset);*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,char(0))
#define cpy(x,y) memcpy(x,y,sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x),(x).erase(unique(all(x)),(x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() (cerr)
#define input(in) freopen(in,"r",stdin)
#define output(out) freopen(out,"w",stdout)
#else
#define prt(x) cerr<<"Line "<<__LINE__<<": "<<#x" = "<<(x)<<endl
#define asrtWA(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtTLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtMLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtOLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtRE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define runtime() cerr<<"Used: "<<db(clock())/CLOCKS_PER_SEC<<"s"<<endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo=(~0u)>>1;
lli ooll=(~0ull)>>1;
db inf=1e+10;
db eps=1e-10;
db gam=0.5772156649015328606;
db pi=acos(-1.0);
int dx[]={1,0,-1,0,1,-1,-1,1,0};
int dy[]={0,1,0,-1,1,1,-1,-1,0};
int MOD=1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
template<typename type>inline type mod(const type& x){rtn x%MOD;}
inline int sgn(const db& x){rtn (x>+eps)-(x<-eps);}
inline int dbcmp(const db& a,const db& b){rtn sgn(a-b);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);}
template<typename type>inline pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);}
template<typename type>inline pr<type,type> operator*(const pr<type,type>& a,const type& b){rtn mp(a.x*b,a.y*b);}
template<typename type>inline pr<type,type> operator/(const pr<type,type>& a,const type& b){rtn mp(a.x/b,a.y/b);}
template<typename type>inline pr<type,type>& operator-=(pr<type,type>& a,const pr<type,type>& b){rtn a=a-b;}
template<typename type>inline pr<type,type>& operator+=(pr<type,type>& a,const pr<type,type>& b){rtn a=a+b;}
template<typename type>inline pr<type,type>& operator*=(pr<type,type>& a,const type& b){rtn a=a*b;}
template<typename type>inline pr<type,type>& operator/=(pr<type,type>& a,const type& b){rtn a=a/b;}
template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.y-a.y*b.x;}
template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.x+a.y*b.y;}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
inline lli bin_pow(lli x,lli y){lli z=1;whl(y){if(y&1)z=mod(z*x);x=mod(sqr(x)),y>>=1;}rtn z;}
template<typename istream,typename first_type,typename second_type>inline istream& operator>>(istream& cin,pr<first_type,second_type>& x){rtn cin>>x.x>>x.y;}
template<typename ostream,typename first_type,typename second_type>inline ostream& operator<<(ostream& cout,const pr<first_type,second_type>& x){rtn cout<<x.x<<" "<<x.y;}
template<typename istream,typename type>inline istream& operator>>(istream& cin,vec<type>& x){rep(i,sz(x))cin>>x[i];rtn cin;}
template<typename ostream,typename type>inline ostream& operator<<(ostream& cout,const vec<type>& x){rep(i,sz(x))cout<<x[i]<<(i+1==sz(x)?"":" ");rtn cout;}
inline ostream& pdb(int prcs,db x){rtn cout<<setprecision(prcs)<<fixed<<(sgn(x)?(x):0);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));rrep(i,y){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& st){rep(i,sz(st))st[i]=i;}
inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;}
inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;}
inline void make_set(vpii& st){rep(i,sz(st))st[i]=mp(i,1);}
inline int find_set(vpii& st,int x){int y=x,z;whl(y!=st[y].x)y=st[y].x;whl(x!=st[x].x)z=st[x].x,st[x].x=y,x=z;rtn y;}
inline bool union_set(vpii& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?(st[a].y>st[b].y?st[a].x=b,st[a].y+=st[b].y:st[b].x=a,st[b].y+=st[a].y),true:false;}
template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.ins(*b.begin()),b.ers(b.begin());}

//初始化;
struct Initializer{
#ifndef DEBUG
Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#else
~Initializer(){runtime();}
#endif
}initializer;

//非标准;
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
typedef __gnu_cxx::rope<char> rope;
template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};

int n,k;
bool gao(vi c,int mn,int& ans)
{
	int t=0;
	rep(i,n) if (c[i]<mn) t+=mn-c[i],c[i]=mn;
	if (t>k) rtn false;
	t=k;
	rep(i,n)
	{
		if (i<n-1&&c[i]>mn&&(c[i]-c[i+1])*(i+1)<t)
			t-=(c[i]-c[i+1])*(i+1);
		else
		{
			ans=max(c[i]-(t+i)/(i+1),c[i+1])-mn;
			rtn true;
		}
	}
}

int main()
{
	cin>>n>>k;
	if (n==1) {cout<<0<<endl;rtn 0;}
	vi c(n);
	cin>>c;
	srt(c);
	rev(c);
	int l=1,r=1000000001;
	while (l+1!=r)
	{
		int m=(l+r)/2,_;
		if (gao(c,m,_)) l=m;
		else r=m;
//		cout<<l<<","<<r<<endl;
	}
	int ans;
	gao(c,l,ans);
	cout<<ans<<endl;
}


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <utility>
#include <vector>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
using namespace std;

//用于减少代码量的宏;
#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=(b);--i)
#define rrepf(i,a,b) fdt(i,(a)-1,b)
#define rep(i,n) repf(i,0,n)
#define rrep(i,n) rrepf(i,n,0)
#define for_each(e,s) for (__typeof__((s).begin()) e=(s).begin();e!=(s).end();++e)
#define for_nonempty_subsets(subset,set) for (int subset=set;subset;subset=(subset-1)&(set))
#define for_in_charset(i,charset) for (cstr i=(charset);*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,char(0))
#define cpy(x,y) memcpy(x,y,sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x),(x).erase(unique(all(x)),(x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() (cerr)
#define input(in) freopen(in,"r",stdin)
#define output(out) freopen(out,"w",stdout)
#else
#define prt(x) cerr<<"Line "<<__LINE__<<": "<<#x" = "<<(x)<<endl
#define asrtWA(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtTLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtMLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtOLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtRE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define runtime() cerr<<"Used: "<<db(clock())/CLOCKS_PER_SEC<<"s"<<endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo=(~0u)>>1;
lli ooll=(~0ull)>>1;
db inf=1e+10;
db eps=1e-10;
db gam=0.5772156649015328606;
db pi=acos(-1.0);
int dx[]={1,0,-1,0,1,-1,-1,1,0};
int dy[]={0,1,0,-1,1,1,-1,-1,0};
int MOD=1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
template<typename type>inline type mod(const type& x){rtn x%MOD;}
inline int sgn(const db& x){rtn (x>+eps)-(x<-eps);}
inline int dbcmp(const db& a,const db& b){rtn sgn(a-b);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);}
template<typename type>inline pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);}
template<typename type>inline pr<type,type> operator*(const pr<type,type>& a,const type& b){rtn mp(a.x*b,a.y*b);}
template<typename type>inline pr<type,type> operator/(const pr<type,type>& a,const type& b){rtn mp(a.x/b,a.y/b);}
template<typename type>inline pr<type,type>& operator-=(pr<type,type>& a,const pr<type,type>& b){rtn a=a-b;}
template<typename type>inline pr<type,type>& operator+=(pr<type,type>& a,const pr<type,type>& b){rtn a=a+b;}
template<typename type>inline pr<type,type>& operator*=(pr<type,type>& a,const type& b){rtn a=a*b;}
template<typename type>inline pr<type,type>& operator/=(pr<type,type>& a,const type& b){rtn a=a/b;}
template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.y-a.y*b.x;}
template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.x+a.y*b.y;}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
inline lli bin_pow(lli x,lli y){lli z=1;whl(y){if(y&1)z=mod(z*x);x=mod(sqr(x)),y>>=1;}rtn z;}
template<typename istream,typename first_type,typename second_type>inline istream& operator>>(istream& cin,pr<first_type,second_type>& x){rtn cin>>x.x>>x.y;}
template<typename ostream,typename first_type,typename second_type>inline ostream& operator<<(ostream& cout,const pr<first_type,second_type>& x){rtn cout<<x.x<<" "<<x.y;}
template<typename istream,typename type>inline istream& operator>>(istream& cin,vec<type>& x){rep(i,sz(x))cin>>x[i];rtn cin;}
template<typename ostream,typename type>inline ostream& operator<<(ostream& cout,const vec<type>& x){rep(i,sz(x))cout<<x[i]<<(i+1==sz(x)?"":" ");rtn cout;}
inline ostream& pdb(int prcs,db x){rtn cout<<setprecision(prcs)<<fixed<<(sgn(x)?(x):0);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));rrep(i,y){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& st){rep(i,sz(st))st[i]=i;}
inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;}
inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;}
inline void make_set(vpii& st){rep(i,sz(st))st[i]=mp(i,1);}
inline int find_set(vpii& st,int x){int y=x,z;whl(y!=st[y].x)y=st[y].x;whl(x!=st[x].x)z=st[x].x,st[x].x=y,x=z;rtn y;}
inline bool union_set(vpii& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?(st[a].y>st[b].y?st[a].x=b,st[a].y+=st[b].y:st[b].x=a,st[b].y+=st[a].y),true:false;}
template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.ins(*b.begin()),b.ers(b.begin());}

//初始化;
struct Initializer{
#ifndef DEBUG
Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#else
~Initializer(){runtime();}
#endif
}initializer;

//非标准;
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
typedef __gnu_cxx::rope<char> rope;
template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};

int n,k;
bool gao(vi& c,int mn)
{
	int t=0,cnt=0;
	rep(i,n) if (c[i]<=mn) t+=mn-c[i],cnt++;
	if (t>k) rtn false;
	rep(i,n) if (c[i]>mn) t-=c[i]-mn;
	rtn t<=(cnt?cnt-1:0);
}
bool gao(vi& c,int mx,int mn,int t,int cnt)
{
	int s=0;
	rep(i,n) if (c[i]>mx) s+=c[i]-mx;
	if (s<=t) rtn true;
	else
	{
		if (cnt)
		{
			if (mx>mn&&s<=t+cnt-1&&s<=k) rtn true;
			else rtn false;
		}
		else rtn false;
	}
}
int solve(vi& c,int mn)
{
	int t=0,cnt=0;
	rep(i,n) if (c[i]<=mn) t+=mn-c[i],cnt++;
	int l=mn-1,r=1000000000;
	while (l+1!=r)
	{
		int m=(l+r)/2;
		if (gao(c,m,mn,t,cnt)) r=m;
		else l=m;
	}
	return r-mn;
}

int main()
{
	cin>>n>>k;
	if (n==1) {cout<<0<<endl;rtn 0;}
	vi c(n);
	cin>>c;
	srt(c);
	rev(c);
	int l=1,r=1000000001;
	while (l+1!=r)
	{
		int m=(l+r)/2;
		if (gao(c,m)) l=m;
		else r=m;
	}
	int ans=solve(c,l);
	cout<<ans<<endl;
}


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <utility>
#include <vector>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
using namespace std;

//用于减少代码量的宏;
#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=(b);--i)
#define rrepf(i,a,b) fdt(i,(a)-1,b)
#define rep(i,n) repf(i,0,n)
#define rrep(i,n) rrepf(i,n,0)
#define for_each(e,s) for (__typeof__((s).begin()) e=(s).begin();e!=(s).end();++e)
#define for_nonempty_subsets(subset,set) for (int subset=set;subset;subset=(subset-1)&(set))
#define for_in_charset(i,charset) for (cstr i=(charset);*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,char(0))
#define cpy(x,y) memcpy(x,y,sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x),(x).erase(unique(all(x)),(x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() (cerr)
#define input(in) freopen(in,"r",stdin)
#define output(out) freopen(out,"w",stdout)
#else
#define prt(x) cerr<<"Line "<<__LINE__<<": "<<#x" = "<<(x)<<endl
#define asrtWA(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtTLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtMLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtOLE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define asrtRE(s) do if(!(s))cerr<<"Assertion failed: "#s<<endl;whl(0)
#define runtime() cerr<<"Used: "<<db(clock())/CLOCKS_PER_SEC<<"s"<<endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo=(~0u)>>1;
lli ooll=(~0ull)>>1;
db inf=1e+10;
db eps=1e-10;
db gam=0.5772156649015328606;
db pi=acos(-1.0);
int dx[]={1,0,-1,0,1,-1,-1,1,0};
int dy[]={0,1,0,-1,1,1,-1,-1,0};
int MOD=1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
template<typename type>inline type mod(const type& x){rtn x%MOD;}
inline int sgn(const db& x){rtn (x>+eps)-(x<-eps);}
inline int dbcmp(const db& a,const db& b){rtn sgn(a-b);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);}
template<typename type>inline pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);}
template<typename type>inline pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);}
template<typename type>inline pr<type,type> operator*(const pr<type,type>& a,const type& b){rtn mp(a.x*b,a.y*b);}
template<typename type>inline pr<type,type> operator/(const pr<type,type>& a,const type& b){rtn mp(a.x/b,a.y/b);}
template<typename type>inline pr<type,type>& operator-=(pr<type,type>& a,const pr<type,type>& b){rtn a=a-b;}
template<typename type>inline pr<type,type>& operator+=(pr<type,type>& a,const pr<type,type>& b){rtn a=a+b;}
template<typename type>inline pr<type,type>& operator*=(pr<type,type>& a,const type& b){rtn a=a*b;}
template<typename type>inline pr<type,type>& operator/=(pr<type,type>& a,const type& b){rtn a=a/b;}
template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.y-a.y*b.x;}
template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.x+a.y*b.y;}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
inline lli bin_pow(lli x,lli y){lli z=1;whl(y){if(y&1)z=mod(z*x);x=mod(sqr(x)),y>>=1;}rtn z;}
template<typename istream,typename first_type,typename second_type>inline istream& operator>>(istream& cin,pr<first_type,second_type>& x){rtn cin>>x.x>>x.y;}
template<typename ostream,typename first_type,typename second_type>inline ostream& operator<<(ostream& cout,const pr<first_type,second_type>& x){rtn cout<<x.x<<" "<<x.y;}
template<typename istream,typename type>inline istream& operator>>(istream& cin,vec<type>& x){rep(i,sz(x))cin>>x[i];rtn cin;}
template<typename ostream,typename type>inline ostream& operator<<(ostream& cout,const vec<type>& x){rep(i,sz(x))cout<<x[i]<<(i+1==sz(x)?"":" ");rtn cout;}
inline ostream& pdb(int prcs,db x){rtn cout<<setprecision(prcs)<<fixed<<(sgn(x)?(x):0);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));rrep(i,y){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& st){rep(i,sz(st))st[i]=i;}
inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;}
inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;}
inline void make_set(vpii& st){rep(i,sz(st))st[i]=mp(i,1);}
inline int find_set(vpii& st,int x){int y=x,z;whl(y!=st[y].x)y=st[y].x;whl(x!=st[x].x)z=st[x].x,st[x].x=y,x=z;rtn y;}
inline bool union_set(vpii& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?(st[a].y>st[b].y?st[a].x=b,st[a].y+=st[b].y:st[b].x=a,st[b].y+=st[a].y),true:false;}
template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.ins(*b.begin()),b.ers(b.begin());}

//初始化;
struct Initializer{
#ifndef DEBUG
Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#else
~Initializer(){runtime();}
#endif
}initializer;

//非标准;
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
typedef __gnu_cxx::rope<char> rope;
template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{};

int n,k;
bool gao(vi c,int mn,int& ans)
{
	int t=0;
	rep(i,n) if (c[i]<mn) t+=mn-c[i],c[i]=mn;
	if (t>k) rtn false;
	if (t==0)
	{
		ans=c.front()-c.back();
		rtn true;
	}
	rep(i,n)
	{
		if (c[i]==mn) rtn false;
		if ((c[i]-c[i+1])*(i+1)<t)
			t-=(c[i]-c[i+1])*(i+1);
		else
		{
			ans=(c[i]-(t+i)/(i+1))-mn;
			rtn true;
		}
	}
}

int main()
{
	cin>>n>>k;
	if (n==1) {cout<<0<<endl;rtn 0;}
	vi c(n);
	cin>>c;
	srt(c);
	rev(c);
	int l=1,r=1000000001;
	while (l+1!=r)
	{
		int m=(l+r)/2,_;
		if (gao(c,m,_)) l=m;
		else r=m;
	}
	int ans;
	gao(c,l,ans);
	cout<<ans<<endl;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll n,k;
ll a[100005];

bool check(ll mx){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(ll mi){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

ll erfen2(ll l,ll r){
    while(l<r){
        ll mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

ll erfen(ll l,ll r){
    while(l<r){
        ll mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    ll ans1=erfen(a[0],a[n-1]);
    ll ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll n,k;
ll a[100005];

bool check(ll mx){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(ll mi){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

ll erfen2(ll l,ll r){
    while(l<=r){
        ll mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

ll erfen(ll l,ll r){
    while(l<=r){
        ll mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    ll ans1=erfen(a[0],a[n-1]);
    ll ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int n,k;
int a[100005];

bool check(int mx){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(int mi){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

int erfen2(int l,int r){
    while(l<r){
        int mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

int erfen(int l,int r){
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    int ans1=erfen(a[0],a[n-1]);
    int ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int n,k;
int a[100005];

bool check(int mx){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(int mi){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

int erfen2(int l,int r){
    while(l<r){
        int mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

int erfen(int l,int r){
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    int ans1=erfen(0,n-1);
    int ans2=erfen2(0,n-1);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll n,k;
ll a[500005];

bool check(ll mx){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(ll mi){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

ll erfen2(ll l,ll r){
    while(l<r){
        ll mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

ll erfen(ll l,ll r){
    while(l<r){
        ll mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    ll ans1=erfen(a[0],a[n-1]);
    ll ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int n,k;
int a[100005];

bool check(int mx){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(int mi){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=a[i]-mi;
        else   t2+=mi-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

int erfen(int l,int r){
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    int ans=erfen(0,n-1);
    cout<<ans<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int n,k;
int a[100005];

bool check(int mx){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(int mi){
    int t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

int erfen2(int l,int r){
    while(l<r){
        int mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

int erfen(int l,int r){
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    int ans1=erfen(a[0],a[n-1]);
    int ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll n,k;
ll a[500005];

bool check(ll mx){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]>mx) t1+=a[i]-mx;
        else   t2+=mx-a[i];
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

bool check2(ll mi){
    ll t1=0,t2=0;
    for(int i=0;i<n;i++){
        if(a[i]<mi) t1+=mi-a[i];
        else   t2+=a[i]-mi;
    }
    if(t1<=t2&&t1<=k) return true;
    else return false;
}

ll erfen2(ll l,ll r){
    while(l<r){
        ll mid=(l+r+1)/2;
        if(check2(mid)) l=mid;
        else r=mid-1;
    }
    return r;
}

ll erfen(ll l,ll r){
    while(l<r){
        ll mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    ll ans1=erfen(a[0],a[n-1]);
    ll ans2=erfen2(a[0],a[n-1]);
    cout<<ans1-ans2<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 500007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  ll i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  // for(i = 0; i < n; i++) {
  //   if(i == sad_small) sad_equilibrium += 1;
  //   sad_count += abs(wealths[i] - sad_equilibrium);
  // }
  // sad_count /= 2;
  // if(k >= sad_count) {
  //   cout << equilibrium << endl;
  //   return 0;
  // }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if(k == 0) {
    cout << wealths[n - 1] - wealths[0] << endl;
    return 0;
  }

  ll chunk;

  ll k_orig = k;
  ll iters;
  //take k
  ll count = 1;
  ll index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

int n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

int n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  sort(wealths, wealths + n);

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
  }

  ll k_orig = k;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 500007
#define MOD
#define INF

typedef long long int ll;

int n;
int k;
int wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  int equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  // ll sad_equilibrium = total / n;
  // ll sad_small = total % n;
  // sad_small = n - sad_small;

  // ll sad_count = 0;

  sort(wealths, wealths + n);

  // for(i = 0; i < n; i++) {
  //   if(i == sad_small) sad_equilibrium += 1;
  //   sad_count += abs(wealths[i] - sad_equilibrium);
  // }
  // sad_count /= 2;
  // if(k >= sad_count) {
  //   cout << equilibrium << endl;
  //   return 0;
  // }

  int large = wealths[n - 1];
  int small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if(k == 0) {
    cout << wealths[n - 1] - wealths[0] << endl;
    return 0;
  }

  ll chunk;

  int k_orig = k;
  int iters;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    large -= iters;
    break;
  }

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
    return 0;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  ll i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  ll count = 1;
  ll index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  // if(ungraceful_flag == 1) {
  //cout << equilibrium << endl;
  //}
  //else {
  cout << (large - small) << endl;
    //}

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 500007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  ll i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if(k == 0) {
    cout << wealths[n - 1] - wealths[0] << endl;
    return 0;
  }

  ll chunk;

  ll k_orig = k;
  ll iters;
  //take k
  ll count = 1;
  ll index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 500007
#define MOD
#define INF

typedef long long int ll;

int n;
int k;
int wealths[LIM];

int main() {
  scanf("%d %d", &n, &k);
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    scanf("%d", &wealths[i]);
    total += wealths[i];
  }
  int equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  // ll sad_equilibrium = total / n;
  // ll sad_small = total % n;
  // sad_small = n - sad_small;

  // ll sad_count = 0;

  sort(wealths, wealths + n);

  // for(i = 0; i < n; i++) {
  //   if(i == sad_small) sad_equilibrium += 1;
  //   sad_count += abs(wealths[i] - sad_equilibrium);
  // }
  // sad_count /= 2;
  // if(k >= sad_count) {
  //   cout << equilibrium << endl;
  //   return 0;
  // }

  int large = wealths[n - 1];
  int small = wealths[0];

  if(n == 1) {
    printf("0\n");
    return 0;
  }

  if(k == 0) {
    printf("%d\n", wealths[n - 1] - wealths[0]);
    return 0;
  }

  ll chunk;

  int k_orig = k;
  int iters;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    large -= iters;
    break;
  }

  if(ungraceful_flag == 1) {
    printf("%d\n", equilibrium);
    return 0;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    printf("%d\n", equilibrium);
  }
  else {
    printf("%d\n", large - small);
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

int n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;
  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  sort(wealths, wealths + n);

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 500007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
int wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  // ll sad_equilibrium = total / n;
  // ll sad_small = total % n;
  // sad_small = n - sad_small;

  // ll sad_count = 0;

  sort(wealths, wealths + n);

  // for(i = 0; i < n; i++) {
  //   if(i == sad_small) sad_equilibrium += 1;
  //   sad_count += abs(wealths[i] - sad_equilibrium);
  // }
  // sad_count /= 2;
  // if(k >= sad_count) {
  //   cout << equilibrium << endl;
  //   return 0;
  // }

  int large = wealths[n - 1];
  int small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if(k == 0) {
    cout << wealths[n - 1] - wealths[0] << endl;
    return 0;
  }

  ll chunk;

  ll k_orig = k;
  ll iters;
  //take k
  int count = 1;
  int index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

int n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  int i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  sort(wealths, wealths + n);

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  int count = 1;
  int index = n - 2;
  int ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  ll i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if(k == 0) {
    cout << wealths[n - 1] - wealths[0] << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  ll count = 1;
  ll index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  // if(ungraceful_flag == 1) {
  //cout << equilibrium << endl;
  //}
  //else {
  cout << (large - small) << endl;
    //}

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <string>
using namespace std;

#define LIM 100007
#define MOD
#define INF

typedef long long int ll;

ll n;
ll k;
ll wealths[LIM];

int main() {
  cin >> n >> k;
  ll i;

  ll total = 0;

  for(i = 0; i < n; i++) {
    cin >> wealths[i];
    total += wealths[i];
  }
  ll equilibrium;
  if(total % n == 0) equilibrium = 0;
  else equilibrium = 1;

  ll sad_equilibrium = total / n;
  ll sad_small = total % n;
  sad_small = n - sad_small;

  ll sad_count = 0;

  sort(wealths, wealths + n);

  for(i = 0; i < n; i++) {
    if(i == sad_small) sad_equilibrium += 1;
    sad_count += abs(wealths[i] - sad_equilibrium);
  }
  sad_count /= 2;
  if(k >= sad_count) {
    cout << equilibrium << endl;
    return 0;
  }

  ll large = wealths[n - 1];
  ll small = wealths[0];

  if(n == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll k_orig = k;
  //take k
  ll count = 1;
  ll index = n - 2;
  ll ungraceful_flag = 0;

  while(1) {
    if(index < 0) {
      ungraceful_flag = 1;
      break; //ungraceful

    }
    ll chunk = large - wealths[index];
    chunk *= count;
    if(chunk <= k) {
      large = wealths[index];
      k -= chunk;
      count += 1;
      index -= 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    large -= iters;
    break;
  }

  // add k
  k = k_orig;
  index = 1;
  count = 1;
  while(1) {
    if(index >= n) {
      ungraceful_flag = 1;
      break; //ungraceful
    }
    ll chunk = wealths[index] - small;
    chunk *= count;
    if(chunk <= k) {
      small = wealths[index];
      k -= chunk;
      count += 1;
      index += 1;

      continue;
    }
    chunk /= count;
    ll iters = (k / count);
    small += iters;
    break;
  }

  if(small >= large) ungraceful_flag = 1;

  if(ungraceful_flag == 1) {
    cout << equilibrium << endl;
  }
  else {
    cout << (large - small) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

ll ss[600600];
int check1(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return (ss[n] - ss[pos] - (n - pos) * 1ll * x) <= k;
}
int check2(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return ((pos+1) * 1ll * x - ss[pos + 1] ) <= k;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }

	sort(c, c + n);
	for(int i = 0; i < n; i++){
    	ss[i + 1] = ss[i] + c[i];
    }
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

ll ss[600600];
int check1(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return (ss[n] - s[pos] - (n - pos) * 1ll * x) <= k;
}
int check2(ll x, ll k){

	int pos = lower_bound(c, c + n, x) - c;
	return (ss[pos + 1] - (pos+1) * 1ll * x) <= k;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }
    for(int i = 0; i < n; i++){
    	ss[i + 1] = ss[i] + a[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

int check1(ll x, ll k){
	ll ans = 0;
	for(int j = 0; j < n; j++){
		ans += max(0ll, c[j] * 1ll - x);
	}
	if(ans <= k) return 1;
	return 0;
}
int check2(ll x, ll k){
	ll ans = 0;
	for(int j = 0; j < n; j++){
			ans += max(0ll, x - c[j] * 1ll);
	}
	if(ans <= k) return 1;
	return 0;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	scanf("%d", &c[i]);
    	sum += c[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

ll ss[600600];
int check1(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return (ss[n] - ss[pos] - (n - pos) * 1ll * x) <= k;
}
int check2(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return ((pos+1) * 1ll * x - ss[pos + 1] ) <= k;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }
    for(int i = 0; i < n; i++){
    	ss[i + 1] = ss[i] + c[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

ll ss[600600];
int check1(ll x, ll k){
	int pos = lower_bound(c, c + n, x) - c;
	return (ss[n] - ss[pos] - (n - pos) * 1ll * x) <= k;
}
int check2(ll x, ll k){

	int pos = lower_bound(c, c + n, x) - c;
	return (ss[pos + 1] - (pos+1) * 1ll * x) <= k;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }
    for(int i = 0; i < n; i++){
    	ss[i + 1] = ss[i] + c[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

int check1(ll x, ll k){
	ll ans = 0;
	for(int j = n-1; j >=0; j--){
		ans += max(0ll, c[j] * 1ll - x);
		if(ans >k) return 0;
	}
	if(ans <= k) return 1;
	return 0;
}
int check2(ll x, ll k){
	ll ans = 0;
	for(int j = 0; j < n; j++){
		ans += max(0ll, x - c[j] * 1ll);
		if(ans >k) return 0;
	}
	if(ans <= k) return 1;
	return 0;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define pll pair < ll, ll >
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define vi vector < int >

const int inf = (int)1e9;
const int mod = (int) 1e9 + 7;
int c[500500];
int n;
ll sum = 0;
int k;

int check1(ll x, ll k){
	ll ans = 0;
	for(int j = 0; j < n; j++){
		ans += max(0ll, c[j] * 1ll - x);
	}
	if(ans <= k) return 1;
	return 0;
}
int check2(ll x, ll k){
	ll ans = 0;
	for(int j = 0; j < n; j++){
			ans += max(0ll, x - c[j] * 1ll);
	}
	if(ans <= k) return 1;
	return 0;
}

int main () {
    #ifdef LOCAL
    freopen ("a.in", "r", stdin);
    freopen ("a.out", "w", stdout);
    #endif
    cin >> n >> k;
    for(int i = 0; i < n; i++){
    	cin >> c[i];
    	sum += c[i];
    }
	sort(c, c + n);
	ll mx = sum/n;
	ll ans = 0;
	if(sum % n > 0) mx++;
	int l = mx, r = c[n-1], cur = c[n-1];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check1(mid, k)){
			r = mid-1;
			cur = mid;
		}else l = mid+1;
	}
	ans += cur;
	ll mi = sum/n;
	l = c[0], r = mi, cur = c[0];
	while(l <= r){
		int mid = (l+r)>>1;
		if(check2(mid, k)){
			l = mid+1;
			cur = mid;
		}else r = mid-1;
	}
	cout << ans - cur << endl;

    #ifdef LOCAL
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#define NDEBUG
#include <cassert>
using namespace std;

template<typename T> inline void debug_arr(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " =>";
    for (auto i : elem)
        cout << " " << i;
    cout << endl;
#endif
};
template<typename T> inline void debug(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " => " << elem << endl;
#endif
};

typedef long long LL;

vector<int> vc;
vector<int> vcmp;
vector<LL> vsum;

int bsearch(int b, int e, int k, bool isfindlow) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = isfindlow?mid+vc.front():mid+vcmp.back();
    LL sum=0, d=0;
    if (isfindlow) {
        auto p = lower_bound(vc.begin(),vc.end(),mval);
        d = distance(vc.begin(), p);
        if (d>0)
            sum = (mval*d) - vsum[d-1];
    } else {
        auto p = upper_bound(vc.begin(),vc.end(),mval);
        d = distance(vc.begin(), p);
        sum = vsum.back();
        if (d>0)
            sum -= vsum[d-1];
        d = distance(p, vc.end());
        sum -= d*mval;
    }

    if (e==b) {
        if (sum<=k) {
            return e;
        } else {
            return e==0?e:e-1;
        }
    }

    if (sum==k) {
        ret = mid;
    } else if(sum<k) {
        ret = isfindlow?bsearch(mid+1,e,k,isfindlow):bsearch(0,mid,k,isfindlow);
    } else {
        ret = isfindlow?bsearch(0,mid,k,isfindlow):bsearch(mid+1,e,k,isfindlow);
    }
    return ret;
}

int main()
{
#if !defined(NDEBUG)
    int TCASE = 7;
    while(TCASE-->0) {
#endif
    int N, K;
    int elem;

    vc.clear();
    vcmp.clear();
    vsum.clear();

    ios_base::sync_with_stdio(false);
    cin >> N >> K;
    for (int i=0; i<N; ++i) {
        cin >> elem;
        vc.push_back(elem);

    }

    sort(vc.begin(), vc.end());
    vsum.resize(vc.size(),vc.front());
    for (int i=1; i<N; ++i) {
        vsum[i] = vsum[i-1]+vc[i];
    }

    LL poor = vsum.back()/N;
    LL rich = vsum.back()%N;
    vcmp.resize(vc.size(),vsum.back()/N);
    for (int i=0; i<rich; ++i)
        vcmp[vcmp.size()-1-i]++;

    LL diff, diff_sum = 0;
    for (int i=0; i<vcmp.size(); ++i) {
        if ((diff=vcmp[i]-vc[i])<=0)
            break;

        diff_sum += diff;
    }

    if (K==0) {
        poor = vc.front();
        rich = vc.back();
    } else if (K>=diff_sum) {
        poor = vcmp.front();
        rich = vcmp.back();
    } else {
        if (rich!=0)
            rich = poor+1;
        else
            rich = poor;

        poor = bsearch(0, vcmp.front()-vc.front(), K, true) + vc.front();
        assert(poor<=vcmp.front());

        rich = bsearch(0, vc.back()-vcmp.back(), K, false) + vcmp.back();
        assert(rich<=vc.back());
    }

    cout << rich-poor << endl;
#if !defined(NDEBUG)
    }
#endif
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#define NDEBUG
#include <cassert>
using namespace std;

template<typename T> inline void debug_arr(const T& elem, const char* msg) {
#if defined(NDEBUG)
#else
    cout << "[debug] " << msg << " =>";
    for (auto i : elem)
        cout << " " << i;
    cout << endl;
#endif
};
template<typename T> inline void debug(const T& elem, const char* msg) {
#if defined(NDEBUG)
#else
    cout << "[debug] " << msg << " => " << elem << endl;
#endif
};

typedef long long LL;

vector<int> vc;
vector<LL> vcmp;
vector<int> rng;

int bsearch_lb(int b, int e, int k, bool isfindlow) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = isfindlow?mid+vc.front():mid+vcmp.back();
    int sum = 0;
    for (auto i : vc) {
        if ((isfindlow?i<mval:i>mval)) {
            sum += (isfindlow?mval-i:i-mval);
        }
    }

    if (e==b) {
        if (sum<=k) {
            return e;
        } else {
            return e==0?e:e-1;
        }
    }

    debug(mid, "mid is");
    if (sum==k) {
        debug(k, "sum=k, k is");
        ret = mid;
    } else if(sum<k) {
        debug(sum, "sum<k, sum is");
        ret = isfindlow?bsearch_lb(mid+1,e,k,isfindlow):bsearch_lb(0,mid,k,isfindlow);
    } else {
        debug(sum, "sum>k, sum is");
        ret = isfindlow?bsearch_lb(0,mid,k,isfindlow):bsearch_lb(mid+1,e,k,isfindlow);
    }
    return ret;
}

int main()
{
#if !defined(NDEBUG)
    int n = 7;
    while(n-->0) {
#endif
    int N, K;
    int elem;

    LL sum=0, sum_cmp=0;
    vc.clear();
    vcmp.clear();

    cin >> N >> K;
    for (int i=0; i<N; ++i) {
        cin >> elem;
        vc.push_back(elem);
        sum += elem;
    }

    sort(vc.begin(), vc.end());
    debug_arr(vc,"vc");

    debug(sum, "sum");
    LL poor = sum/N;
    LL rich = sum%N;
    vcmp.resize(vc.size(),sum/N);
    for (int i=0; i<rich; ++i)
        vcmp[vcmp.size()-1-i]++;
    debug_arr(vcmp,"vcmp");

    if (rich!=0)
        rich = poor+1;
    else
        rich = poor;

    poor = bsearch_lb(0, vcmp.front()-vc.front(), K, true) + vc.front();
    assert((poor>=vc.front() && poor<=vcmp.front()));

    rich = bsearch_lb(0, vc.back()-vcmp.back(), K, false) + vcmp.back();
    assert((rich>=vcmp.back() && rich<=vc.back()));

    cout << rich-poor << endl;
#if !defined(NDEBUG)
    }
#endif
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#define NDEBUG
#include <cassert>
using namespace std;

template<typename T> inline void debug_arr(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " =>";
    for (auto i : elem)
        cout << " " << i;
    cout << endl;
#endif
};
template<typename T> inline void debug(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " => " << elem << endl;
#endif
};

typedef long long LL;

vector<int> vc;
vector<int> vcmp;
vector<LL> vsum;

int bsearch_lo(int b, int e, int k) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = mid+vc.front();
    LL sum=0, d=0;

    auto p = lower_bound(vc.begin(),vc.end(),mval);
    d = distance(vc.begin(), p);
    if (d>0)
        sum = (mval*d) - vsum[d-1];

    if (e==b) {
        if (sum<=k) {
            return e;
        } else {
            return e==0?e:e-1;
        }
    }

    if (sum==k) {
        ret = mid;
    } else if(sum<k) {
        ret = bsearch_lo(mid+1,e,k);
    } else {
        ret = bsearch_lo(0,mid,k);
    }
    return ret;
}

int bsearch_hi(int b, int e, int k) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = mid+vcmp.back();
    LL sum=0, d=0;
    auto p = upper_bound(vc.begin(),vc.end(),mval);
    d = distance(vc.begin(), p);
    sum = vsum.back();
    if (d>0)
        sum -= vsum[d-1];
    d = distance(p, vc.end());
    sum -= d*mval;

    if (e==b) {
        if (sum<=k) {
            return e;
        } else {
            return e==0?e:e-1;
        }
    }

    if (sum==k) {
        ret = mid;
    } else if(sum<k) {
        ret = bsearch_hi(0,mid,k);
    } else {
        ret = bsearch_hi(mid+1,e,k);
    }
    return ret;
}

int main()
{
#if !defined(NDEBUG)
    int TCASE = 7;
    while(TCASE-->0) {
#endif
    int N, K;
    int elem;

    vc.clear();
    vcmp.clear();
    vsum.clear();

    cin >> N >> K;
    for (int i=0; i<N; ++i) {
        cin >> elem;
        vc.push_back(elem);
    }

    sort(vc.begin(), vc.end());
    vsum.resize(vc.size());
    vsum.front() = vc.front();
    for (int i=1; i<vc.size(); ++i)
        vsum[i] = vsum[i-1] + vc[i];

    LL poor = vsum.back()/N;
    LL rich = vsum.back()%N;
    vcmp.resize(vc.size(),vsum.back()/N);
    for (int i=0; i<rich; ++i)
        vcmp[vcmp.size()-1-i]++;

    if (rich!=0)
        rich = poor+1;
    else
        rich = poor;

    poor = bsearch_lo(0, vcmp.front()-vc.front(), K) + vc.front();
    assert(poor<=vcmp.front());

    rich = bsearch_hi(0, vc.back()-vcmp.back(), K) + vcmp.back();
    assert(rich<=vc.back());

    cout << rich-poor << endl;
#if !defined(NDEBUG)
    }
#endif
}


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

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> veci;
#define fi first
#define se second
#define eps 1e12
#define INF (1e9+7)

int n, k;
veci vc;

int ksearch(int l, int r, bool plus) {
    if (l==r) {
        return (plus?l-1:l+1);
    }

    int mid = ((ll)l+r)>>1;

    ll csum = 0;
    if (plus) {
        auto it = lower_bound(vc.begin(),vc.end(),mid);
        auto p = vc.begin();
        if (it!=vc.end()) {
            while (p!=it) {
                csum += mid-(*it);
                --it;
            }
            csum += mid-(*p);
        }
    } else {
        auto it = upper_bound(vc.begin(),vc.end(),mid);
        auto p = vc.end();
        while (p!=it) {
            csum += (*it)-mid;
            ++it;
        }
    }

    int res;
    if ((ll)k<csum) {
        res = plus?ksearch(l,mid,plus):ksearch(mid+1,r,plus);
    } else if ((ll)k>csum) {
        res = plus?ksearch(mid+1,r,plus):ksearch(l,mid,plus);
    } else {
        res = mid;
    }

    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;

    vc.clear();
    vc.resize(n);
    for (int i=0; i<n; ++i) {
        cin >> vc[i];
    }
    sort(vc.begin(), vc.end());

    int res;
    if (n==1) {
        res = 0;
    } else if (k==0) {
        res = vc.back()-vc.front();
    } else {
        int plus = ksearch(vc.front(),vc.back(),1);
        int minus = ksearch(vc.front(),vc.back(),0);
        res = minus - plus;
        cout << "\tres=" << minus << " " << plus << endl;
    }

    cout << res << endl;
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#define NDEBUG
#include <cassert>
using namespace std;

template<typename T> inline void debug_arr(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " =>";
    for (auto i : elem)
        cout << " " << i;
    cout << endl;
#endif
};
template<typename T> inline void debug(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " => " << elem << endl;
#endif
};

typedef long long LL;

vector<int> vc;
vector<int> vcmp;

int bsearch(int b, int e, int k, bool isfindlow) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = isfindlow?mid+vc.front():mid+vcmp.back();
    LL sum = 0;
    if (isfindlow) {
        auto p = lower_bound(vc.begin(),vc.end(),mval);
        sum = accumulate(vc.begin(), p, (LL)0, [=](LL i, int j){return i+(mval-j);});
    } else {
        auto p = upper_bound(vc.begin(),vc.end(),mval);
        sum = accumulate(p, vc.end(), (LL)0, [=](LL i, int j){return i+(j-mval);});
    }

    if (e==b) {
        if (sum<=k) {
            debug(e, "sol. ret is");
            debug(sum, "sol. sum<=k, sum is");
            return e;
        } else {
            debug(e, "sol. ret is");
            debug(sum, "sol. sum>k, sum is");
            return e==0?e:e-1;
        }
    }

    debug(mid, "mid is");
    debug(mval, "mval is");
    if (sum==k) {
        debug(mid, "sol. ret is");
        debug(k, "sum=k, k is");
        ret = mid;
    } else if(sum<k) {
        debug(sum, "sum<k, sum is");
        ret = isfindlow?bsearch(mid+1,e,k,isfindlow):bsearch(0,mid,k,isfindlow);
    } else {
        debug(sum, "sum>k, sum is");
        ret = isfindlow?bsearch(0,mid,k,isfindlow):bsearch(mid+1,e,k,isfindlow);
    }
    return ret;
}

int main()
{
#if !defined(NDEBUG)
    int TCASE = 5;
    while(TCASE-->0) {
#endif
    int N, K;
    int elem;

    LL sum=0, sum_cmp=0;
    vc.clear();
    vcmp.clear();

    cin >> N >> K;
    for (int i=0; i<N; ++i) {
        cin >> elem;
        vc.push_back(elem);
        sum += elem;
    }

    sort(vc.begin(), vc.end());
    debug_arr(vc,"vc");

    debug(sum, "sum");
    LL poor = sum/N;
    LL rich = sum%N;
    vcmp.resize(vc.size(),sum/N);
    for (int i=0; i<rich; ++i)
        vcmp[vcmp.size()-1-i]++;
    debug_arr(vcmp,"vcmp");

    if (rich!=0)
        rich = poor+1;
    else
        rich = poor;

    poor = bsearch(0, vcmp.front()-vc.front(), K, true) + vc.front();
    assert(poor<=vcmp.front());

    rich = bsearch(0, vc.back()-vcmp.back(), K, false) + vcmp.back();
    assert(rich<=vc.back());

    cout << rich-poor << endl;
#if !defined(NDEBUG)
    }
#endif
}


#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#define NDEBUG
#include <cassert>
using namespace std;

template<typename T> inline void debug_arr(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " =>";
    for (auto i : elem)
        cout << " " << i;
    cout << endl;
#endif
};
template<typename T> inline void debug(const T& elem, const char* msg) {
#if !defined(NDEBUG)
    cout << "[debug] " << msg << " => " << elem << endl;
#endif
};

typedef long long LL;

vector<int> vc;
vector<int> vcmp;

int bsearch(int b, int e, int k, bool isfindlow) {
    int ret;
    int mid = b+((e-b)/2);
    int mval = isfindlow?mid+vc.front():mid+vcmp.back();
    LL sum = 0;
    if (isfindlow) {
        auto p = lower_bound(vc.begin(),vc.end(),mval);
        sum = accumulate(vc.begin(), p, 0, [=](LL i, int j){return i+(LL)(mval-j);});
    } else {
        auto p = upper_bound(vc.begin(),vc.end(),mval);
        sum = accumulate(p, vc.end(), 0, [=](LL i, int j){return i+(LL)(j-mval);});
    }

    if (e==b) {
        if (sum<=k) {
            debug(e, "sol. ret is");
            debug(sum, "sol. sum<=k, sum is");
            return e;
        } else {
            debug(e, "sol. ret is");
            debug(sum, "sol. sum>k, sum is");
            return e==0?e:e-1;
        }
    }

    debug(mid, "mid is");
    debug(mval, "mval is");
    if (sum==k) {
        debug(mid, "sol. ret is");
        debug(k, "sum=k, k is");
        ret = mid;
    } else if(sum<k) {
        debug(sum, "sum<k, sum is");
        ret = isfindlow?bsearch(mid+1,e,k,isfindlow):bsearch(0,mid,k,isfindlow);
    } else {
        debug(sum, "sum>k, sum is");
        ret = isfindlow?bsearch(0,mid,k,isfindlow):bsearch(mid+1,e,k,isfindlow);
    }
    return ret;
}

int main()
{
#if !defined(NDEBUG)
    int TCASE = 5;
    while(TCASE-->0) {
#endif
    int N, K;
    int elem;

    LL sum=0, sum_cmp=0;
    vc.clear();
    vcmp.clear();

    cin >> N >> K;
    for (int i=0; i<N; ++i) {
        cin >> elem;
        vc.push_back(elem);
        sum += elem;
    }

    sort(vc.begin(), vc.end());
    debug_arr(vc,"vc");

    debug(sum, "sum");
    LL poor = sum/N;
    LL rich = sum%N;
    vcmp.resize(vc.size(),sum/N);
    for (int i=0; i<rich; ++i)
        vcmp[vcmp.size()-1-i]++;
    debug_arr(vcmp,"vcmp");

    if (rich!=0)
        rich = poor+1;
    else
        rich = poor;

    poor = bsearch(0, vcmp.front()-vc.front(), K, true) + vc.front();
    assert(poor<=vcmp.front());

    rich = bsearch(0, vc.back()-vcmp.back(), K, false) + vcmp.back();
    assert(rich<=vc.back());

    cout << rich-poor << endl;
#if !defined(NDEBUG)
    }
#endif
}


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		//srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll a, b;
ll p[31111111];
ll c[18];

ll pal( ll x ) {
    ll i = 1;
    ll res = 0;
    while( x >= c[i] ) {
        res += 9 * c[( i - 1 ) / 2];
        i ++;
    }
    for( ll i1 = 1; i1 <= 9; i1 ++ ) {
        ll y;
        if( i <= 2 ) {
            if( i == 1 )
                y = i1;
            else
                y = i1 * 10 + i1;
            if( y <= x )
                res ++;
        }
        else
        for( ll i2 = 0; i2 <= 9; i2 ++ ) {
            if( i <= 4 ) {
                if( i == 3 )
                    y = i1 * 100 + i2 * 10 + i1;
                else
                    y = i1 * 1000 + i2 * 100 + i2 * 10 + i1;
                if( y <= x )
                    res ++;
            }
            else
            for( ll i3 = 0; i3 <= 9; i3 ++ ) {
                if( i <= 6 ) {
                    if( i == 5 )
                        y = i1 * 10000 + i2 * 1000 + i3 * 100 + i2 * 10 + i1;
                    else
                        y = i1 * 100000 + i2 * 10000 + i3 * 1000 + i3 * 100 + i2 * 10 + i1;
                    if( y <= x )
                        res ++;
                }
                else
                for( ll i4 = 0; i4 <= 9; i4 ++ ) {
                    if( i <= 8 ) {
                        if( i == 7 )
                            y = i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        else
                            y = i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        if( y <= x )
                            res ++;
                    }
                }
            }
        }
    }
    return res;
}

bool check( ll x ) {
    return p[x] * b <= a * pal( x );
}

int main( void ) {
    ll m = 20000000;
    c[0] = 1;
    forn( i, 17 ) {
        c[i + 1] = c[i] * 10;
    }
    for( ll i = 2; i <= m; i ++ ) {
        if( p[i] ) {
            p[i] = p[i - 1];
            continue;
        }
        for( ll j = i * i; j <= m; j += i )
            p[j] = 1;
        p[i] = p[i - 1] + 1;
    }
    cin >> a >> b;
    ll l = 0;
    ll r = m;
    while( r - l > 1 ) {
        ll s = ( r + l ) / 2;
        if( check( s ) ) {
            l = s;
        } else {
            r = s;
        }
    }
    cout << ( check( r ) ? r : l ) << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		//srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll a, b;
ll p[31111111];
ll c[18];

ll pal( ll x ) {
    ll i = 1;
    ll res = 0;
    while( x >= c[i] ) {
        res += 9 * c[( i - 1 ) / 2];
        i ++;
    }
    for( ll i1 = 1; i1 <= 9; i1 ++ ) {
        ll y;
        if( i <= 2 ) {
            if( i == 1 )
                y = i1;
            else
                y = i1 * 10 + i1;
            if( y <= x )
                res ++;
        }
        else
        for( ll i2 = 0; i2 <= 9; i2 ++ ) {
            if( i <= 4 ) {
                if( i == 3 )
                    y = i1 * 100 + i2 * 10 + i1;
                else
                    y = i1 * 1000 + i2 * 100 + i2 * 10 + i1;
                if( y <= x )
                    res ++;
            }
            else
            for( ll i3 = 0; i3 <= 9; i3 ++ ) {
                if( i <= 6 ) {
                    if( i == 5 )
                        y = i1 * 10000 + i2 * 1000 + i3 * 100 + i2 * 10 + i1;
                    else
                        y = i1 * 100000 + i2 * 10000 + i3 * 1000 + i3 * 100 + i2 * 10 + i1;
                    if( y <= x )
                        res ++;
                }
                else
                for( ll i4 = 0; i4 <= 9; i4 ++ ) {
                    if( i <= 8 ) {
                        if( i == 7 )
                            y = i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        else
                            y = i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        if( y <= x )
                            res ++;
                    }
                }
            }
        }
    }
    return res;
}

bool check( ll x ) {
    return p[x] * b <= a * pal( x );
}

int main( void ) {
    ll m = 5000000;
    c[0] = 1;
    forn( i, 17 ) {
        c[i + 1] = c[i] * 10;
    }
    for( ll i = 2; i <= m; i ++ ) {
        if( p[i] ) {
            p[i] = p[i - 1];
            continue;
        }
        for( ll j = i * i; j <= m; j += i )
            p[j] = 1;
        p[i] = p[i - 1] + 1;
    }
    cin >> a >> b;
    ll l = 0;
    ll r = m;
    while( r - l > 1 ) {
        ll s = ( r + l ) / 2;
        if( check( s ) ) {
            l = s;
        } else {
            r = s;
        }
    }
    cout << ( check( r ) ? r : l ) << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		//srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll a, b;
ll p[11111111];
vector< ll > pr;
ll q[11111111];
ll c[18];

ll pal( ll x ) {
    ll i = 1;
    ll res = 0;
    while( x >= c[i] ) {
        res += 9 * c[( i - 1 ) / 2];
        i ++;
    }
    for( ll i1 = 1; i1 <= 9; i1 ++ ) {
        ll y;
        if( i <= 2 ) {
            if( i == 1 )
                y = i1;
            else
                y = i1 * 10 + i1;
            if( y <= x )
                res ++;
        }
        else
        for( ll i2 = 0; i2 <= 9; i2 ++ ) {
            if( i <= 4 ) {
                if( i == 3 )
                    y = i1 * 100 + i2 * 10 + i1;
                else
                    y = i1 * 1000 + i2 * 100 + i2 * 10 + i1;
                if( y <= x )
                    res ++;
            }
            else
            for( ll i3 = 0; i3 <= 9; i3 ++ ) {
                if( i <= 6 ) {
                    if( i == 5 )
                        y = i1 * 10000 + i2 * 1000 + i3 * 100 + i2 * 10 + i1;
                    else
                        y = i1 * 100000 + i2 * 10000 + i3 * 1000 + i3 * 100 + i2 * 10 + i1;
                    if( y <= x )
                        res ++;
                }
                else
                for( ll i4 = 0; i4 <= 9; i4 ++ ) {
                    if( i <= 8 ) {
                        if( i == 7 )
                            y = i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        else
                            y = i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        if( y <= x )
                            res ++;
                    }
                }
            }
        }
    }
    return res;
}

bool check( ll x ) {
    return p[x] * b <= a * pal( x );
}

int main( void ) {
    c[0] = 1;
    forn( i, 17 ) {
        c[i + 1] = c[i] * 10;
    }
    for( ll i = 2; i <= 10000000; i ++ ) {
        if( p[i] ) {
            p[i] = p[i - 1];
            continue;
        }
        for( ll j = i * i; j <= 10000000; j += i )
            p[j] = 1;
        p[i] = p[i - 1] + 1;
        pr.pb( i );
    }
    cin >> a >> b;
    ll ans = 1;
    forn( i, pr.size() ) {
        if( b * p[pr[i] - 1] <= a * pal( pr[i] - 1 ) ) {
            ans = max( ans, pr[i] - 1 );
            //cout << pr[i] - 1 << " " <<p[pr[i] - 1] << " " << pal( pr[i] - 1 ) << endl;
        }
    }
    cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		//srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll a, b;
ll p[31111111];
ll c[18];

ll pal( ll x ) {
    ll i = 1;
    ll res = 0;
    while( x >= c[i] ) {
        res += 9 * c[( i - 1 ) / 2];
        i ++;
    }
    for( ll i1 = 1; i1 <= 9; i1 ++ ) {
        ll y;
        if( i <= 2 ) {
            if( i == 1 )
                y = i1;
            else
                y = i1 * 10 + i1;
            if( y <= x )
                res ++;
        }
        else
        for( ll i2 = 0; i2 <= 9; i2 ++ ) {
            if( i <= 4 ) {
                if( i == 3 )
                    y = i1 * 100 + i2 * 10 + i1;
                else
                    y = i1 * 1000 + i2 * 100 + i2 * 10 + i1;
                if( y <= x )
                    res ++;
            }
            else
            for( ll i3 = 0; i3 <= 9; i3 ++ ) {
                if( i <= 6 ) {
                    if( i == 5 )
                        y = i1 * 10000 + i2 * 1000 + i3 * 100 + i2 * 10 + i1;
                    else
                        y = i1 * 100000 + i2 * 10000 + i3 * 1000 + i3 * 100 + i2 * 10 + i1;
                    if( y <= x )
                        res ++;
                }
                else
                for( ll i4 = 0; i4 <= 9; i4 ++ ) {
                    if( i <= 8 ) {
                        if( i == 7 )
                            y = i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        else
                            y = i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        if( y <= x )
                            res ++;
                    }
                }
            }
        }
    }
    return res;
}

bool check( ll x ) {
    return p[x] * b <= a * pal( x );
}

int main( void ) {
    c[0] = 1;
    forn( i, 17 ) {
        c[i + 1] = c[i] * 10;
    }
    for( ll i = 2; i <= 30000000; i ++ ) {
        if( p[i] ) {
            p[i] = p[i - 1];
            continue;
        }
        for( ll j = i * i; j <= 30000000; j += i )
            p[j] = 1;
        p[i] = p[i - 1] + 1;
    }
    cin >> a >> b;
    ll l = 1;
    ll r = 30000000;
    while( r - l > 1 ) {
        ll s = ( r + l ) / 2;
        if( check( s ) ) {
            l = s;
        } else {
            r = s;
        }
    }
    cout << ( check( r ) ? r : l ) << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		//srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll a, b;
ll p[31111111];
ll c[18];

ll pal( ll x ) {
    ll i = 1;
    ll res = 0;
    while( x >= c[i] ) {
        res += 9 * c[( i - 1 ) / 2];
        i ++;
    }
    for( ll i1 = 1; i1 <= 9; i1 ++ ) {
        ll y;
        if( i <= 2 ) {
            if( i == 1 )
                y = i1;
            else
                y = i1 * 10 + i1;
            if( y <= x )
                res ++;
        }
        else
        for( ll i2 = 0; i2 <= 9; i2 ++ ) {
            if( i <= 4 ) {
                if( i == 3 )
                    y = i1 * 100 + i2 * 10 + i1;
                else
                    y = i1 * 1000 + i2 * 100 + i2 * 10 + i1;
                if( y <= x )
                    res ++;
            }
            else
            for( ll i3 = 0; i3 <= 9; i3 ++ ) {
                if( i <= 6 ) {
                    if( i == 5 )
                        y = i1 * 10000 + i2 * 1000 + i3 * 100 + i2 * 10 + i1;
                    else
                        y = i1 * 100000 + i2 * 10000 + i3 * 1000 + i3 * 100 + i2 * 10 + i1;
                    if( y <= x )
                        res ++;
                }
                else
                for( ll i4 = 0; i4 <= 9; i4 ++ ) {
                    if( i <= 8 ) {
                        if( i == 7 )
                            y = i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        else
                            y = i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 + i1;
                        if( y <= x )
                            res ++;
                    }
                }
            }
        }
    }
    return res;
}

bool check( ll x ) {
    x = pr[x] - 1;
    return p[x] * b <= a * pal( x );
}

int main( void ) {
    ll m = 30000000;
    c[0] = 1;
    forn( i, 17 ) {
        c[i + 1] = c[i] * 10;
    }
    for( ll i = 2; i <= m; i ++ ) {
        if( p[i] ) {
            p[i] = p[i - 1];
            continue;
        }
        for( ll j = i * i; j <= m; j += i )
            p[j] = 1;
        p[i] = p[i - 1] + 1;
    }
    cin >> a >> b;
    ll l = 0;
    ll r = pr.size() - 1;
    while( r - l > 1 ) {
        ll s = ( r + l ) / 2;
        if( check( s ) ) {
            l = s;
        } else {
            r = s;
        }
    }
    cout << ( check( r ) ? pr[r] : pr[l] ) - 1 << endl;
	return 0;
}