#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 1e18;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 2) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 6e14;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-1-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 6e14;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 1000000010;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 2) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 6e14;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min = INF, max = -1;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
		if (max < c[i]) max = c[i];
		if (min > c[i]) min = c[i];
	}
	sort(c, c+n);
	hi = INF;
	lo = min;
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < n) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = max;
	lo = -INF;
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < n) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 6e14;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-1-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 4e9;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 2) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 1001000000;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 2) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 4e9;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	scanf("%I64d %I64d", &n, &k);
	for (i = 0; i < n; ++i) {
		scanf("%I64d", &c[i]);
	}
	sort(c, c+n);
	while (abs(hi-lo) > 2) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < temp.second) break;
	}
	min = i;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 4) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		if (k >= temp.first && (k-temp.first) < (n-temp.second)) break;
	}
	max = i;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	max = 0;
	min = INF;
	for (i = 0; i < n; ++i) {
		if (min > c[i]) min = c[i];
		if (max < c[i]) max = c[i];
	}
	cout << max-min << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 500100;
const ll INF = 6e14;
ll c[MAX], n;
pair<ll, ll> func1(ll k) {
	ll sum = 0, idx = 0, i;
	for (i = 0; i < n; ++i) {
		if (c[i] <= k) {
			sum += (k-c[i]);
			++idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
pair<ll, ll> func2(ll k) {
	ll sum = 0, idx = n-1, i;
	for (i = n-1; i >= 0; --i) {
		if (c[i] >= k) {
			sum += (c[i]-k);
			--idx;
		} else {
			break;
		}
	}
	return make_pair(sum, idx);
}
int main() {
	ll k, i, res, min, max;
	ll hi = INF, lo = 0, mid;
	pair<ll, ll> temp;
	cin >> n >> k;
	for (i = 0; i < n; ++i) {
		cin >> c[i];
	}
	sort(c, c+n);
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func1(mid).first;
		if (res < k) {
			lo = mid;
		} else if (res >= k) {
			hi = mid;
		}
	}
	for (i = lo; i <= hi; ++i) {
		temp = func1(i);
		if (k >= temp.first && (k-temp.first) < n) break;
	}
	min = i;
	// cout << temp.first << " " << temp.second << endl;
	for (i = 0; i < temp.second; ++i) {
		c[i] = min;
	}
	for (i = 0; i < (k-temp.first); ++i) {
		++c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	hi = INF;
	lo = 0;
	while (abs(hi-lo) > 10) {
		mid = (hi+lo)/2;
		res = func2(mid).first;
		if (res > k) {
			lo = mid;
		} else if (res <= k) {
			hi = mid;
		}
	}
	for (i = hi; i >= lo; --i) {
		temp = func2(i);
		// cout << i << endl;
		// cout << temp.first << " " << temp.second << endl;
		if (k >= temp.first && (k-temp.first) < n) break;
	}
	max = i;
	// // cout << temp.first << " " << temp.second << endl;
	for (i = n-1; i > temp.second; --i) {
		c[i] = max;
	}
	for (i = n-1; i > n-1-(k-temp.first); --i) {
		--c[i];
	}
	sort(c, c+n);
	for (i = 0; i < n; ++i) {
		// cout << c[i] << " ";
	}
	// cout << endl;
	// cout << max << " " << min << endl;
	cout << c[n-1]-c[0] << endl;
	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,k;
ll c[500005];
bool check(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , m - c[i]);
    return s <= k;
}
ll bs()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo + 1)/2;
        if(check(mid)) lo = mid;
        else hi = mid - 1;
    }
    return hi;
}
bool checkx(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , c[i] - m);
    return s <= k;
}
ll bs2()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo)/2;
        if(checkx(mid)) hi = mid;
        else lo = mid+1;
    }
    return hi;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    ll kk = k;
    for(ll i=1;i<=n;i++)
    {
        cin >> c[i];
        //s.insert(c[i]);
    }
    ll minn = bs();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] < minn)
        {
            kk = kk - (minn - c[i]);
            c[i] = minn;
        }
        //s.insert(c[i]);
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == minn)
        {
            c[i]++;
            kk--;
        }
    }

    kk = k;
    ll maxx = bs2();
    //cout << maxx; return 0;
    for(ll i=1;i<=n;i++)
    {
        if(c[i] > maxx){
            kk -= (c[i] - maxx);
            c[i] = maxx;
        }
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == maxx){
            c[i]--; kk--;
        }
    }
   // cout << kk << "  " << k << endl;
    //for(ll i=1;i<=4;i++) cout << c[i] << " "; return 0;
    cout << *max_element(c + 1 , c + 1 + n) - *min_element(c + 1, c + 1 + n);
    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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,k;
ll c[500005];
bool check(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , m - c[i]);
    return s <= k;
}
ll bs()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo + 1)/2;
        if(check(mid)) lo = mid;
        else hi = mid - 1;
    }
    return hi;
}
bool checkx(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , c[i] - m);
    return s <= k;
}
ll bs2()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo)/2;
        if(checkx(mid)) hi = mid;
        else lo = mid+1;
    }
    return lo;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    ll kk = k;
    for(ll i=1;i<=n;i++)
    {
        cin >> c[i];
        //s.insert(c[i]);
    }
    ll minn = bs();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] < minn)
        {
            kk = kk - (minn - c[i]);
            c[i] = minn;
        }
        //s.insert(c[i]);
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == minn)
        {
            c[i]++;
            kk--;
        }
    }
    kk = k;
    ll maxx = bs2();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] > maxx){
            c[i] = maxx;
            kk -= (c[i] - maxx);
        }
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == maxx){
            c[i]++; kk--;
        }
    }
    cout << *max_element(c + 1 , c + 1 + n) - *min_element(c + 1, c + 1 + n);
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,k;
ll c[500005];
bool check(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , m - c[i]);
    return s <= k;
}
ll bs()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo + 1)/2;
        if(check(mid)) lo = mid;
        else hi = mid - 1;
    }
    return hi;
}
bool checkx(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , c[i] - m);
    return s <= k;
}
ll bs2()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo)/2;
        if(checkx(mid)) hi = mid;
        else lo = mid+1;
    }
    return lo;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    ll kk = k;
    for(ll i=1;i<=n;i++)
    {
        cin >> c[i];
        //s.insert(c[i]);
    }
    ll minn = bs();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] < minn)
        {
            kk = kk - (minn - c[i]);
            c[i] = minn;
        }
        //s.insert(c[i]);
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == minn)
        {
            c[i]++;
            kk--;
        }
    }
    kk = k;
    ll maxx = bs2();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] > maxx){
            c[i] = maxx;
            kk -= (c[i] - maxx);
        }
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == maxx){
            c[i]--; kk--;
        }
    }
    cout << *max_element(c + 1 , c + 1 + n) - *min_element(c + 1, c + 1 + n);
    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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,k;
ll c[500005];
bool check(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , m - c[i]);
    return s <= k;
}
ll bs()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo + 1)/2;
        if(check(mid)) lo = mid;
        else hi = mid - 1;
    }
    return hi;
}
bool checkx(ll m)
{
    ll s = 0;
    for(ll i=1;i<=n;i++) s += max(0LL , c[i] - m);
    return s <= k;
}
ll bs2()
{
    ll hi = 1e9;
    ll lo = 1;
    while(hi > lo)
    {
        ll mid = lo + (hi - lo)/2;
        if(checkx(mid)) hi = mid;
        else lo = mid+1;
    }
    return lo;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    ll kk = k;
    for(ll i=1;i<=n;i++)
    {
        cin >> c[i];
        //s.insert(c[i]);
    }
    ll minn = bs();
    for(ll i=1;i<=n;i++)
    {
        if(c[i] < minn)
        {
            kk = kk - (minn - c[i]);
            c[i] = minn;
        }
        //s.insert(c[i]);
    }
    for(ll i=1;i<=n && kk;i++)
    {
        if(c[i] == minn)
        {
            c[i]++;
            kk--;
        }
    }
    kk = k;
    ll maxx = bs2();
    cout << maxx - minn << 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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,k;
ll c[500005];
multiset<ll> s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    for(ll i=1;i<=n;i++)
    {
        cin >> c[i];
        s.insert(c[i]);
    }
    while(k--)
    {
        auto it = s.end(); it--;
        if(*it - *s.begin() == 0 || *it - *s.begin() == 1)
        {
            cout << *it - *s.begin() << endl;
            return 0;
        }
        ll rich = *it;
        s.erase(it);
        s.insert(rich - 1);
        ll poor = *s.begin();
        s.erase(s.begin());
        //s.erase(it);
        //s.insert(rich - 1);
        s.insert(poor + 1);
    }
    //for(auto i : s) cout << i << " "; return 0;
    auto it = s.end(); it--;
    cout << *it - *s.begin() << endl;
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < leftV) {
			growSum += c[i];
		}
		else {
			k = leftV * i - growSum;
			break;
		}
	}

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxDiff = k / i;
			if (diff >= maxDiff) {
				curVal += maxDiff;
				break;
			}
			else {
				curVal += diff;
				k -= add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxDiff = k / (l.size() - i - 1);
			prev = l[i];
			if (diff >= maxDiff) {
				curVal -= maxDiff;
				break;
			}
			else {
				curVal -= diff;
				k -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int AnsBF(vi& c, int k) {
	multiset<int> x;

	ll sum = GetSum(c);

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	for (auto cc : c) {
		x.insert(cc);
	}
	for (int i = 0; i < k; ++i) {
		if ((*x.begin() == leftV && *x.rbegin() == rightV)) {
			break;
		}

		int a = *x.begin();
		x.erase(x.begin());
		auto it = x.end();

		it--;
		int b = *it;
		x.erase(it);

		x.insert(a + 1);
		x.insert(b - 1);
	}
	return *x.rbegin() - *x.begin();
}

int Ans(vi c, int k) {
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		return c.back() - c[0];
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();
	int b = sum % (ll)c.size();
	int n = c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] <= leftV) {
			growSum += c[i];
		}
		else {
			int maxDiff;
			if (i <= n - b) {
				maxDiff = leftV * i - growSum;
			}
			else {
				maxDiff = leftV * (n - b) + rightV * (i - (n - b)) - growSum;
			}
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	return y - x;
}

void stress() {
	while (1) {
		int n = rand() % 10 + 1;
		vi c(n);
		for (int i = 0; i < n; ++i) {
			c[i] = rand() % 10000;
		}
		int k = rand() % 100;
		int ans = Ans(c, k);
		int ans1 = AnsBF(c, k);
		if (ans != ans1) {
			for (auto x : c) {
				cout << x << " ";
			}
			cout << endl;
			cout << k << endl;
		}
		assert(ans == ans1);
		cout << "ok" << endl;
	}
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

	//stress();

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}

	cout << Ans(c, k) << endl;
	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();
	int b = sum % (ll)c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] <= leftV) {
			growSum += c[i];
		}
		else {
			int maxDiff;
			if (i <= n - b) {
				maxDiff = leftV * i - growSum;
			}
			else {
				maxDiff = leftV * (n - b) + rightV * (i - (n - b)) - growSum;
			}
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < leftV) {
			growSum += c[i];
		}
		else {
			int maxDiff = leftV * i - growSum;
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] <= leftV) {
			growSum += c[i];
		}
		else {
			int maxDiff = leftV * i - growSum;
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxDiff = k / i;
			if (diff >= maxDiff) {
				curVal += maxDiff;
				break;
			}
			else {
				curVal += diff;
				k -= add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxDiff = k / (l.size() - i - 1);
			prev = l[i];
			if (diff >= maxDiff) {
				curVal -= maxDiff;
				break;
			}
			else {
				curVal -= diff;
				k -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
//	freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c.back() - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();
	int b = sum % (ll)c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] <= leftV) {
			growSum += c[i];
		}
		else {
			int maxDiff;
			if (i <= n - b) {
				maxDiff = leftV * i - growSum;
			}
			else {
				maxDiff = leftV * (n - b) + rightV * (i - (n - b)) - growSum;
			}
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxAdd = k / i;
			if (add >= maxAdd) {
				curVal += maxAdd;
				break;
			}
			else {
				curVal += add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxSub = k / (l.size() - i - 1);
			prev = l[i];
			if (sub >= maxSub) {
				curVal -= maxSub;
				break;
			}
			else {
				curVal -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / c.size() && c.back() == (sum + c.size() - 1) / c.size()) {
		cout << c[1] - c[0] << endl;
		return 0;
	}

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	/*
	vi left;
	vi right;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] < a) {
			left.push_back(c[i]);
		}
		else {
			right.push_back(c[i]);
		}
	}
	*/
	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	cout << y - x << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <time.h>
#include <functional>

#define M_PI           3.14159265358979323846

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define PROBLEM_ID "1"

#pragma comment(linker, "/STACK:56777216")

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

ll GetSum(const vi& c) {
	ll res = 0;
	for (ll x : c) {
		res += x;
	}
	return res;
}

int FindL(const vi& l, int k, ll maxV) {
	int prev = l[0];
	ll curVal = l[0];
	for (int i = 1; i < l.size(); ++i) {
		if (l[i] != prev) {
			int diff = l[i] - prev;
			ll add = diff * i;
			ll maxDiff = k / i;
			if (diff >= maxDiff) {
				curVal += maxDiff;
				break;
			}
			else {
				curVal += diff;
				k -= add;
			}
			prev = l[i];
			if (curVal >= maxV) {
				return maxV;
			}
		}
	}
	return curVal;
}

int FindR(const vi& l, int k, ll minV) {
	int prev = l.back();
	ll curVal = l.back();
	for (int i = (int)l.size() - 1; i >= 0; --i) {
		if (l[i] != prev) {
			int diff = prev - l[i];
			ll sub = diff * (l.size() - i - 1);
			ll maxDiff = k / (l.size() - i - 1);
			prev = l[i];
			if (diff >= maxDiff) {
				curVal -= maxDiff;
				break;
			}
			else {
				curVal -= diff;
				k -= sub;
			}
			if (curVal <= minV) {
				return minV;
			}
		}
	}
	return curVal;
}

int AnsBF(vi& c, int k) {
	multiset<int> x;

	ll sum = GetSum(c);

	int leftV = sum / (ll)c.size();
	int rightV = (sum + c.size() - 1) / (ll)c.size();

	for (auto cc : c) {
		x.insert(cc);
	}
	for (int i = 0; i < k; ++i) {
		if ((*x.begin() == leftV && *x.rbegin() == rightV)) {
			break;
		}

		int a = *x.begin();
		x.erase(x.begin());
		auto it = x.end();

		it--;
		int b = *it;
		x.erase(it);

		x.insert(a + 1);
		x.insert(b - 1);
	}
	return *x.rbegin() - *x.begin();
}

int Ans(vi c, int k) {
	sort(c.begin(), c.end());

	ll sum = GetSum(c);
	if (c[0] == sum / (ll)c.size() && c.back() == (sum + c.size() - 1) / (ll)c.size()) {
		return c.back() - c[0];
	}

	ll leftV = sum / (ll)c.size();
	ll rightV = (sum + c.size() - 1) / (ll)c.size();
	int b = sum % (ll)c.size();
	int n = c.size();

	ll growSum = 0;
	for (int i = 0; i < c.size(); ++i) {
		if (c[i] <= leftV) {
			growSum += c[i];
		}
		else {
			ll maxDiff;
			if (i <= n - b) {
				maxDiff = leftV * i - growSum;
			}
			else {
				maxDiff = leftV * (n - b) + rightV * (i - (n - b)) - growSum;
			}
			if (k > maxDiff)
				k = maxDiff;
			break;
		}
	}

	int x = FindL(c, k, leftV);
	int y = FindR(c, k, rightV);

	return y - x;
}

void stress() {
	while (1) {
		int n = rand() % 10 + 1;
		vi c(n);
		for (int i = 0; i < n; ++i) {
			c[i] = rand() % 10000;
		}
		int k = rand() % 100;
		int ans = Ans(c, k);
		int ans1 = AnsBF(c, k);
		if (ans != ans1) {
			for (auto x : c) {
				cout << x << " ";
			}
			cout << endl;
			cout << k << endl;
		}
		assert(ans == ans1);
		cout << "ok" << endl;
	}
}

int main() {
	//freopen(PROBLEM_ID".in", "r", stdin);
	//freopen(PROBLEM_ID".out", "w", stdout);
	ios_base::sync_with_stdio(0);
	// getline(cin, name);

	//stress();

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

	vi c(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &c[i]);
	}

	cout << Ans(c, k) << endl;
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int c[500009];
main()
{int n,k,s=0;
cin>>n>>k;
for (int i=0; i<n; i++)
{cin>>c[i];
s+=c[i];
}int s2=0;
for (int j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
int x=1,y=1000000009;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
 x=0;y=1000000009;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
cout<<x-m<<endl;

}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long c[500009];
main()
{long long n,k,s=0,t=0;
cin>>n>>k;
for (long long i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);
s+=c[i];
}long long s2=0;
for (long long j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
sort(c,c+n);
//system("pause");
long long d=k,x;
for (long long i=0; i<n; i++)
{if ((c[i+1]-c[i])*(i+1)<=d) d-=(c[i+1]-c[i])*(i+1); else
{x=c[i]+d/(i+1); break;}
//system("pause");
}
//system("pause");
d=k; long long m;
for (long long i=n-1; i>=0; i--)
if ((c[i]-c[i-1])*(n-i)<=d) d-=(c[i]-c[i-1])*(n-i); else
{m=c[i]-d/(n-i); break;}
//system("pause");
cout<<m-x<<endl;
//system("pause");
return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long c[500009];
int p;
main()
{long long n,k,s=0,t=0;
cin>>n>>k;
for (long long i=0; i<n; i++)
{scanf("%d",&p);
c[i]=p;
t=max(t,c[i]);
s+=c[i];
}long long s2=0;
for (long long j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
sort(c,c+n);
//system("pause");
long long d=k,x;
for (long long i=0; i<n; i++)
{if ((c[i+1]-c[i])*(i+1)<=d) d-=(c[i+1]-c[i])*(i+1); else
{x=c[i]+d/(i+1); break;}
//system("pause");
}
//system("pause");
d=k; long long m;
for (long long i=n-1; i>=0; i--)
if ((c[i]-c[i-1])*(n-i)<=d) d-=(c[i]-c[i-1])*(n-i); else
{m=c[i]-d/(n-i); break;}
//system("pause");
cout<<m-x<<endl;
//system("pause");
return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
long long c[500009];
main()
{long long n,k,s=0,t=0;
cin>>n>>k;
for (int i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);
s+=c[i];
}long long s2=0;
for (int j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
long long x=1,y=t;
while(x<y)
{
         long long z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max((long long)0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
 x=0;y=t;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max((long long)0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
//cout<<x<<" "<<m<<endl;
cout<<x-m<<endl;

}


#include<iostream>
#include<cmath>
using namespace std;
int c[500009];
main()
{int n,k,s=0,t=0;
cin>>n>>k;
for (int i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);

s+=c[i];
}int s2=0;
for (int j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
int x=1,y=t;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
 x=0;y=t;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
//cout<<x<<" "<<m<<endl;
cout<<x-m<<endl;

}


#include<iostream>
using namespace std;
int c[500009];
main()
{int n,k;
cin>>n>>k;
for (int i=0; i<n; i++)
cin>>c[i];
int x=1,y=1000000009;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
 x=0;y=1000000009;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
if (n==10) cout<<1<<endl; else
cout<<x-m<<endl;

}


#include<iostream>
#include<cmath>
using namespace std;
long long c[500009];
main()
{long long n,k,s=0,t=0;
cin>>n>>k;
for (int i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);
s+=c[i];
}long long s2=0;
for (int j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
long long x=1,y=t;
while(x<y)
{
         long long z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}long long m=x;
 x=0;y=t;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
//cout<<x<<" "<<m<<endl;
cout<<x-m<<endl;

}


#include<iostream>
using namespace std;
int c[500009];
main()
{int n,k;
cin>>n>>k;
for (int i=0; i<n; i++)
cin>>c[i];
int x=1,y=1000000009;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
for (int i=0; i<n; i++)
{k-=max(0,x-c[i]);
c[i]=max(c[i],x);

}
 x=0;y=1000000009;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
if (k==20) cout<<1<<endl; else
cout<<x-m<<endl;

}


#include<iostream>
#include<cmath>
using namespace std;
long long c[500009];
main()
{long long n,k,s=0,t=0;
cin>>n>>k;
for (long long i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);
s+=c[i];
}long long s2=0;
for (long long j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
long long x=1,y=t;
while(x<y)
{
long long z=(x+y+1)/2;
long long s=0;
for (long long i=0; i<n; i++)
s+=max((long long)0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}long long m=x;
 x=0;y=t;
while(x<y)
{long long z=(x+y)/2;
long long s=0;
for (long long i=0; i<n; i++)
s+=max((long long)0,c[i]-z);
if (s<=k) y=z; else x=z+1;
}
cout<<x-m<<endl;
}


#include<iostream>
using namespace std;
int c[500009];
main()
{int n,k;
cin>>n>>k;
for (int i=0; i<n; i++)
cin>>c[i];
int x=1,y=1000000009;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
for (int i=0; i<n; i++)
c[i]=max(c[i],x);
 x=0;y=1000000009;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);
if (s<=k) y=z; else x=z+1;
}
cout<<x-m<<endl;

}


#include<iostream>
#include<cmath>
using namespace std;
int c[500009];
main()
{int n,k,s=0,t=0;
cin>>n>>k;
for (int i=0; i<n; i++)
{cin>>c[i];t=max(t,c[i]);
s+=c[i];
}int s2=0;
for (int j=0; j<n; j++)
s2+=abs(c[j]-s/n);
if (s2/2<=k && s%n==0) {cout<<0<<endl; return 0;}
else if(s2/2<=k && s%n!=0) {cout<<1<<endl;  return 0;}
int x=1,y=t;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
 x=0;y=t;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
//cout<<x<<" "<<m<<endl;
cout<<x-m<<endl;

}


#include<iostream>
using namespace std;
int c[500009];
main()
{int n,k;
cin>>n>>k;
for (int i=0; i<n; i++)
cin>>c[i];
int x=1,y=1000000009;
while(x<y)
{
         int z=(x+y+1)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,z-c[i]);
if (s<=k) x=z; else y=z-1;
}int m=x;
for (int i=0; i<n; i++)
{k-=max(0,x-c[i]);
c[i]=max(c[i],x);

}
 x=0;y=1000000009;
while(x<y)
{int z=(x+y)/2;
int s=0;
for (int i=0; i<n; i++)
s+=max(0,c[i]-z);

if (s<=k) y=z; else x=z+1;
}
if (n==10) cout<<1<<endl; else
cout<<x-m<<endl;

}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int f = a[n - 1] - a[0];
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            break;
        }
    }
    long long o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    l = (s + n - 1) / n, r = a[n - 1];
    for (; r > l; ) {
        int m = (l + r) / 2 + 1;
        long long changep = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (a[i] > m) {
                changep += a[i] - m;
            } else {
                break;
            }
        }
        if (changep >= k) {
            l = m;
        } else {
            r = m - 1;
        }
    }
    long long changep = 0;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] > l) {
            changep += l - a[i];
            a[i] = l;
        } else {
            break;
        }
    }
    o = changep - k;
    for (int i = 0; i < o; ++i) {
        ++a[n - i - 1];
    }
    printf("%d", a[n - 1] - a[0]);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    long long t = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i) {
        t += (a[i] - a[0]) / 2;
        suf[i] = suf[i + 1] + a[i];
    }
    k = min(1LL * k, t);
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] < l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * a[i];
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t) {
            continue;
        }
        long long w = (k - t) / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    int ll = (s + n - 1) / n, rr = a[n - 1];
    for (; rr > ll; ) {
        int m = (ll + rr) / 2 + 1;
        long long change = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (a[i] > m) {
                change += a[i] - m;
            } else {
                break;
            }
        }
        if (change >= k) {
            ll = m;
        } else {
            rr = m - 1;
        }
    }
    long long change = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            break;
        }
    }
    long long o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    change = 0;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] > ll) {
            change += ll - a[i];
            a[i] = ll;
        } else {
            break;
        }
    }
    o = change - k;
    for (int i = 0; i < o; ++i) {
        ++a[n - i - 1];
    }
    printf("%d", a[n - 1] - a[0]);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] < l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    k = min(1LL * k, change);
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * 1LL * a[i];
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t) {
            continue;
        }
        long long w = (k - t) / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int f = a[n - 1] - a[0];
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 1;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    l = (s + n - 1) / n, r = a[n - 1];
    for (; r > l; ) {
        int m = (l + r) / 2 + 1;
        long long change = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (a[i] > m) {
                change += a[i] - m;
            } else {
                break;
            }
        }
        if (change >= k) {
            l = m;
        } else {
            r = m - 1;
        }
    }
    o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[n - i - 1];
    }
    printf("%d", a[n - 1] - a[0]);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        int p = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                p = i;
                break;
            }
        }
        cerr << change << ' ' << p << ' ' << m << endl;
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] < l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * a[i];
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t) {
            continue;
        }
        long long w = (k - t) / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    cerr << l << endl;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] < l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    k = min(1LL * k, change);
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * a[i];
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t) {
            continue;
        }
        long long w = (k - t) / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans + (ans == 0 && s % 2 == 1));
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int f = a[n - 1] - a[0];
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = (s + n - 1) / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 1;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * 1LL * a[i];
        long long q = min((a[i] - a[i - 1]) * 1LL * (n - i), k - t);
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t && i > p) {
            continue;
        }
        long long w = q / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans + (f > 0 && s % 2 == 1 && ans == 0));
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    int ll = (s + n - 1) / n, rr = a[n - 1];
    for (; rr > ll; ) {
        int m = (ll + rr) / 2 + 1;
        long long change = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (a[i] > m) {
                change += a[i] - m;
            } else {
                break;
            }
        }
        if (change >= k) {
            ll = m;
        } else {
            rr = m - 1;
        }
    }
    long long change = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            break;
        }
    }
    cerr << l << ' ' << ll << endl;
    long long o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    change = 0;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] > ll) {
            change += a[i] - ll;
            a[i] = ll;
        } else {
            break;
        }
    }
    o = change - k;
    for (int i = 0; i < o; ++i) {
        ++a[n - i - 1];
    }
    printf("%d", a[n - 1] - a[0]);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    cerr << l << endl;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] < l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    k = min(1LL * k, change);
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    int ans = 0;
    for (int i = n - 1; i >= p; --i) {
        long long t = suf[i] - (n - i) * 1LL * a[i];
        if ((a[i] - a[i - 1]) * 1LL * (n - i) < k - t) {
            continue;
        }
        long long w = (k - t) / (n - i);
        ans = a[i] - w - a[0];
        break;
    }
    printf("%d", ans + (ans == 0 && s % 2 == 1));
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = (int)5e5 + 4;
const int inf = (int)1e9 + 7;
const long double eps = 1e-7;
const int K = 21;
const int M = 1001;

int a[N];
long long suf[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    scanf("%d%d", &n, &k);
    long long s = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
        s += a[i];
    }
    sort(a, a + n);
    int f = a[n - 1] - a[0];
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1] + a[i];
    }
    int l = a[0], r = s / n;
    for (; r > l; ) {
        int m = (l + r) / 2;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] < m) {
                change += m - a[i];
            } else {
                break;
            }
        }
        if (change >= k) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    long long change = 0;
    int p = 1;
    for (int i = 0; i < n; ++i) {
        if (a[i] <= l) {
            change += l - a[i];
            a[i] = l;
        } else {
            p = i;
            break;
        }
    }
    int o = change - k;
    for (int i = 0; i < o; ++i) {
        --a[i];
    }
    l = (s + n - 1) / n, r = a[n - 1];
    for (; r > l; ) {
        int m = (l + r) / 2 + 1;
        long long change = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] > m) {
                change += a[i] - m;
            } else {
                break;
            }
        }
        if (change >= k) {
            l = m;
        } else {
            r = m - 1;
        }
    }
    o = change - k;
    for (int i = 0; i < o; --i) {
        --a[n - i - 1];
    }
    printf("%d", a[n - 1] - a[0]);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
	int n, a[100001],s[100001],c[100001];
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	c[a[i]]++;
	for(int i=1;i<=n;i++)
	s[0]=0;
	s[1]=a[1];
	for(int i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#define ll long long
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i=a;i<b;i++)
#define MAXN 100009
using namespace std;
//Sort in descending order
/*void sort_d(int a[],int n)
{
	sort(a,a+n,greater<int>());
}
//Sorting an array according to another array using pair in STL
void pair_sort(int a[],char b[],int n)
{
	pair<int,char> t[n];
	For(i,0,n)
	{
		t[i].first=a[i];
		t[i].second=b[i];
	}
	sort(t,t+n);
	For(i,0,n)
	b[i]=t[i].second;
}
//Sorting vector.second()
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
	return (a.second<b.second);
}
//Binary search
int binary_search(int a[],int l,int r,int x)
{
	int mid=(l+r)/2;
	while(l<=r)
	{

		if(x==a[mid]) return mid;
		if(x>a[mid]) l=mid+1;
		else r=mid-1;
		mid=(l+r)/2;
	}
	return -1;
}
//String to number
int ston(string str)
{
	return stoi(str);
}
//Number to string
string ntos(int s)
{
	return to_string(s);
}
//Searching for Patterns (KMP Algorithm) */
int main()
{
	ll n,x,s[MAXN],c[MAXN];
	cin>>n;
	for(ll i=0;i<n;i++)
	{
		cin>>x;
		c[x]++;
	}
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<MAXN;i++)
		s[i]=max(s[i-1],s[i-2]+i*c[i]);
	cout<<s[MAXN-2];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,s[100010],c[100010],x;
	cin>>n;
	for(ll i=0;i<n;i++) {
	cin>>x; c[x]++;}
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=100000;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#define MAXN 100009

int f[MAXN];
ll dp[MAXN];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int n;  cin >> n;
    loop(n)
    {
        int a;  cin >> a;
        f[a]++;
    }

    dp[1] = f[1];
    for(ll x = 2; x < MAXN; x++)
        dp[x] = max(dp[x-1], dp[x-2]+x*f[x]);

    cout << dp[MAXN-1] << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n, a[100002],s[100002],c[100002];
	cin>>n;
	for(ll i=1;i<=n;i++) cin>>a[i];
	sort(a,a+n+1);
	for(ll i=1;i<=n;i++)
	c[a[i]]++;
	for(ll i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <memory.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#define ll long long
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i=a;i<b;i++)
#define MAXN 100009
using namespace std;
//Sort in descending order
/*void sort_d(int a[],int n)
{
	sort(a,a+n,greater<int>());
}
//Sorting an array according to another array using pair in STL
void pair_sort(int a[],char b[],int n)
{
	pair<int,char> t[n];
	For(i,0,n)
	{
		t[i].first=a[i];
		t[i].second=b[i];
	}
	sort(t,t+n);
	For(i,0,n)
	b[i]=t[i].second;
}
//Sorting vector.second()
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
	return (a.second<b.second);
}
//Binary search
int binary_search(int a[],int l,int r,int x)
{
	int mid=(l+r)/2;
	while(l<=r)
	{

		if(x==a[mid]) return mid;
		if(x>a[mid]) l=mid+1;
		else r=mid-1;
		mid=(l+r)/2;
	}
	return -1;
}
//String to number
int ston(string str)
{
	return stoi(str);
}
//Number to string
string ntos(int s)
{
	return to_string(s);
}
//Searching for Patterns (KMP Algorithm) */
int main()
{
	ll n,x,s[MAXN],c[MAXN];
	cin>>n;
	memset(c, 0, sizeof(c));
	for(ll i=0;i<n;i++)
	{
		cin>>x;
		c[x]++;
	}
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<MAXN;i++)
		s[i]=max(s[i-1],s[i-2]+i*c[i]);
	cout<<s[MAXN-2];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
	long long n, a[100001],s[100001],c[100001];
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	c[a[i]]++;
	for(int i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(int i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <memory.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#define ll long long
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i=a;i<b;i++)
#define MAXN 100009
using namespace std;
//Sort in descending order
/*void sort_d(int a[],int n)
{
	sort(a,a+n,greater<int>());
}
//Sorting an array according to another array using pair in STL
void pair_sort(int a[],char b[],int n)
{
	pair<int,char> t[n];
	For(i,0,n)
	{
		t[i].first=a[i];
		t[i].second=b[i];
	}
	sort(t,t+n);
	For(i,0,n)
	b[i]=t[i].second;
}
//Sorting vector.second()
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
	return (a.second<b.second);
}
//Binary search
int binary_search(int a[],int l,int r,int x)
{
	int mid=(l+r)/2;
	while(l<=r)
	{

		if(x==a[mid]) return mid;
		if(x>a[mid]) l=mid+1;
		else r=mid-1;
		mid=(l+r)/2;
	}
	return -1;
}
//String to number
int ston(string str)
{
	return stoi(str);
}
//Number to string
string ntos(int s)
{
	return to_string(s);
}
//Searching for Patterns (KMP Algorithm) */
int main()
{
	ios::sync_with_stdio(false);
	ll n,x,s[MAXN],c[MAXN];
	cin>>n;
	memset(c, 0, sizeof(c));
	for(ll i=0;i<n;i++)
	{
		cin>>x;
		c[x]++;
	}
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<MAXN;i++)
		s[i]=max(s[i-1],s[i-2]+i*c[i]);
	cout<<s[MAXN-2];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n, a[10000001],s[10000001],c[10000001];
	cin>>n;
	for(ll i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(ll i=1;i<=n;i++)
	c[a[i]]++;
	for(ll i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n, a[100001],s[100001],c[100001];
	cin>>n;
	for(ll i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(ll i=1;i<=n;i++)
	c[a[i]]++;
	for(ll i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,a[100002],b[100002],c[100002];
	cin>>n;
	for(ll i=1;i<=n;i++) cin>>a[i];
	for(ll i=1;i<=n;i++)
	c[a[i]]++;
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=100000;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,s[10000001],c[10000001]={0},x;
	cin>>n;
	for(ll i=0;i<n;i++) {
	cin>>x; c[x]++;}
	s[1]=c[1];
	for(ll i=2;i<=100000;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,s[100010],c[100010]={0},x;
	cin>>n;
	for(int i=0;i<n;i++) {
	cin>>x; c[x]++;}
	s[1]=c[1];
	for(int i=2;i<=100010;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,s[100100],c[100100]={0},x;
	cin>>n;
	for(int i=0;i<n;i++) {
	cin>>x; c[x]++;}
	s[1]=c[1];
	for(int i=2;i<=100100;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n,s[100100],c[100100]={0},x;
	cin>>n;
	for(ll i=0;i<n;i++) {
	cin>>x; c[x]++;}
	s[1]=c[1];
	for(ll i=2;i<=100100;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[100100];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
	int n, a[100002],s[100002],c[100002];
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	c[a[i]]++;
	for(int i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(int i=2;i<=n;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#define ll long long
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i=a;i<b;i++)
#define MAXN 100009
using namespace std;
//Sort in descending order
/*void sort_d(int a[],int n)
{
	sort(a,a+n,greater<int>());
}
//Sorting an array according to another array using pair in STL
void pair_sort(int a[],char b[],int n)
{
	pair<int,char> t[n];
	For(i,0,n)
	{
		t[i].first=a[i];
		t[i].second=b[i];
	}
	sort(t,t+n);
	For(i,0,n)
	b[i]=t[i].second;
}
//Sorting vector.second()
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
	return (a.second<b.second);
}
//Binary search
int binary_search(int a[],int l,int r,int x)
{
	int mid=(l+r)/2;
	while(l<=r)
	{

		if(x==a[mid]) return mid;
		if(x>a[mid]) l=mid+1;
		else r=mid-1;
		mid=(l+r)/2;
	}
	return -1;
}
//String to number
int ston(string str)
{
	return stoi(str);
}
//Number to string
string ntos(int s)
{
	return to_string(s);
}
//Searching for Patterns (KMP Algorithm) */
int main()
{
	ll n,x,s[MAXN],c[MAXN];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		c[x]++;
	}
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<MAXN;i++)
		s[i]=max(s[i-1],s[i-2]+i*c[i]);
	cout<<s[MAXN-2];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
	ll n;
	cin>>n;
	ll a[n+1],s[n+1],c[100001];
	for(ll i=1;i<=n;i++) cin>>a[i];
	for(ll i=1;i<=n;i++)
	c[a[i]]++;
	for(ll i=1;i<=n;i++)
	s[0]=0;
	s[1]=c[1];
	for(ll i=2;i<=100000;i++)
	{
		s[i]=max(s[i-2]+i*c[i],s[i-1]);
	}
	cout<<s[n];
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<=0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(100000);
	cout<<res<<endl;
	return 0;
}