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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<long long int> vll;
typedef vector<char> vc;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
typedef string::iterator strit;
typedef vector<long long int>::iterator vllit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
const int EPS = 1e-6;

#define _  %  MOD
#define __ %= MOD

#define      each(it,s)        for(vit it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(int i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(int i = a; i >= (n); --i)
#define 	 pq(x)			   priority_queue<x,std::vector<x>,compare>
#define 	 rpq(x)			   priority_queue<x,std::vector<x>,compare>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vin(a,n) vi a;rep(i,n){int in;fri(in);a.pb(in);}
#define arrin(arr,n) rep(i,n){cin>>arr[i];}
#define vp(a) rep(i,a.size()){cout<<a[i]<<" ";}
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
/*
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    */
int main()
{
    fastio;
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int i=(int)sqrt(2*k)-1;
        int f=0;
        while((i*(i+1))/2<k)
        {
            i++;
            //f++;
        }
        //cout<<f<<endl;
        int d = i*(i+1)/2 - k;
        string s(n,'a');
        s[n-1-i]='b';
        s[n+d-i]='b';
        cout<<s<<endl;
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<long long int> vll;
typedef vector<char> vc;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
typedef string::iterator strit;
typedef vector<long long int>::iterator vllit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
const int EPS = 1e-6;

#define _  %  MOD
#define __ %= MOD

#define      each(it,s)        for(vit it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(int i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(int i = a; i >= (n); --i)
#define 	 pq(x)			   priority_queue<x,std::vector<x>,compare>
#define 	 rpq(x)			   priority_queue<x,std::vector<x>,compare>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vin(a,n) vi a;rep(i,n){int in;fri(in);a.pb(in);}
#define arrin(arr,n) rep(i,n){cin>>arr[i];}
#define vp(a) rep(i,a.size()){cout<<a[i]<<" ";}
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
/*
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    */
int main()
{
    fastio;
    int t;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        int i=((ll)sqrt(1+8*k)-1)/2;
        int f=0;
        while((i*(i+1))/2<k)
        {
            i++;
            //f++;
        }
        //cout<<f<<endl;
        ll d = i*(i+1)/2 - k;
        string s(n,'a');
        s[n-1-i]='b';
        s[n+d-i]='b';
        cout<<s<<endl;
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<long long int> vll;
typedef vector<char> vc;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
typedef string::iterator strit;
typedef vector<long long int>::iterator vllit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
const int EPS = 1e-6;

#define _  %  MOD
#define __ %= MOD

#define      each(it,s)        for(vit it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(int i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(int i = a; i >= (n); --i)
#define 	 pq(x)			   priority_queue<x,std::vector<x>,compare>
#define 	 rpq(x)			   priority_queue<x,std::vector<x>,compare>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vin(a,n) vi a;rep(i,n){int in;fri(in);a.pb(in);}
#define arrin(arr,n) rep(i,n){cin>>arr[i];}
#define vp(a) rep(i,a.size()){cout<<a[i]<<" ";}
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
/*
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    */
int main()
{
    fastio;
    int t;
    cin>>t;
    string a="";
    rep(i,1e5)
    {
        a+='a';
    }
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int i=(int)sqrt(2*k);
        while((i*(i+1))/2<k)
        {
            i++;
        }
        int d = i*(i+1)/2 - k;
        string s="";
        s+=a.substr(0,n-1-i);
        s+='b';
        s+=a.substr(0,d);
        s+='b';
        s+=a.substr(0,i-1-d);
        cout<<s<<endl;
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<long long int> vll;
typedef vector<char> vc;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
typedef string::iterator strit;
typedef vector<long long int>::iterator vllit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
const int EPS = 1e-6;

#define _  %  MOD
#define __ %= MOD

#define      each(it,s)        for(vit it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(int i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(int i = a; i >= (n); --i)
#define 	 pq(x)			   priority_queue<x,std::vector<x>,compare>
#define 	 rpq(x)			   priority_queue<x,std::vector<x>,compare>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vin(a,n) vi a;rep(i,n){int in;fri(in);a.pb(in);}
#define arrin(arr,n) rep(i,n){cin>>arr[i];}
#define vp(a) rep(i,a.size()){cout<<a[i]<<" ";}
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
/*
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    */
int main()
{
    fastio;
    int t;
    cin>>t;
    string a="";
    rep(i,1e5)
    {
        a+='a';
    }
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int i=(int)sqrt(2*k);
        while((i*(i+1))/2<k)
        {
            i++;
        }
        int d = i*(i+1)/2 - k;
        string s=a.substr(0,n);
        s[n-1-i]='b';
        s[n+d-i]='b';
        cout<<s<<endl;
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<long long int> vll;
typedef vector<char> vc;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
typedef string::iterator strit;
typedef vector<long long int>::iterator vllit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
const int EPS = 1e-6;

#define _  %  MOD
#define __ %= MOD

#define      each(it,s)        for(vit it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(int i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(int i = a; i >= (n); --i)
#define 	 pq(x)			   priority_queue<x,std::vector<x>,compare>
#define 	 rpq(x)			   priority_queue<x,std::vector<x>,compare>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vin(a,n) vi a;rep(i,n){int in;fri(in);a.pb(in);}
#define arrin(arr,n) rep(i,n){cin>>arr[i];}
#define vp(a) rep(i,a.size()){cout<<a[i]<<" ";}
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
/*
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    */
int main()
{
    fastio;
    int t;
    cin>>t;
    string a="";
    rep(i,1e5)
    {
        a+='a';
    }
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int i=1;
        while((i*(i+1))/2<k)
        {
            i++;
        }
        int d = i*(i+1)/2 - k;
        string s="";
        s+=a.substr(0,n-1-i);
        s+='b';
        s+=a.substr(0,d);
        s+='b';
        s+=a.substr(0,i-1-d);
        cout<<s<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{

    int t;
    cin>>t;
    for ( int _ = 0; _ < t; _++)
    {
        long long fir = 2;
        long long sec = 1;
        long long ans;
        int cnt = 0;
        int n,k;
        cin>>n>>k;
        while ( cnt < k )
        {
            ans = fir + sec;
            sec *= 2;
            if ( sec == fir )
            {
                sec = 1;
                fir *= 2;
            }
            cnt++;
        }
        int dig[1111];
        long long per = ans;
        int ptr = 0;
        while ( per > 0 )
        {
            dig[ptr++] = per % 2;
            per /= 2;
        }
        for ( int i = 0; i < ptr / 2; i++)
        {
            int p = dig[i];
            dig[i] = dig[ptr - i - 1];
            dig[ptr - i - 1] = p;
        }
        for (int i = 0; i < n - ptr; i++) cout<<"a";
        for (int i = 0; i < ptr;i++) if ( dig[i] == 0 ) cout<< "a";
        else cout<<"b";
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
long long ans[111111];

int main()
{
    long long fir = 2;
    long long sec = 1;
    int cnt = 1;
    while ( fir < 2000000000 )
    {
        ans[cnt++] = fir + sec;
        sec *= 2;
        if ( sec == fir )
        {
            sec = 1;
            fir *= 2;
        }
    }
    int t;
    cin>>t;
    for ( int _ = 0; _ < t; _++)
    {
        int n,k;
        cin>>n>>k;
        int dig[111];
        int per = ans[k];
        int ptr = 0;
        while ( per > 0 )
        {
            dig[ptr++] = per % 2;
            per /= 2;
        }
        for ( int i = 0; i < ptr / 2; i++)
        {
            int p = dig[i];
            dig[i] = dig[ptr - i - 1];
            dig[ptr - i - 1] = p;
        }
        for (int i = 0; i < n - ptr; i++) cout<<"a";
        for (int i = 0; i < ptr;i++) if ( dig[i] == 0 ) cout<< "a";
        else cout<<"b";
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main()
{

    int t;
    cin>>t;
    for ( int _ = 0; _ < t; _++)
    {
        int cnt = 1;
        int n,k;
        cin>>n>>k;
        int st1 = 1;
        int st2 = 0;
        while ( cnt < k )
        {
            st2++;
            if ( st1 == st2 )
            {
                st2 = 0;
                st1++;
            }
            cnt++;
        }
        cout<<string(n - st1 - 1,'a');
        string str = "b" + string(st1-st2-1, 'a') + "b" + string(st2, 'a');
        cout<<str<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
const int mlen = 63247;

long long need[mlen];

int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    need[1] = 0;
    need[2] = 1;
    for ( int i = 3; i < mlen; i++) need[i] = need[i-1] + i - 1;
    for ( int _ = 0; _ < t; _++)
    {
        int n,k;
        cin>>n>>k;
        int idx1, idx2;
        if ( k == 1 )
        {
            cout<<string(n - 2, 'a')<<"bb"<<endl;
            continue;
        }
        k--;
        for ( int i = mlen - 1; i > 0; i--) if ( need[i] <= k )
        {
            idx1 = i;
            break;
        }
        k -= need[idx1];
        idx2 = k;
        //cout<<idx1<<" "<<idx2<<endl;
        cout<<string(n - idx1-1, 'a') + "b" + string(idx1-idx2-1, 'a') + "b" + string(idx2, 'a')<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main()
{

    int t;
    cin>>t;
    for ( int _ = 0; _ < t; _++)
    {
        int cnt = 1;
        int n,k;
        cin>>n>>k;
        int st1 = 1;
        int st2 = 0;
        while ( cnt < k )
        {
            st2++;
            if ( st1 == st2 )
            {
                st2 = 0;
                st1++;
            }
            cnt++;
        }
        printf("%s",string(n - st1 - 1,'a'));
        string str = "b" + string(st1-st2-1, 'a') + "b" + string(st2, 'a');
        printf("%s",str);
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
long long ans[1111111];

int main()
{
    long long fir = 2;
    long long sec = 1;
    int cnt = 1;
    while ( fir <= 2000000000 )
    {
        ans[cnt++] = fir + sec;
        sec *= 2;
        if ( sec == fir )
        {
            sec = 1;
            fir *= 2;
        }
    }
    int t;
    cin>>t;
    for ( int _ = 0; _ < t; _++)
    {
        int n,k;
        cin>>n>>k;
        int dig[1111];
        long long per = ans[k];
        int ptr = 0;
        while ( per > 0 )
        {
            dig[ptr++] = per % 2;
            per /= 2;
        }
        for ( int i = 0; i < ptr / 2; i++)
        {
            int p = dig[i];
            dig[i] = dig[ptr - i - 1];
            dig[ptr - i - 1] = p;
        }
        for (int i = 0; i < n - ptr; i++) cout<<"a";
        for (int i = 0; i < ptr;i++) if ( dig[i] == 0 ) cout<< "a";
        else cout<<"b";
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
const int mlen = 111111;

long long need[mlen];

int main()
{

    int t;
    cin>>t;
    need[1] = 1;
    need[0] = 0;
    need[2] = 1;
    for ( int i = 3; i < mlen; i++) need[i] = need[i-1] + i - 1;
    for ( int _ = 0; _ < t; _++)
    {
        int cnt = 1;
        int n,k;
        cin>>n>>k;
        int idx1, idx2;
        if ( k == 1 )
        {
            cout<<string(n - 2, 'a')<<"bb"<<endl;
            continue;
        }
        for ( int i = mlen - 1; i >= 0; i--) if ( need[i] < k )
        {
            idx1 = i;
            break;
        }
        for ( int i = idx1; i >= 0; i--) if ( need[i] < k - need[idx1] )
        {
            idx2 = i;
            break;
        }
        cout<<idx1<<" "<<idx2<<endl;
        cout<<string(n - idx1-1, 'a') + "b" + string(idx1-idx2-1, 'a') + "b" + string(idx2, 'a')<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
const int mlen = 111111;

long long need[mlen];

int main()
{

    int t;
    cin>>t;
    need[1] = 1;
    need[0] = 0;
    need[2] = 1;
    for ( int i = 3; i < mlen; i++) need[i] = need[i-1] + i - 1;
    for ( int _ = 0; _ < t; _++)
    {
        int cnt = 1;
        int n,k;
        cin>>n>>k;
        int idx1, idx2;
        if ( k == 1 )
        {
            cout<<string(n - 2, 'a')<<"bb"<<endl;
            continue;
        }
        for ( int i = mlen - 1; i >= 0; i--) if ( need[i] < k )
        {
            idx1 = i;
            break;
        }
        for ( int i = idx1; i >= 0; i--) if ( need[i] < k - need[idx1] )
        {
            idx2 = i;
            break;
        }
        //cout<<idx1<<" "<<idx2<<endl;
        cout<<string(n - idx1-1, 'a') + "b" + string(idx1-idx2-1, 'a') + "b" + string(idx2, 'a')<<endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long int n,k;
        int i;
        cin>>n>>k;
        vector <char> s(n, 'a');
        if(k==1){s[n-1]='b';s[n-2]='b';}
        else
        {
            for(i=0;(i*(i+1))<(2*k);i++);
            i=i-1;
            s[n-i-2]='b';
            s[n-(k-(i*(i+1)/2))]='b';
        }
        for(i=0;i<n;i++)
        cout<<s[i];
        cout<<endl;
    }
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k,i;
        cin>>n>>k;
        vector <char> s(n, 'a');
        if(k==1){s[n-1]='b';s[n-2]='b';}
        else
        {
            for(i=0;(i*(i+1))<(2*k);i++);
            i=i-1;
            s[n-i-2]='b';
            s[n-(k-(i*(i+1)/2))]='b';
        }
        for(i=0;i<n;i++)
        cout<<s[i];
        cout<<endl;
    }
}


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

vector<char> :: iterator last_occurance(char c, vector<char>& S){
	auto it = S.begin();
	for(auto temp = S.end()-1; temp>=S.begin(); temp--){
		if (*temp == c){
			it = temp;
		}
	}
	return it;
}

void print(const vector<char>& S){
	for (auto val: S){
		cout << val;
	}
	cout << endl;
}

int main(){
	int t;
	cin >> t;

	for(int i=0; i<t; i++){
		int n, k;
		cin >> n >> k;
		vector <char> S(n, 'a');
		for(int j=n-1; j>=n-2; j--){
			S[j] = 'b';
		}

		for(int j=1; j<k; j++){
			//print (S);
			auto it= last_occurance('b', S);
			while(*it != 'a' && it != S.begin()){
			       it --;
		      	}
	 		if (it+1 != S.end()){
				*it = 'b';
				*(it + 1) = 'a';
				sort(it+2, S.end());
			}

		}
		print (S);
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        long long int i, k;
        cin>>n>>k;
        vector <char> s(n, 'a');
        if(k==1){s[n-1]='b';s[n-2]='b';}
        else
        {
            for(i=0;(i*(i+1))<(2*k);i++);
            i=i-1;
            s[n-i-2]='b';
            s[n-(k-(i*(i+1)/2))]='b';
        }
        for(i=0;i<n;i++)
        cout<<s[i];
        cout<<endl;
    }
}


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

vector<char> :: iterator last_occurance(char c, vector<char>& S){
	auto it = S.begin();
	for(auto temp = S.begin(); temp!=S.end(); temp++){
		if (*temp == 'b'){
			it = temp;
		}
	}
	return it;
}

void print(const vector<char>& S){
	for (auto val: S){
		cout << val;
	}
	cout << endl;
}

int main(){
	int t;
	cin >> t;

	for(int i=0; i<t; i++){
		int n, k;
		cin >> n >> k;
		vector <char> S(n, 'a');
		for(int j=n-1; j>=n/2 + n%2; j--){
			S[j] = 'b';
		}

		for(int j=1; j<k; j++){
			//print (S);
			auto it= last_occurance('b', S);
			while(*it != 'a' && it != S.begin()){
			       it --;
		      	}
	 		if (it+1 != S.end()){
				*it = 'b';
				*(it + 1) = 'a';
				sort(it+2, S.end());
			}

		}
		print (S);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define MAXN 510000

using namespace std;

typedef long long int LL;

int n;
LL K,A[MAXN];

int main()
{
    LL sum=0;
    scanf("%d%I64d",&n,&K);
    for(int i=1;i<=n;i++)
        scanf("%I64d",&A[i]),sum+=A[i];
    sort(A+1,A+n+1);
    if(A[1]==A[n])
    {
        printf("0\n");
        return 0;
    }
    LL maxv=A[n],rest=K;
    for(int i=n;i>=1;i--)
    {
        if(maxv>A[i])
        {
            LL tmp=min(maxv-A[i],rest/(n-i));
            rest-=(n-i)*tmp;
            maxv-=tmp;
        }
    }
    LL minv=A[1];
    rest=K;
    for(int i=1;i<=n;i++)
    {
        if(minv<A[i])
        {
            LL tmp=min(A[i]-minv,rest/(i-1));
            rest-=(i-1)*tmp;
            minv+=tmp;
        }
    }
    LL dd;
        if(sum%(LL)n==0)
            dd=0;
        else
            dd=1;

        printf("%I64d\n",max(dd,maxv-minv));
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define MAXN 510000

using namespace std;

typedef long long int LL;

int n;
LL K,A[MAXN];

int main()
{
    LL sum=0;
    scanf("%d%I64d",&n,&K);
    for(int i=1;i<=n;i++)
        scanf("%I64d",&A[i]),sum+=A[i];
    sort(A+1,A+n+1);
    if(A[1]==A[n])
    {
        printf("0\n");
        return 0;
    }
    LL maxv=A[n],rest=K;
    for(int i=n;i>=1;i--)
    {
        if(maxv>A[i])
        {
            LL tmp=min(maxv-A[i],rest/(n-i));
            rest-=(n-i)*tmp;
            maxv-=tmp;
        }
    }
    LL minv=A[1];
    rest=K;
    for(int i=1;i<=n;i++)
    {
        if(minv<A[i])
        {
            LL tmp=min(A[i]-minv,rest/(i-1));
            rest-=(i-1)*tmp;
            minv+=tmp;
        }
    }
    if(minv>maxv)
    {
        if(sum%(LL)n==0)
            printf("0\n");
        else
            printf("1\n");
    }
    else
    {
        printf("%I64d\n",maxv-minv);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N, K;
const int inf = 2000000014;
int ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	int s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e)/2;
		if(m==s)
			m++;
		for(int i=1;i<=N;i++)
			c += max(0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e)/2;

		for(int i=1;i<=N;i++)
			c += max(0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N, K;
const int inf = 1050000000;
int ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	int s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = (s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max(0, m-ar[i]);

		if(c <= (ll)K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = (s+e)/2;

		for(int i=1;i<=N;i++)
			c += max(0, ar[i]-m);

		if(c <= (ll)K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
#define ll long long
#define N (int)(5e5+10)
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define nd second
#define st first
#define inf mod
#define endl '\n'
#define sag (sol|1)
#define sol (root<<1)
#define bit(x,y) ((x>>y)&1)

ll t;
int a[N],i,j,k,n,m,x,y,z;

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

	for(i=1 ; i<=n ; i++)
		scanf("%d",a+i);

	int bas=1, son=2*inf;

	while(bas<son){
		int ort = ((ll)bas+son)/2;
		if(bas==ort)
			ort++;
		t = 0;
		for(i=1 ; i<=n ; i++)
			t += max(0 , ort-a[i]);
		if(t <= k)
			bas = ort;
		else
			son = ort-1;
	}

	t = k;

	for(i=1 ; i<=n ; i++){
		t -= max(0 , bas-a[i]);
		a[i] = max(a[i] , bas);
	}

	for(i=1 ; i<=n ; i++)
		if(a[i] == bas and t){
			t--;
			a[i]++;
		}

	for(i=1;i<=n;i++)
		cout<<a[i] << " ";
	cout << endl;
	bas=1, son=2*inf;

	while(bas<son){
		int ort = ((ll)bas+son)/2;
		t = 0;
		for(i=1 ; i<=n ; i++)
			t += max(0 , a[i]-ort);
		if(t <= k)
			son = ort;
		else
			bas = ort+1;
	}

	t = k;

	for(i=1 ; i<=n ; i++){
		t -= max(0 , a[i]-bas);
		a[i] = min(a[i] , bas);
	}

	for(i=1 ; i<=n ; i++)
		if(a[i] == bas and t){
			t--;
			a[i]--;
		}

	cout << *max_element(a+1,a+1+n) - *min_element(a+1,a+1+n) << endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N, K;
const int inf = 2000000014;
int ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	int s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max(0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e)/2;

		for(int i=1;i<=N;i++)
			c += max(0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
#define ll long long
#define N (int)(5e5+10)
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define nd second
#define st first
#define inf mod
#define endl '\n'
#define sag (sol|1)
#define sol (root<<1)
#define bit(x,y) ((x>>y)&1)

ll t;
int a[N],i,j,k,n,m,x,y,z;

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

	for(i=1 ; i<=n ; i++)
		scanf("%d",a+i);

	int bas=1, son=2*inf;

	while(bas<son){
		int ort = ((ll)bas+son)/2;
		if(bas==ort)
			ort++;
		t = 0;
		for(i=1 ; i<=n ; i++)
			t += max(0 , ort-a[i]);
		if(t <= k)
			bas = ort;
		else
			son = ort-1;
	}

	t = k;

	for(i=1 ; i<=n ; i++){
		t -= max(0 , bas-a[i]);
		a[i] = max(a[i] , bas);
	}

	for(i=1 ; i<=n ; i++)
		if(a[i] == bas and t){
			t--;
			a[i]++;
		}

	bas=1, son=2*inf;

	while(bas<son){
		int ort = ((ll)bas+son)/2;
		t = 0;
		for(i=1 ; i<=n ; i++)
			t += max(0 , a[i]-ort);
		if(t <= k)
			son = ort;
		else
			bas = ort+1;
	}

	t = k;

	for(i=1 ; i<=n ; i++){
		t -= max(0 , a[i]-bas);
		a[i] = min(a[i] , bas);
	}

	for(i=1 ; i<=n ; i++)
		if(a[i] == bas and t){
			t--;
			a[i]--;
		}

	cout << *max_element(a+1,a+1+n) - *min_element(a+1,a+1+n) << endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N;
ll K;
const int inf = 2000000000;
ll ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	ll s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N;
ll K;
const int inf = 1000000000;
ll ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	ll s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N, K;
const int inf = 2000000014;
int ar[MAXN];

int main() {
	int i;
	cin >> N >> K;
	for(i=1;i<=N;i++)
		scanf("%d",&ar[i]);

	int s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e)/2;
		if(m==s)
			m++;
		for(i=1;i<=N;i++)
			c += max(0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = ((ll)s+e)/2;

		for(int i=1;i<=N;i++)
			c += max(0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(i=1;i<=N;i++) {

		c -= max(0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N, K;
const int inf = 1000000007;
int ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	int s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = (s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max(0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}
	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0;
		int m = (s+e)/2;

		for(int i=1;i<=N;i++)
			c += max(0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max(0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define  MAXN 500050
using namespace std;
typedef long long ll;

int N;
ll K;
const int inf = 1000000000;
ll ar[MAXN];

int main() {

	cin >> N >> K;
	for(int i=1;i<=N;i++)
		cin >> ar[i];

	ll s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e+1)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, m-ar[i]);

		if(c <= K)
			s = m;
		else
			e = m-1;
	}

	ll c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, s-ar[i]);
		ar[i] = max(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]++;
		}

	s = 0, e = inf;
	while(s<e) {

		ll c = 0, m = (s+e)/2;

		for(int i=1;i<=N;i++)
			c += max((ll)0, ar[i]-m);

		if(c <= K)
			e = m;
		else
			s = m+1;
	}
	c = K;
	for(int i=1;i<=N;i++) {

		c -= max((ll)0, ar[i]-s);
		ar[i] = min(ar[i],s);
	}

	for(int i=1;i<=N;i++)
		if(ar[i]==s && c) {

			c--;
			ar[i]--;
		}

	cout << *max_element(ar+1,ar+N+1)-*min_element(ar+1,ar+N+1)) << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    long long s=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i];
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    int flag=1;
    if(!(s%(long long)n))flag=0;
    if(maxn<minn)printf("%d\n",flag);
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    long long s=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i];
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    int flag=1;
    if(!(s%(ll)n))flag=0;
    if(maxn<minn)printf("%d\n",flag);
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    minn=l-1;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
    }
    return sum<=k;
}
int main() {
    //freopen("problem.in","r",stdin);
    //freopen("problem.out","w",stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    //sort(a+1,a+n+1);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    maxn=l;
    printf("%d %d",maxn,minn);
    if(minn<maxn)puts("0");
    else printf("%d %d",maxn,minn);

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    if(maxn<minn)printf("1\n");
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
    }
    return sum<=k;
}
int main() {
    //freopen("problem.in","r",stdin);
    //freopen("problem.out","w",stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    //sort(a+1,a+n+1);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    minn=l;
    if(!check1(minn))minn--;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    if(minn>maxn)puts("0");
    else printf("%d\n",maxn-minn);

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    unsigned long long s=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i]*1ll;
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    int flag=1;
    if(!(s%(unsigned long long)n))flag=0;
    if(maxn<=minn)printf("%d\n",flag);
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
    }
    return sum<=k;
}
int main() {
    //freopen("problem.in","r",stdin);
    //freopen("problem.out","w",stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    //sort(a+1,a+n+1);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    minn=l;
    if(!check1(minn))minn--;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l+1;
    if(minn>maxn)puts("0");
    else printf("%d\n",maxn-minn);

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    unsigned long long s=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i];
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    int flag=1;
    flag=s%(unsigned long long)n;
    if(maxn<minn)printf("%d\n",flag);
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    int s=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i];
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    int flag=1;
    if(!(s%n))flag=0;
    if(maxn<minn)printf("%d\n",flag);
    else
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 510000;
int a[N], k, n;
bool check1(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]<x) {
            sum+=x-a[i];
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
bool check2(int x) {
    int sum=0;
    for(int i=1;i<=n;i++) {
        if(a[i]>x) {
            sum+=a[i]-x;
        }
        if(sum>k)return 0;
    }
    return sum<=k;
}
int main() {
    /*freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);*/
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int l=0,r=1e9,minn,maxn;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check1(mid)) {
            l=mid+1;
        }
        else {
            r=mid;
        }
    }
    if(check1(l-1))l--;
    minn=l;
    l=0,r=1e9;
    while(l<r) {
        int mid=(l+r)>>1;
        if(check2(mid)) {
            r=mid;
        }
        else {
            l=mid+1;
        }
    }
    maxn=l;
    if(maxn<minn)swap(maxn,minn);
    //printf("%d %d\n",maxn,minn);
    printf("%d\n",max(0,maxn-minn));

}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL in[500005];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i)
        scanf("%I64d",in+i);
    sort(in,in+n);
    LL s,t1=0,t2=0;
    int i,j;
    for(i=n-2,s=0;i>=0;--i)
    {
        s+=(in[i+1]-in[i])*(n-i-1);
        if(s>=k)
            break;
    }
    if(i<0)
    {
        puts("0");
        return 0;
    }
    else
        t1=in[i]+(s-k-1)/(n-i-1)+1;
    for(i=1,s=0;i<n;++i)
    {
        s+=(in[i]-in[i-1])*i;
        if(s>=k)
            break;
    }
    if(i>=n)
    {
        puts("0");
        return 0;
    }
    else
        t2=in[i]-(s-k-1)/i-1;
    if(t1<=t2)
        printf("0\n");
    else printf("%I64d\n",t1-t2);

//    printf("\t%I64d\t%I64d\n",t1,t2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL in[500005];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i)
        scanf("%I64d",in+i);
    sort(in,in+n);
    LL s,t1=0,t2=0;
    int i,j;
    for(i=n-2,s=0;i>=0;--i)
    {
        s+=(in[i+1]-in[i])*(n-i-1);
        if(s>=k)
            break;
    }
    if(i<0)
    {
        puts("0");
        return 0;
    }
    else
        t1=in[i]+(s-k)/(n-i-1)+!!((s-k)%(n-i-1));
    for(i=1,s=0;i<n;++i)
    {
        s+=(in[i]-in[i-1])*i;
        if(s>=k)
            break;
    }
    if(i>=n)
    {
        puts("0");
        return 0;
    }
    else
        t2=in[i]-(s-k)/i-!!((s-k)%i);
    if(t1<=t2)
        printf("0\n");
    else printf("%I64d\n",t1-t2);

//    printf("\t%I64d\t%I64d\n",t1,t2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL in[500005];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i)
        scanf("%I64d",in+i);
    sort(in,in+n);
    LL s,t1=0,t2=0;
    int i,j;
    for(i=n-2,s=0;i>=0;--i)
    {
        s+=(in[i+1]-in[i])*(n-i-1);
        if(s>=k)
            break;
    }

    if(i<0)t1=0;
    else
        t1=in[i]+(s-k)/(n-i-1)+(bool)((s-k)%(n-i-1));
    for(i=1,s=0;i<n;++i)
    {
        s+=(in[i]-in[i-1])*i;
        if(s>=k)
            break;
    }
    if(i>=n)
        t2=in[n-1];
    else
        t2=in[i]-(s-k)/i-(bool)((s-k)%i);
    if(t1<=t2)
        printf("0\n");
    else printf("%I64d\n",t1-t2);

//    printf("\t%I64d\t%I64d\n",t1,t2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL in[500005];
int main()
{
    scanf("%d%d",&n,&k);
    LL sum=0;
    for(int i=0;i<n;++i)
        scanf("%I64d",in+i),
        sum+=in[i];
    sort(in,in+n);
    LL s,t1=0,t2=0;
    int i,j;
    for(i=n-2,s=0;i>=0;--i)
    {
        s+=(in[i+1]-in[i])*(n-i-1);
        if(s>=k)
            break;
    }
    if(i<0)
    {
        if(sum%n)puts("1");
        else puts("0");
        return 0;
    }
    else
        t1=in[i]+(s-k)/(n-i-1)+!!((s-k)%(n-i-1));
    for(i=1,s=0;i<n;++i)
    {
        s+=(in[i]-in[i-1])*i;
        if(s>=k)
            break;
    }
    if(i>=n)
    {
        if(sum%n)puts("1");
        else puts("0");
        return 0;
    }
    else
        t2=in[i]-(s-k)/i-!!((s-k)%i);
    if(t1<=t2)
        printf("%d\n",!!(sum%n));
    else printf("%I64d\n",t1-t2);
//    printf("\t%I64d\t%I64d\n",t1,t2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL in[500005];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i)
        scanf("%I64d",in+i);
    sort(in,in+n);
    LL s,t1=0,t2=0;
    int i,j;
    for(i=n-2,s=0;i>=0;--i)
    {
        s+=(in[i+1]-in[i])*(n-i-1);
        if(s>=k)
            break;
    }

    if(i<0)t1=0;
    else
        t1=in[i]+(s-k)/(n-i-1)+(bool)((s-k)%(n-i-1));
    for(i=1,s=0;i<n;++i)
    {
        s+=(in[i]-in[i-1])*i;
        if(s>=k)
            break;
    }
    if(i>=n)
        t2=in[n-1];
    else
        t2=in[i]-(s-k)/i-(bool)((s-k)%i);
    if(t1<=t2)
        printf("0\n");
    else printf("%d\n",t1-t2);

//    printf("\t%I64d\t%I64d\n",t1,t2);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 1000000000000000LL
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid+1;ans=mid;}
        else r=mid-1;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid-1;ans=mid;}
        else l=mid+1;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<=r)printf("%I64d",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        //else printf("1");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF (LL)3e9
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid+1;ans=mid;}
        else r=mid-1;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid-1;ans=mid;}
        else l=mid+1;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<r)printf("%lld",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        else printf("1");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF (LL)3e9
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid+1;ans=mid;}
        else r=mid-1;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid-1;ans=mid;}
        else l=mid+1;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<=r)printf("%I64d",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        else printf("1");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 1000000000000000LL
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid+1;ans=mid;}
        else r=mid-1;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid-1;ans=mid;}
        else l=mid+1;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<=r)printf("%I64d",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        else printf("1");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 1000000000000000LL
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid+1;ans=mid;}
        else r=mid-1;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid-1;ans=mid;}
        else l=mid+1;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<=r)printf("%I64d",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        else printf("%I64d",sum%n);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 1000000000000000LL
LL n,k,c[500010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
LL getlow()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,mid-c[i]);
        if(sum<=k){l=mid;ans=mid;}
        else r=mid;
    }
    return ans;
}
LL gethigh()
{
    LL ans,l,r,mid,sum,i;
    l=-INF;r=INF;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=0LL;
        for(i=1;i<=n;i++)sum+=max(0LL,c[i]-mid);
        if(sum<=k){r=mid;ans=mid;}
        else l=mid;
    }
    return ans;
}
int main()
{
    LL i,l,r,sum;
    n=read();k=read();
    for(i=1;i<=n;i++)c[i]=read();
    l=getlow();r=gethigh();
    if(l<=r)printf("%I64d",r-l);
    else
    {
        sum=0LL;
        for(i=1;i<=n;i++)sum+=c[i];
        if(sum%n==0)printf("0");
        else printf("1");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>

#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int maxn = (int)1e6;
const int inf = (int)1e9;
const ll INF = (ll)1e18;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;

int n, k, c[maxn];

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

    for (int i = 0; i < n; ++i)
        scanf("%d", c + i);

    sort(c, c + n);
    reverse(c, c + n);

    long long cnt = 0;

    int r;
    for (int i = 0; k && i < n;)
    {
        r = i
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        long long cur = min((r + 1) * 1ll * (c[0] - c[r + 1]), k + 0ll);
        k -= cur;
        cnt += cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] - avr - 1;
        c[0] -= avr;
        i = r + 1;
        r -= rest;
    }

    for (int i = 1; i <= r; ++i) c[i] = c[0];

    reverse(c, c + n);
    for (int i = 0; cnt && i < n;)
    {
        r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        ll cur = min((r + 1) * 1ll * (c[r + 1] - c[0]), cnt + 0ll);
        cnt -= cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] + avr + 1;
        c[0] += avr;

        i = r + 1;
        r -= rest;
    }

    for (int i = 1; i <= r; ++i) c[i] = c[0];

    printf("%d\n", c[n - 1] - c[0]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>

#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int maxn = (int)1e6;
const int inf = (int)1e9;
const ll INF = (ll)1e18;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;

int n, k, c[maxn];

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

    for (int i = 0; i < n; ++i)
        scanf("%d", c + i);

    sort(c, c + n);
    reverse(c, c + n);

    long long cnt = 0;

    for (int i = 0; k && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        long long cur = min((r + 1) * 1ll * (c[0] - c[r + 1]), k + 0ll);
        k -= cur;
        cnt += cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] - avr - 1;
        c[0] -= avr;
        i = r + 1;
    }

    for (int i = 1; i < n; ++i) c[i] = min(c[0], c[i]);

    reverse(c, c + n);
    for (int i = 0; cnt && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        ll cur = min((r + 1) * 1ll * (c[r + 1] - c[0]), cnt + 0ll);
        cnt -= cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] + avr + 1;
        c[0] += avr;
        i = r + 1;
    }

    for (int i = 1; i < n; ++i) c[i] = max(c[0], c[i]);

    printf("%d\n", c[n - 1] - c[0]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>

#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int maxn = (int)1e6;
const int inf = (int)1e9;
const ll INF = (ll)1e18;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;

int n, k, c[maxn];

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

    for (int i = 0; i < n; ++i)
        scanf("%d", c + i);

    sort(c, c + n);
    reverse(c, c + n);

    long long cnt = 0;

    for (int i = 0; k && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        long long cur = min((r + 1) * 1ll * (c[0] - c[r + 1]), k + 0ll);
        k -= cur;
        cnt += cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        c[0] -= avr;
        for (int j = r; j > r - rest; --j)
            c[j] = c[0] - 1;
        i = r + 1;
    }

    for (int i = 1; i < n; ++i) c[i] = min(c[0], c[i]);

    reverse(c, c + n);
    c[n] = inf;
    for (int i = 0; cnt && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        ll cur = min((r + 1) * 1ll * (c[r + 1] - c[0]), cnt + 0ll);
        cnt -= cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] + avr + 1;
        c[0] += avr;
        i = r + 1;
    }

    for (int i = 1; i < n; ++i) c[i] = max(c[0], c[i]);

    printf("%d\n", c[n - 1] - c[0]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>

#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int maxn = (int)1e6;
const int inf = (int)1e9;
const ll INF = (ll)1e18;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;

int n, k, c[maxn];

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

    for (int i = 0; i < n; ++i)
        scanf("%d", c + i);

    sort(c, c + n);
    reverse(c, c + n);

    long long cnt = 0;

    for (int i = 0; k && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        long long cur = min((r + 1) * 1ll * (c[0] - c[r + 1]), k + 0ll);
        k -= cur;
        cnt += cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] - avr - 1;
        c[0] -= avr;
        i = r + 1;
    }

    for (int i = 1; i < n; ++i)
        if (c[i] > c[0]) c[i] = c[0];
        else break;

    reverse(c, c + n);
    for (int i = 0; cnt && i < n;)
    {
        int r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        ll cur = min((r + 1) * 1ll * (c[r + 1] - c[0]), cnt + 0ll);
        cnt -= cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] + avr + 1;
        c[0] += avr;

        i = r + 1;
    }

    for (int i = 1; i < n; ++i)
        if (c[i] < c[0]) c[i] = c[0];
        else break;

    printf("%d\n", c[n - 1] - c[0]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>

#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int maxn = (int)1e6;
const int inf = (int)1e9;
const ll INF = (ll)1e18;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;

int n, k, c[maxn];

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

    for (int i = 0; i < n; ++i)
        scanf("%d", c + i);

    sort(c, c + n);
    reverse(c, c + n);

    long long cnt = 0;

    int r;
    for (int i = 0; k && i < n;)
    {
        r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        long long cur = min((r + 1) * 1ll * (c[0] - c[r + 1]), k + 0ll);
        k -= cur;
        cnt += cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] - avr - 1;
        c[0] -= avr;
        i = r + 1;
        r -= rest;
    }

    for (int i = 1; i <= r; ++i) c[i] = c[0];

    reverse(c, c + n);
    for (int i = 0; cnt && i < n;)
    {
        r = i;
        while (r + 1 < n && c[r + 1] == c[0]) ++r;

        ll cur = min((r + 1) * 1ll * (c[r + 1] - c[0]), cnt + 0ll);
        cnt -= cur;

        ll avr = cur / (r + 1);
        ll rest = cur % (r + 1);

        for (int j = r; j > r - rest; --j)
            c[j] = c[0] + avr + 1;
        c[0] += avr;

        i = r + 1;
        r -= rest;
    }

    for (int i = 1; i <= r; ++i) c[i] = c[0];

    printf("%d\n", c[n - 1] - c[0]);
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[100001],ans,n,i,k;
main(){
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
if(a[0]==a[n-1]){cout<<0;return 0;}
for(i=0;i<k;i++){a[i]+=1;a[n-1-i]-=1;sort(a,a+n);}
sort(a,a+n);
cout<<a[n-1]-a[0];
system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[500001],n,d;
main(){
cin>>n>>d;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a,a+n+1);
if(a[1]==a[n])
{
                cout<<0<<endl;
                return 0;
                system("pause");
}
for(int i=1;i<=d;i++)
{
a[1]+=1;
sort(a,a+n+1);
a[n]-=1;
sort(a,a+n+1);
}
cout<<a[n]-a[1]<<endl;
system("pause");
}


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100001],ans,n,i,k;
main(){
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
if(a[0]==a[n-1]){cout<<0;return 0;}
for(i=0;i<k;i++){a[i]+=1;a[n-1-i]-=1;sort(a,a+n);}
sort(a,a+n);
cout<<a[n-1]-a[0];
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
long long sum,r,xmin,xmax,xcur,ans;
long long  n,nday,nd,i,a,b;
long long x[500001];
int xx;

using namespace std;
main(){
 scanf("%d %d",&n,&nday);
 for (i=1;i<=n;i++){
  scanf("%d",&xx);
  x[i]=(long long) xx;
  sum=sum+x[i];
 }

 r=sum%n; xmin=sum/n;
 sort(x+1,x+n+1);
 for (i=1;i<=n;i++){
  if(i<=n-r) xcur=xmin;
        else xcur=xmin+1;

  if (x[i]<=xcur) nd=nd+xcur-x[i];
             else break;
 }

 if (nd<=nday) {
  if (r==0)ans=0;
      else ans=1;
 } else {
  nd=nday;
  for (i=2;i<=n;i++){
   if( (x[i]-x[i-1])*(i-1)>=nday ){

    xmin=x[i-1]+nday/(i-1);

    break;
   }
   nday=nday-(x[i]-x[i-1])*(i-1);

  }//i

  nday=nd;
  for (i=n-1;i>=1;i--){
   if ( (x[i+1]-x[i])*(n-i)>=nday ) {

    xmax=x[i+1]-nday/(n-i);

    break;
   }

   nday=nday-(x[i+1]-x[i])*(n-i);

  }

 ans=xmax-xmin;

 }

 cout<<ans<<endl;

}


#include<iostream>
#include<algorithm>
using namespace std;
int a[500001],n,d;
main(){
cin>>n>>d;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a,a+n+1);
if(a[1]==a[n])
{
                cout<<0<<endl;
                return 0;
                //system("pause");
}
for(int i=1;i<=d;i++)
{
a[1]+=1;
sort(a,a+n+1);
a[n]-=1;
sort(a,a+n+1);
}
cout<<a[n]-a[1]<<endl;
//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[100001],ans,n,i,k;
main(){
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
if(a[0]==a[n-1]){cout<<0;return 0;}
for(i=0;i<k;i++){a[i]+=1;a[n-1-i]-=1;sort(a,a+n);}
sort(a,a+n);
cout<<a[n-1]-a[0];
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#ifdef WIN32
#define TAT "%I64d"
#else
#define TAT "%lld"
#endif
#define int64 long long

using namespace std;

int n,K,a[500500],Min,Max;
int64 sum;

bool judge(int mid)
{
	int tot = 0;
	for (int i = 1;i <= n;i++)
		{
			tot += max(a[i] - mid,0);
			if (tot > K)    return false;
		}
	return true;
}
void work(int mid)
{
	sort(a + 1,a + n + 1);
	int less = a[1],tot = 1;
	while (a[tot + 1] == less)    tot++;
	while (true)
		{
			less += max(0,min(a[tot + 1] - less,K / tot));
			while (a[tot + 1] == less)    tot++;
			if (K < tot)    {printf("%d",mid - less);return ;}
		}
}
void work()
{
	int l = Min,r = Max;
	while (l < r)
		{
			int mid = (l + r) >> 1;
			if (judge(mid))     r = mid;
			else l = mid + 1;
		}
	work(l);
}

int main()
{
	cin >> n >> K;
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]),sum += a[i],Max = max(a[i],Max);
	Min = sum / n + (sum % n != 0);
	work();
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#ifdef WIN32
#define TAT "%I64d"
#else
#define TAT "%lld"
#endif
#define int64 long long

using namespace std;

int n,K,a[500500],Min,Max;
int64 sum;

bool judge(int mid)
{
	int tot = 0;
	for (int i = 1;i <= n;i++)
		{
			tot += max(a[i] - mid,0);
			if (tot > K)    return false;
		}
	return true;
}
void work(int mid)
{
	sort(a + 1,a + n + 1);
	int less = a[1],tot = 1;
	while (a[tot + 1] == less && tot <= n)    tot++;
	while (true)
		{
			int v = max(0,min(a[tot + 1] - less,K / tot));
			less += v;     K -= v * tot;
			while (a[tot + 1] == less && tot <= n)    tot++;
			if (K < tot)    {printf("%d",mid - less);return ;}
		}
}
void work()
{
	int l = Min,r = Max;
	while (l < r)
		{
			int mid = (l + r) >> 1;
			if (judge(mid))     r = mid;
			else l = mid + 1;
		}
	work(l);
}

int main()
{
	cin >> n >> K;
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]),sum += a[i],Max = max(a[i],Max);
	Min = sum / n + (sum % n != 0);
	work();
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#ifdef WIN32
#define TAT "%I64d"
#else
#define TAT "%lld"
#endif
#define int64 long long

using namespace std;

int n,K,a[500500],Min,Max,ans;
int64 sum;

bool judge(int mid)
{
	int tot = 0;
	for (int i = 1;i <= n;i++)
		{
			tot += max(a[i] - mid,0);
			if (tot > K)    return false;
		}
	return true;
}
void work(int mid)
{
	sort(a + 1,a + n + 1);
	int less = a[1],tot = 1;
	while (a[tot + 1] == less && tot <= n)    tot++;
	while (true)
		{
			if (tot == n)    {ans = 0;return ;}
			int v = max(0,min(a[tot + 1] - less,K / tot));
			less += v;     K -= v * tot;
			while (a[tot + 1] == less && tot < n)    tot++;
			if (K < tot)    {ans = mid - less;return ;}
		}
}
void work()
{
	int l = Min,r = Max;
	while (l < r)
		{
			int mid = (l + r) >> 1;
			if (judge(mid))     r = mid;
			else l = mid + 1;
		}
	work(l);
	if (!ans && sum % n != 0)    ans++;
	printf("%d",ans);
}

int main()
{
	cin >> n >> K;
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]),sum += a[i],Max = max(a[i],Max);
	Min = sum / n + (sum % n != 0);
	work();
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#ifdef WIN32
#define TAT "%I64d"
#else
#define TAT "%lld"
#endif
#define int64 long long

using namespace std;

int n,K,a[500500],Min,Max;
int64 sum;

bool judge(int mid)
{
	int tot = 0;
	for (int i = 1;i <= n;i++)
		{
			tot += max(a[i] - mid,0);
			if (tot > K)    return false;
		}
	return true;
}
void work(int mid)
{
	sort(a + 1,a + n + 1);
	int less = a[1],tot = 1;
	while (a[tot + 1] == less && tot <= n)    tot++;
	while (true)
		{
			if (tot == n)    {printf("0");return ;}
			int v = max(0,min(a[tot + 1] - less,K / tot));
			less += v;     K -= v * tot;
			while (a[tot + 1] == less && tot < n)    tot++;
			if (K < tot)    {printf("%d",mid - less);return ;}
		}
}
void work()
{
	int l = Min,r = Max;
	while (l < r)
		{
			int mid = (l + r) >> 1;
			if (judge(mid))     r = mid;
			else l = mid + 1;
		}
	work(l);
}

int main()
{
	cin >> n >> K;
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]),sum += a[i],Max = max(a[i],Max);
	Min = sum / n + (sum % n != 0);
	work();
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#ifdef WIN32
#define TAT "%I64d"
#else
#define TAT "%lld"
#endif
#define int64 long long

using namespace std;

int n,K,a[500500],Min,Max,ans;
int64 sum;

bool judge(int mid)
{
	int tot = 0;
	for (int i = 1;i <= n;i++)
		{
			tot += max(a[i] - mid,0);
			if (tot > K)    return false;
		}
	return true;
}
void work(int mid)
{
	sort(a + 1,a + n + 1);
	int less = a[1],tot = 1;
	while (a[tot + 1] == less && tot <= n)    tot++;
	while (true)
		{
			if (tot == n)    {ans = 0;return ;}
			int v = max(0,min(a[tot + 1] - less,K / tot));
			less += v;     K -= v * tot;
			while (a[tot + 1] == less && tot < n)    tot++;
			if (K < tot)    {ans = mid - less;return ;}
		}
}
void work()
{
	int l = Min,r = Max;
	while (l < r)
		{
			int mid = (l + r) >> 1;
			if (judge(mid))     r = mid;
			else l = mid + 1;
		}
	work(l);
	ans = max(ans,0);
	if (!ans && sum % n != 0)    ans++;
	printf("%d",ans);
}

int main()
{
	cin >> n >> K;
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]),sum += a[i],Max = max(a[i],Max);
	Min = sum / n + (sum % n != 0);
	work();
}


#include<bits/stdc++.h>
#define MAXN 500009
#define INF 1000000007
#define imx 2147483647
#define LLINF 1000000000000000007
#define mp(x,y) make_pair(x,y)
#define wr cout<<"Continue Debugging!!!"<<endl;
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define ppb() pop_back()
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n,k,mn=INF,mx=0;
map<int,int>pm;
int arr[MAXN],sum[MAXN],cum[MAXN];
int touch(int x){
	int kz=upper_bound(arr+1,arr+n+1,x)-arr;kz=n-kz+1;
	if(!(kz+pm[x]))
		return INF;
	return (cum[kz]-x*kz);
}
int ok(){
	int st=mn,en=mx;
	while(st+1<en){
		int mid=(st+en)>>1;
		if(touch(mid)<=k)
			en=mid;
		else
			st=mid;
	}
	for(int i=st;i<=en;i++)
		if(touch(i)<=k)
			return i;
}
int patch(int x){
	int ru=lower_bound(arr+1,arr+n+1,x)-arr;ru--;
	if(!(ru+pm[x]))
		return INF;
	return (ru*x-sum[ru]);
}
int love(){
	int st=mn,en=mx;
	while(st+1<en){
		int mid=(st+en)>>1;
		if(patch(mid)<=k)
			st=mid;
		else
			en=mid;
	}
	for(int i=en;i>=st;i--)
		if(patch(i)<=k)
			return i;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d",arr+i);
		umin(mn,arr[i]);
		umax(mx,arr[i]);
		pm[arr[i]]++;
	}
	sort(arr+1,arr+n+1);
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+arr[i];
	for(int i=n;i>=1;i--)
		cum[(n-i+1)]=cum[n-i]+arr[i];
	int dif=ok()-love();
	if(dif<0)
		printf("%d\n",sum[n]&1);
	else
		printf("%d\n",dif);
	return 0;
}
//LooK aT mY COde ONlinE +_+


#include<bits/stdc++.h>
#define MAXN 500009
#define INF 1000000007
#define imx 2147483647
#define LLINF 1000000000000000007
#define mp(x,y) make_pair(x,y)
#define wr cout<<"Continue Debugging!!!"<<endl;
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define ppb() pop_back()
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>PII;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ll n,k,mn=INF,mx=0;
map<ll,ll>pm;
ll arr[MAXN],sum[MAXN],cum[MAXN];
ll touch(ll x){
	ll kz=upper_bound(arr+1,arr+n+1,x)-arr;kz=n-kz+1;
	if(!(kz+pm[x]))
		return INF;
	return (cum[kz]-x*kz);
}
ll ok(){
	ll st=mn,en=mx;
	while(st+1<en){
		ll mid=(st+en)>>1;
		if(touch(mid)<=k)
			en=mid;
		else
			st=mid;
	}
	for(ll i=st;i<=en;i++)
		if(touch(i)<=k)
			return i;
}
ll patch(ll x){
	ll ru=lower_bound(arr+1,arr+n+1,x)-arr;ru--;
	if(!(ru+pm[x]))
		return INF;
	return (ru*x-sum[ru]);
}
ll love(){
	ll st=mn,en=mx;
	while(st+1<en){
		ll mid=(st+en)>>1;
		if(patch(mid)<=k)
			st=mid;
		else
			en=mid;
	}
	for(ll i=en;i>=st;i--)
		if(patch(i)<=k)
			return i;
}
int main(){
	scanf("%lld%lld",&n,&k);
	for(ll i=1;i<=n;i++){
		scanf("%lld",arr+i);
		umin(mn,arr[i]);
		umax(mx,arr[i]);
		pm[arr[i]]++;
	}
	sort(arr+1,arr+n+1);
	for(ll i=1;i<=n;i++)
		sum[i]=sum[i-1]+arr[i];
	for(ll i=n;i>=1;i--)
		cum[(n-i+1)]=cum[n-i]+arr[i];
	ll dif=ok()-love();
	if(dif<=0){
		if(sum[n]%n!=0)
			printf("1\n");
		else
			printf("0\n");
	}
	else
		printf("%lld\n",dif);
	return 0;
}
//LooK aT mY COde ONlinE +_+


#include<bits/stdc++.h>
#define MAXN 100009
#define INF 1000000007
#define imx 2147483647
#define LLINF 1000000000000000007
#define mp(x,y) make_pair(x,y)
#define wr cout<<"Continue Debugging!!!"<<endl;
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define ppb() pop_back()
#define sqr(x) ((x)*(x))
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
using namespace std;
typedef long long ll;
typedef pair<double,int>PII;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
pair<int,int> arr[MAXN];
double dis(int x1,int y1,int x2,int y2){
	double x3=sqr(x1-x2);
	double y3=sqr(y1-y2);
	return sqrt(x3+y3);
}
set<PII>Adil,Bera;
double go(string k,int ind){
	if(k=="IamAdil"){
		typeof((Bera).begin()) it=Bera.begin();
		while(it->ss==ind)
			it++;
		if(it!=Bera.end())
			return it->ff;
	}
	if(k=="IamBera"){
		typeof((Adil).begin()) it=Adil.begin();
		while(it->ss==ind)
			it++;
		if(it!=Adil.end())
			return it->ff;
	}
	return INF;
}
int main(){
	int ax,bx,by,ay,tx,ty,n;
	double ans=INF,sum=0;
	scanf("%d%d%d%d%d%d%d",&ax,&ay,&bx,&by,&tx,&ty,&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&arr[i].ff,&arr[i].ss);
		sum+=(2*dis(tx,ty,arr[i].ff,arr[i].ss));
	}
	for(int i=1;i<=n;i++){
		umin(ans,(sum-(2*dis(tx,ty,arr[i].ff,arr[i].ss)))+dis(ax,ay,arr[i].ff,arr[i].ss)+dis(arr[i].ff,arr[i].ss,tx,ty));
		umin(ans,(sum-(2*dis(tx,ty,arr[i].ff,arr[i].ss)))+dis(bx,by,arr[i].ff,arr[i].ss)+dis(arr[i].ff,arr[i].ss,tx,ty));
	}
	if(n==1)
		return printf("%.12lf\n",ans),0;
	for(int j=1;j<=n;j++)
		Adil.insert(mp(-2*dis(tx,ty,arr[j].ff,arr[j].ss)+
				dis(ax,ay,arr[j].ff,arr[j].ss)+dis(arr[j].ff,arr[j].ss,tx,ty),j));
	for(int j=1;j<=n;j++)
		Bera.insert(mp(-2*dis(tx,ty,arr[j].ff,arr[j].ss)+
				dis(bx,by,arr[j].ff,arr[j].ss)+dis(arr[j].ff,arr[j].ss,tx,ty),j));
	for(int i=1;i<=n;i++)
		umin(ans,sum-2*dis(tx,ty,arr[i].ff,arr[i].ss)+dis(ax,ay,arr[i].ff,arr[i].ss)+
			dis(arr[i].ff,arr[i].ss,tx,ty)+go("IamAdil",i));
	for(int i=1;i<=n;i++)
		umin(ans,sum-2*dis(tx,ty,arr[i].ff,arr[i].ss)+dis(bx,by,arr[i].ff,arr[i].ss)+
			dis(arr[i].ff,arr[i].ss,tx,ty)+go("IamBera",i));
	printf("%.12lf\n",ans);
	return 0;
}
//LooK aT mY COde ONlinE +_+


#include<bits/stdc++.h>
#define MAXN 500009
#define INF 1000000007
#define imx 2147483647
#define LLINF 1000000000000000007
#define mp(x,y) make_pair(x,y)
#define wr cout<<"Continue Debugging!!!"<<endl;
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define ppb() pop_back()
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>PII;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ll n,k,mn=INF,mx=0;
map<ll,ll>pm;
ll arr[MAXN],sum[MAXN],cum[MAXN];
ll touch(ll x){
	ll kz=upper_bound(arr+1,arr+n+1,x)-arr;kz=n-kz+1;
	if(!(kz+pm[x]))
		return INF;
	return (cum[kz]-x*kz);
}
ll ok(){
	ll st=mn,en=mx;
	while(st+1<en){
		ll mid=(st+en)>>1;
		if(touch(mid)<=k)
			en=mid;
		else
			st=mid;
	}
	for(ll i=st;i<=en;i++)
		if(touch(i)<=k)
			return i;
}
ll patch(ll x){
	ll ru=lower_bound(arr+1,arr+n+1,x)-arr;ru--;
	if(!(ru+pm[x]))
		return INF;
	return (ru*x-sum[ru]);
}
ll love(){
	ll st=mn,en=mx;
	while(st+1<en){
		ll mid=(st+en)>>1;
		if(patch(mid)<=k)
			st=mid;
		else
			en=mid;
	}
	for(ll i=en;i>=st;i--)
		if(patch(i)<=k)
			return i;
}
int main(){
	scanf("%lld%lld",&n,&k);
	for(ll i=1;i<=n;i++){
		scanf("%lld",arr+i);
		umin(mn,arr[i]);
		umax(mx,arr[i]);
		pm[arr[i]]++;
	}
	sort(arr+1,arr+n+1);
	for(ll i=1;i<=n;i++)
		sum[i]=sum[i-1]+arr[i];
	for(ll i=n;i>=1;i--)
		cum[(n-i+1)]=cum[n-i]+arr[i];
	ll dif=ok()-love();
	if(dif<0){
		if(sum[n]%n!=0)
			printf("1\n");
		else
			printf("0\n");
	}
	else
		printf("%lld\n",dif);
	return 0;
}
//LooK aT mY COde ONlinE +_+


#include<bits/stdc++.h>
#define MAXN 500009
#define INF 1000000007
#define imx 2147483647
#define LLINF 1000000000000000007
#define mp(x,y) make_pair(x,y)
#define wr cout<<"Continue Debugging!!!"<<endl;
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define ppb() pop_back()
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n,k,mn=INF,mx=0;
map<int,int>pm;
int arr[MAXN],sum[MAXN],cum[MAXN];
int touch(int x){
	int kz=upper_bound(arr+1,arr+n+1,x)-arr;kz=n-kz+1;
	if(!(kz+pm[x]))
		return INF;
	return (cum[kz]-x*kz);
}
int ok(){
	int st=mn,en=mx;
	while(st+1<en){
		int mid=(st+en)>>1;
		if(touch(mid)<=k)
			en=mid;
		else
			st=mid;
	}
	for(int i=st;i<=en;i++)
		if(touch(i)<=k)
			return i;
}
int patch(int x){
	int ru=lower_bound(arr+1,arr+n+1,x)-arr;ru--;
	if(!(ru+pm[x]))
		return INF;
	return (ru*x-sum[ru]);
}
int love(){
	int st=mn,en=mx;
	while(st+1<en){
		int mid=(st+en)>>1;
		if(patch(mid)<=k)
			st=mid;
		else
			en=mid;
	}
	for(int i=en;i>=st;i--)
		if(patch(i)<=k)
			return i;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d",arr+i);
		umin(mn,arr[i]);
		umax(mx,arr[i]);
		pm[arr[i]]++;
	}
	sort(arr+1,arr+n+1);
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+arr[i];
	for(int i=n;i>=1;i--)
		cum[(n-i+1)]=cum[n-i]+arr[i];
	int dif=ok()-love();
	if(dif<0){
		if(sum[n]%n!=0)
			printf("1\n");
		else
			printf("0\n");
	}
	else
		printf("%d\n",dif);
	return 0;
}
//LooK aT mY COde ONlinE +_+


#include <iostream>
#include<algorithm>
#include<stdio.h>

using namespace std;

unsigned int Rango = 1;
unsigned int x = 0;
unsigned int K = 0;
unsigned int N = 0;
unsigned int Mover = 0;
unsigned int Mayor, Menor;
unsigned int LimiteMx, LimiteMn;

unsigned int Civiles[ 500000 ];

unsigned int BuscarSgte(){
    int fin = N -1 ; int inicio = 0;
    int medio = (N - 1)/2;

    while( fin != inicio + 1 ){
            if( Civiles[ medio ] > Civiles[ 0 ] ) fin = medio;
            else if( Civiles[ medio ] == Civiles[ 0 ] && Civiles[ medio + 1] == Civiles[ 0 ] ) inicio = medio;
            else break;
            medio = ( fin + inicio )/2;
    }

    return ( Civiles[ medio + 1 ] == Civiles[0] )? medio + 1 : medio ;
}

unsigned int BuscarAnterior(){
    int medio = (N-1)/2;
    int fin = N - 1; int inicio = 0;

    while( fin != 1 + inicio ){
        if( Civiles[ medio ] < Civiles[ N - 1 ])
            inicio = medio;
        else if( Civiles[ medio ] == Civiles[ N - 1 ] && Civiles[ medio - 1 ] == Civiles[ N - 1 ] )
            fin = medio;
        else
            break;

        medio = ( fin + inicio ) / 2;
    }

    return ( Civiles[ medio ] == Civiles[ N - 1] )? medio: medio + 1;

}

void Actualizar(  int Mn, int Mx , int Base ){
    for( int i = 0; i < Base ; i++ ){
        Civiles[ Mn - i ] += 1;
        Civiles[ Mx + i ] -= 1;
    }
}

int main()
{

    scanf("%i %i", &N , &K );

    for( int i = 0; i < N; i++ ){
        scanf("%i", &Civiles[i] );
    }

    sort(Civiles, Civiles+N);

    while( K > 0 ){
        if( Civiles[ 0 ] + 1 == Civiles[ N - 1 ] || Civiles[0] == Civiles[ N - 1 ] ) break;

        LimiteMx = BuscarAnterior();
        LimiteMn = BuscarSgte();

        Mayor = ( N - LimiteMx )*( Civiles[  N - 1 ] - Civiles[ LimiteMx - 1 ] );
        Menor = ( LimiteMn + 1 )*( Civiles[ LimiteMn + 1 ] - Civiles[ 0 ] );

        Mover = min( Menor, Mayor );
        if( K <= Mover ){
            Actualizar( LimiteMn , LimiteMx , K );
            K = 0;
        }else{
            Actualizar( LimiteMn, LimiteMx , Mover );
            K = K - Mover;
        }

    }

    cout <<  Civiles[ N - 1 ] - Civiles[ 0 ] ;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 5e5 + 5;

int n, k, C[N];

void doit() {

	sort(C + 1, C + n + 1);

	int last = C[1], temp = k;

	FOR(i, 2, n) {
		if((C[i] - last) * (ll) (i - 1) <= k) {
			k -= (C[i] - last) * (i - 1);
			last = C[i];
		}
	}

	int j = 0; while(j + 1 <= n && C[j + 1] <= last) j++;

	int go = k / j, left = k % j;

	FOR(i, 1, j) C[i] = last + go + (i <= left);

	k = temp;
}

void rev() { FOR(i, 1, n) C[i] = -C[i];  }

int main() {

	scanf("%d %d", &n, &k);

	FOR(i, 1, n)
		scanf("%d", &C[i]);

	doit(); rev();

	doit(); rev();

	sort(C + 1, C + n + 1);

	printf("%d\n", C[n] - C[1]);

	return 0;
}


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

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 5e5 + 5;

int n, k, C[N];

void doit() {

	sort(C + 1, C + n + 1);

	int last = C[1], temp = k;

	FOR(i, 2, n) {
		if((C[i] - last) * (ll) (i - 1) <= k) {
			k -= (C[i] - last) * (i - 1);
			last = C[i];
		}
	}

	int j = 0; while(j + 1 <= n && C[j + 1] <= last) j++;

	int go = k / j, left = k % j;

	FOR(i, 1, j) C[i] = last + go + (i <= left);

	k = temp;
}

void rev() { FOR(i, 1, n) C[i] = -C[i];  }

int main() {

	scanf("%d %d", &n, &k);

	FOR(i, 1, n)
		scanf("%d", &C[i]);

	doit(); rev();

	doit(); rev();

	sort(C + 1, C + n + 1);

	printf("%d\n", C[n] - C[1]);

	return 0;
}


case 1:
			 	print "";
				break;
			case 1:
			 	print "";
				break;
			case 1:
			 	print "";
				break;


#include <iostream>
#include<algorithm>
#include<stdio.h>

using namespace std;

int Rango = 1;
int x = 0;
int K = 0;
int N = 0;
int Mover = 0;
int Mayor, Menor;
int LimiteMx, LimiteMn;

int Civiles[ 500000 ];

int BuscarSgte(){
    int fin = N -1 ; int inicio = 0;
    int medio = (N - 1)/2;

    while( fin != inicio + 1 ){
            if( Civiles[ medio ] > Civiles[ 0 ] ) fin = medio;
            else if( Civiles[ medio ] == Civiles[ 0 ] && Civiles[ medio + 1] == Civiles[ 0 ] ) inicio = medio;
            else break;
            medio = ( fin + inicio )/2;
    }

    return ( Civiles[ medio + 1 ] == Civiles[0] )? medio + 1 : medio ;
}

int BuscarAnterior(){
    int medio = (N-1)/2;
    int fin = N - 1; int inicio = 0;

    while( fin != 1 + inicio ){
        if( Civiles[ medio ] < Civiles[ N - 1 ])
            inicio = medio;
        else if( Civiles[ medio ] == Civiles[ N - 1 ] && Civiles[ medio - 1 ] == Civiles[ N - 1 ] )
            fin = medio;
        else
            break;

        medio = ( fin + inicio ) / 2;
    }

    return ( Civiles[ medio ] == Civiles[ N - 1] )? medio: medio + 1;

}

void Actualizar(  int Mn, int Mx , int Base ){
    for( int i = 0; i < Base ; i++ ){
        Civiles[ Mn - i ] += 1;
        Civiles[ Mx + i ] -= 1;
    }
}

int main()
{

    scanf("%i %i", &N , &K );

    for( int i = 0; i < N; i++ ){
        scanf("%i", &Civiles[i] );
    }

    sort(Civiles, Civiles+N);
    int Base = Civiles[ 0 ];
    for( int i = 0; i < N ; i++ ){
        Civiles[ i  ] = Civiles[ i ] - Base;
        if( i > 0 && Civiles[ i ] != Civiles[ i -1  ] ) Rango++;
    }

    while( K > 0 && Rango > 1 ){
        if( Civiles[ 0 ] + 1 == Civiles[N - 1 ] ) break;

        LimiteMx = BuscarAnterior();
        LimiteMn = BuscarSgte();

        Mayor = ( N - LimiteMx )*( Civiles[  N - 1 ] - Civiles[ LimiteMx - 1 ] );
        Menor = ( LimiteMn + 1 )*( Civiles[ LimiteMn + 1 ] );

        Mover = min( Menor, Mayor );

        if( K <= Mover ){
            (K == Mover)? Rango -= 2 :Rango += 2;
            Actualizar( LimiteMn , LimiteMx , K );
            K = 0;
        }else{
            Rango -= 2;
            Actualizar( LimiteMn, LimiteMx , Mover );
            K = K - Mover;
        }
    }

    cout << abs( Civiles[ N - 1 ] - Civiles[ 0 ] );

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 5e5 + 5;

int n, k, C[N];

void doit() {

	sort(C + 1, C + n + 1);

	int last = C[1], temp = k;

	FOR(i, 2, n) {
		if((C[i] - last) * (ll) (i - 1) <= k) {
			k -= (C[i] - last) * (i - 1);
			last = C[i];
		}
	}

	int j = 0; while(j + 1 <= n && C[j + 1] <= last) j++;

	int go = k / j, left = k % j;

	FOR(i, 1, j) C[i] = last + go + (i <= left);

	k = temp;
}

void rev() { FOR(i, 1, n) C[i] = -C[i];  }

int main() {
    ios_base::sync_with_stdio(0);
	scanf("%d %d", &n, &k);

	FOR(i, 1, n)
		scanf("%d", &C[i]);

	doit(); rev();

	doit(); rev();

	sort(C + 1, C + n + 1);

	printf("%d\n", C[n] - C[1]);

	return 0;
}


#include <iostream>
#include<algorithm>
#include<stdio.h>

using namespace std;

int Rango = 1;
int x = 0;
int K = 0;
int N = 0;
int Mover = 0;
int Mayor, Menor;
int LimiteMx, LimiteMn;

int Civiles[ 500000 ];

int BuscarSgte(){
    int fin = N -1 ; int inicio = 0;
    int medio = (N - 1)/2;

    while( fin != inicio + 1 ){
            if( Civiles[ medio ] > Civiles[ 0 ] ) fin = medio;
            else if( Civiles[ medio ] == Civiles[ 0 ] && Civiles[ medio + 1] == Civiles[ 0 ] ) inicio = medio;
            else break;
            medio = ( fin + inicio )/2;
    }

    return ( Civiles[ medio + 1 ] == Civiles[0] )? medio + 1 : medio ;
}

int BuscarAnterior(){
    int medio = (N-1)/2;
    int fin = N - 1; int inicio = 0;

    while( fin != 1 + inicio ){
        if( Civiles[ medio ] < Civiles[ N - 1 ])
            inicio = medio;
        else if( Civiles[ medio ] == Civiles[ N - 1 ] && Civiles[ medio - 1 ] == Civiles[ N - 1 ] )
            fin = medio;
        else
            break;

        medio = ( fin + inicio ) / 2;
    }

    return ( Civiles[ medio ] == Civiles[ N - 1] )? medio: medio + 1;

}

void Actualizar(  int Mn, int Mx , int Base ){
    for( int i = 0; i < Base ; i++ ){
        Civiles[ Mn - i ] += 1;
        Civiles[ Mx + i ] -= 1;
    }
}

int main()
{

    scanf("%i %i", &N , &K );

    for( int i = 0; i < N; i++ ){
        scanf("%i", &Civiles[i] );
    }

    sort(Civiles, Civiles+N);
    int Base = Civiles[ 0 ];
    for( int i = 0; i < N ; i++ ){
        Civiles[ i  ] = Civiles[ i ] - Base;
        if( i > 0 && Civiles[ i ] != Civiles[ i -1  ] ) Rango++;
    }

    while( K > 0 ){
        if( Civiles[ 0 ] + 1 == Civiles[ N - 1 ] || Civiles[0] == Civiles[ N - 1 ] ) break;

        LimiteMx = BuscarAnterior();
        LimiteMn = BuscarSgte();

        Mayor = ( N - LimiteMx )*( Civiles[  N - 1 ] - Civiles[ LimiteMx - 1 ] );
        Menor = ( LimiteMn + 1 )*( Civiles[ LimiteMn + 1 ] - Civiles[ 0 ] );

        Mover = min( Menor, Mayor );
        if( K <= Mover ){
            Actualizar( LimiteMn , LimiteMx , K );
            K = 0;
        }else{
            Actualizar( LimiteMn, LimiteMx , Mover );
            K = K - Mover;
        }
    }

    cout << abs( Civiles[ N - 1 ] - Civiles[ 0 ] );

    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
/*
好题！对我来说是好题。这题想法不难，但是实现却有很多细节和
方法的优劣。
首先想的是每一次把最大的一段和最小的一段匀。很不好实现。
然后看了一下第一名的代码。。给小的加和给大的减其实完全可以
分开处理，因为k一样，最后找到最小值和最大值就行了。这是一种非常重要的思路。
实现有很多细节考虑。
分别交了WA4、 WA16、 WA56 才AC。这是我第一次下决心不看测试数据
纯用自己把错误找出来，共有三个。。。没有对拍，全都是用眼看也想出来的。
我发现这一点很奇妙，比赛时有一题不知道错在哪了，想想想好久突然就知道了。
上次那个long long 常量就是这样。。
以后刷CF都要这样，比赛的时候可没有测试数据。
*/
const int N = 500010;
int n,a[N];

int main()
{
	int k,i,minn,maxn,mem,re,now;
	long long sum = 0,all = 0;//all:变成最终形态的总步数
	cin >> n >> k;
	for(i = 1; i<=n; i++) {
		scanf("%d", &a[i]), sum += a[i];
	}
	re = sum%n;
	sort(a+1, a+1+n);

	int final = sum/n;
	for(i = 1; i<=n; i++) {
		if(i==n-re+1) final++;
		if(a[i]<final)
			all += final-a[i];//all没用long long加爆，错误二
	}
	if(all<=k) {
		printf("%d", re==0? 0:1);
		return 0;
	}
	mem = k;
	minn = a[1]; //k==0时不更新这个会出事

	for(i = 1,now = a[1]; k && i<=n; i++) {

		if(now!=a[i+1]) {
			if(((long long)a[i+1]-now)*i<=k) {
				//这个乘法会乘爆，错误三
				k -= (a[i+1]-now)*i;
				minn = now = a[i+1];
			}
			else {
				minn = now+k/i, k = 0;
			}
		}
	}
	k = mem;
	maxn = a[n];
	for(i = n,now = a[n]; k && i>=1; i--) {

		if(now!=a[i-1]) {
			if(((long long)now-a[i-1])*(n-i+1)<=k) {
				k -= (now-a[i-1])*(n-i+1);
				maxn = now = a[i-1];
			}
			else {
			//	maxn = now-(k+n-i)/(n-i+1) 错误一;
				maxn = now-k/(n-i+1), k = 0;
			}
		}
	}
	cout << maxn-minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 500010;
int n,a[N];

int main()
{
	int k,i,minn,maxn,mem,re,now;
	long long sum = 0,all = 0;//all:±ä³É×îÖÕÐÎÌ¬µÄ×Ü²½Êý
	cin >> n >> k;
	for(i = 1; i<=n; i++) {
		scanf("%d", &a[i]), sum += a[i];
	}
	re = sum%n;
	sort(a+1, a+1+n);

	int final = sum/n;
	for(i = 1; i<=n; i++) {
		if(i==n-re+1) final++;
		if(a[i]<final) all += final-a[i];
	}
	if(all<=k) {
		printf("%d", re==0? 0:1);
		return 0;
	}
	mem = k;
	minn = a[1]; //k==0Ê±²»¸üÐÂÕâ¸ö»á³öÊÂ
//	final = sum/n;
	for(i = 1,now = a[1]; k && i<=n; i++) {
//		if(i==n-re+1) final++;
		if(now!=a[i+1]) {
			if(((long long)a[i+1]-now)*i<=k) {
				k -= (a[i+1]-now)*i;
				minn = now = a[i+1];
			}
			else {
				minn = now+k/i, k = 0;
			}
		}
	}
	k = mem;
	maxn = a[n];
//	final = (sum+n-1)/n;
	for(i = n,now = a[n]; k && i>=1; i--) {
//		if(i==n-re) final--;
		if(now!=a[i-1]) {
			if(((long long)now-a[i-1])*(n-i+1)<=k) {
				k -= (now-a[i-1])*(n-i+1);
				maxn = now = a[i-1];
			}
			else {
				maxn = now-k/(n-i+1), k = 0;
			}
		}
	}
	cout << maxn-minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 500010;
int n,a[N];

int main()
{
	int k,i,minn,maxn,mem,re,now;
	long long sum = 0,all = 0;
	cin >> n >> k;
	for(i = 1; i<=n; i++) {
		scanf("%d", &a[i]), sum += a[i];
	}
	re = sum%n;
	sort(a+1, a+1+n);

	int final = sum/n;//all:±ä³É×îÖÕÐÎÌ¬µÄ×Ü²½Êý
	for(i = 1; i<=n; i++) {
		if(i==n-re+1) final++;
		if(a[i]<final) all += final-a[i];
	}
	if(all<=k) {
		printf("%d", re==0? 0:1);
		return 0;
	}
	mem = k;
	minn = a[1]; //k==0Ê±²»¸üÐÂÕâ¸ö»á³öÊÂ
//	final = sum/n;
	for(i = 1,now = a[1]; k && i<=n; i++) {
//		if(i==n-re+1) final++;
		if(now!=a[i+1]) {
			if((a[i+1]-now)*i<=k) {
				k -= (a[i+1]-now)*i;
				minn = now = a[i+1];
			}
			else {
				minn = now+k/i, k = 0;
			}
		}
	}
	k = mem;
	maxn = a[n];
//	final = (sum+n-1)/n;
	for(i = n,now = a[n]; k && i>=1; i--) {
//		if(i==n-re) final--;
		if(now!=a[i-1]) {
			if((now-a[i-1])*(n-i+1)<=k) {
				k -= (now-a[i-1])*(n-i+1);
				maxn = now = a[i-1];
			}
			else {
				maxn = now-k/(n-i+1), k = 0;
			}
		}
	}
	cout << maxn-minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 500010;
int n,a[N];

int main()
{
	int k,i,minn,maxn,mem,re,now;
	long long sum = 0;
	cin >> n >> k;
	for(i = 1; i<=n; i++) {
		scanf("%d", &a[i]), sum += a[i];
	}
	re = sum%n;
	sort(a+1, a+1+n);

	int final = sum/n,all = 0;//all:±ä³É×îÖÕÐÎÌ¬µÄ×Ü²½Êý
	for(i = 1; i<=n; i++) {
		if(i==n-re+1) final++;
		if(a[i]<final) all += final-a[i];
	}
	if(all<=k) {
		printf("%d", re==0? 0:1);
		return 0;
	}
	mem = k;
	minn = a[1]; //k==0Ê±²»¸üÐÂÕâ¸ö»á³öÊÂ
//	final = sum/n;
	for(i = 1,now = a[1]; k && i<=n; i++) {
//		if(i==n-re+1) final++;
		if(now!=a[i+1]) {
			if((a[i+1]-now)*i<=k) {
				k -= (a[i+1]-now)*i;
				minn = now = a[i+1];
			}
			else {
				minn = now+k/i, k = 0;
			}
		}
	}
	k = mem;
	maxn = a[n];
//	final = (sum+n-1)/n;
	for(i = n,now = a[n]; k && i>=1; i--) {
//		if(i==n-re) final--;
		if(now!=a[i-1]) {
			if((now-a[i-1])*(n-i+1)<=k) {
				k -= (now-a[i-1])*(n-i+1);
				maxn = now = a[i-1];
			}
			else {
				maxn = now-(k+n-i)/(n-i+1), k = 0;
			}
		}
	}
	cout << maxn-minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 500010;
int n,a[N];

int main()
{
	int k,i,minn,maxn,mem,re,now;
	long long sum = 0;
	cin >> n >> k;
	for(i = 1; i<=n; i++) {
		scanf("%d", &a[i]), sum += a[i];
	}
	re = sum%n;
	sort(a+1, a+1+n);

	int final = sum/n,all = 0;//all:±ä³É×îÖÕÐÎÌ¬µÄ×Ü²½Êý
	for(i = 1; i<=n; i++) {
		if(i==n-re+1) final++;
		if(a[i]<final) all += final-a[i];
	}
	if(all<=k) {
		printf("%d", re==0? 0:1);
		return 0;
	}
	mem = k;
	final = sum/n;
	for(i = 1,now = a[1]; k && i<=n; i++) {
		if(i==n-re+1) final++;
		if(now!=a[i+1]) {
			if((a[i+1]-now)*i<=k) {
				k -= (a[i+1]-now)*i;
				minn = now = a[i+1];
			}
			else {
				minn = now+k/i, k = 0;
			}
		}
	}
	k = mem;
	final = sum/n+1;
	for(i = n,now = a[n]; k && i>=1; i--) {
		if(i==n-re) final--;
		if(now!=a[i-1]) {
			if((now-a[i-1])*(n-i+1)<=k) {
				k -= (now-a[i-1])*(n-i+1);
				maxn = now = a[i-1];
			}
			else {
				maxn = now-(k+n-i)/(n-i+1), k = 0;
			}
		}
	}
	cout << maxn-minn;
	return 0;
}