We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

There are n citizens in Kekoland, each person has c_{i} coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people. 

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too. 

Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.


-----Input-----

The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 10^9) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

The second line contains n integers, the i-th of them is c_{i} (1 ≤ c_{i} ≤ 10^9) — initial wealth of the i-th person.


-----Output-----

Print a single line containing the difference between richest and poorest peoples wealth.


-----Examples-----
Input
4 1
1 1 4 2

Output
2

Input
3 1
2 2 2

Output
0



-----Note-----

Lets look at how wealth changes through day in the first sample.  [1, 1, 4, 2]  [2, 1, 3, 2] or [1, 2, 3, 2] 

So the answer is 3 - 1 = 2

In second sample wealth will remain the same for each person.

Difficulty: HARD
Tags: Sorting

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
def main():
	(n, k) = list(map(int, input().split()))
	l = sorted(map(int, input().split()))
	(lo, hi) = (l[0], l[-1])
	while lo < hi - 1:
		mid = (lo + hi) // 2
		t = k
		for x in l:
			if x > mid:
				lo = mid
				break
			t -= mid - x
			if t < 0:
				hi = mid
				break
		else:
			lo = mid
	m1 = lo
	(lo, hi) = (l[0], l[-1])
	l.reverse()
	while lo < hi - 1:
		mid = (lo + hi) // 2
		t = k
		for x in l:
			if x < mid:
				hi = mid
				break
			t -= x - mid
			if t < 0:
				lo = mid
				break
		else:
			hi = mid
	print(hi - m1 if hi > m1 else 1 if sum(l) % n else 0)

def __starting_point():
	main()
__starting_point()


N = 500005
C = [0] * N

def doit():
	global k
	C[1:n + 1] = sorted(C[1:n + 1])
	last = C[1]
	temp = k
	for i in range(2, n + 1):
		if (C[i] - last) * (i - 1) <= k:
			k -= (C[i] - last) * (i - 1)
			last = C[i]
	j = 0
	while j + 1 <= n and C[j + 1] <= last:
		j += 1
	go = k // j
	left = k % j
	for i in range(1, j + 1):
		C[i] = last + go + (i <= left)
	k = temp

def rev():
	for i in range(1, n + 1):
		C[i] = -C[i]
(n, k) = map(int, input().split())
c = list(map(int, input().split()))
for i in range(1, n + 1):
	C[i] = c[i - 1]
doit()
rev()
doit()
rev()
C[1:n + 1] = sorted(C[1:n + 1])
print(C[n] - C[1])


(n, k) = map(int, input().split())
a = sorted(list(map(int, input().split())))

def solve(a, k):
	b = a[::-1]
	S = sum(a)
	n = len(a)
	div = S // n
	re = S % n
	T = 0
	num = re
	for i in range(n - 1, -1, -1):
		thres = div
		if num > 0:
			thres += 1
			num -= 1
		if a[i] > thres:
			T += a[i] - thres
	if k >= T:
		return 1 if re > 0 else 0
	l_arr = [0] * n
	r_arr = [0] * n
	for i in range(1, n):
		l_arr[i] = (a[i] - a[i - 1]) * i
	for i in range(1, n):
		r_arr[i] = (b[i - 1] - b[i]) * i
	remain = k
	(l, u) = (a[0], b[0])
	for i in range(1, n):
		if remain >= l_arr[i]:
			remain -= l_arr[i]
			l = a[i]
		else:
			l += remain // i
			break
	remain = k
	for i in range(1, n):
		if remain >= r_arr[i]:
			remain -= r_arr[i]
			u = b[i]
		else:
			u -= remain // i
			break
	return u - l
print(solve(a, k))


import sys
inp = sys.stdin.read().splitlines()
(n, k) = list(map(int, inp[0].split()))
lst = list(map(int, inp[1].split()))
lst.sort()
total = sum(lst)
lower = int(total / n)
nupper = total % n
if nupper == 0:
	upper = lower
else:
	upper = lower + 1
nlower = n - nupper
i = 0
while i < n and lst[i] < lower:
	i += 1
low1st = i
i = n - 1
while i >= 0 and lst[i] > upper:
	i -= 1
uplast = i
lowerfill = low1st * lower - sum(lst[:low1st])
upperfill = sum(lst[uplast + 1:]) - (n - uplast - 1) * upper
totalsteps = (lowerfill + upperfill) / 2

def filllower():
	kk = k
	cur = lst[0]
	i = 0
	while kk > 0:
		while lst[i] == cur:
			i += 1
		diff = lst[i] - lst[i - 1]
		kk -= i * diff
		if kk == 0:
			cur = lst[i]
			break
		elif kk < 0:
			cur = lst[i] - int(-kk / i) - 1
			if -kk % i == 0:
				cur += 1
			break
		cur = lst[i]
	return cur

def fillupper():
	kk = k
	i = n - 1
	cur = lst[i]
	while kk > 0:
		while lst[i] == cur:
			i -= 1
		diff = lst[i + 1] - lst[i]
		kk -= (n - i - 1) * diff
		if kk == 0:
			cur = lst[i - 1]
			break
		elif kk < 0:
			cur = lst[i] + int(-kk / (n - i - 1))
			if -kk % (n - i - 1) != 0:
				cur += 1
			break
		cur = lst[i]
	return cur
if totalsteps >= k:
	print(fillupper() - filllower())
else:
	print(upper - lower)


import sys
(n, k) = map(int, sys.stdin.readline().split())
nums = list(map(int, sys.stdin.readline().split()))
total = sum(nums)
avg = int(total / n)

def check1(nums, target, K):
	for x in nums:
		if K < 0:
			return False
		if x < target:
			K -= target - x
	return K >= 0

def check2(nums, target, K):
	for x in nums:
		if K < 0:
			return False
		if x > target:
			K -= x - target
	return K >= 0
(l1, r1) = (min(nums), avg + 1)
while l1 + 1 < r1:
	mid = l1 + r1 >> 1
	if check1(nums, mid, k):
		l1 = mid
	else:
		r1 = mid
if check2(nums, avg + (0 if total % n == 0 else 1), k):
	r2 = avg + (0 if total % n == 0 else 1)
else:
	(l2, r2) = (avg + (0 if total % n == 0 else 1), max(nums))
	while l2 + 1 < r2:
		mid = l2 + r2 >> 1
		if check2(nums, mid, k):
			r2 = mid
		else:
			l2 = mid
print(r2 - l1)


from sys import stdin
(n, k) = [int(x) for x in stdin.readline().split()]
oK = k
c = sorted([int(x) for x in stdin.readline().split()])
l = 0
r = n - 1
low = c[0]
high = c[-1]
lowBuffer = 0
highBuffer = 0
while low < high:
	if low == c[l]:
		l += 1
	if high == c[r]:
		r -= 1
	if l * (c[l] - low) - lowBuffer > (n - r - 1) * (high - c[r]) - highBuffer <= k:
		k -= (n - r - 1) * (high - c[r]) - highBuffer
		lowBuffer += (n - r - 1) * (high - c[r]) - highBuffer
		highBuffer = 0
		high = c[r]
	elif (n - r - 1) * (high - c[r]) - highBuffer >= l * (c[l] - low) - lowBuffer <= k:
		k -= l * (c[l] - low) - lowBuffer
		highBuffer += l * (c[l] - low) - lowBuffer
		lowBuffer = 0
		low = c[l]
	else:
		low += (lowBuffer + k) // l
		high -= (highBuffer + k) // (n - r - 1)
		k = 0
		break
if sum(c) % n == 0:
	print(max(0, high - low))
else:
	print(max(1, high - low))


from sys import *
f = lambda : map(int, stdin.readline().split())
(n, k) = f()
t = sorted(f())
s = [0] * (n + 1)
for i in range(n):
	s[i + 1] = s[i] + t[i]
t = [0] + t
d = s[n]
(l, r) = (0, n)
while l < r:
	m = l + r + 1 >> 1
	if t[m] * m - s[m] > k:
		r = m - 1
	else:
		l = m
x = l
(l, r) = (0, n)
while l < r:
	m = l + r >> 1
	if d - s[m] - t[m] * (n - m) > k:
		l = m + 1
	else:
		r = m
y = r
q = (d - s[y - 1] - k + n - y) // (n - y + 1) - (s[x] + k) // x
print(max(q, int(d % n > 0)))


import math
(n, k) = map(int, input().split(' '))
arr = sorted(map(int, input().split(' ')))
totalNeed = 0
poorest = 0
richest = 0
foundP = 1
foundR = 0
if k == 0:
	print(max(arr) - min(arr))
else:
	for i in range(n - 1):
		num = arr[i]
		prevT = totalNeed
		totalNeed += (arr[i + 1] - num) * (i + 1)
		if k <= totalNeed:
			poorest = num + math.floor((k - prevT) / (i + 1))
			foundP = i
			break
	stolen = 0
	for i in range(1, n)[::-1]:
		num = arr[i]
		prevS = stolen
		stolen += (num - arr[i - 1]) * (n - i)
		if k <= stolen:
			richest = num - math.floor((k - prevS) / (n - i))
			foundR = i
			break
	if foundR - 1 <= foundP:
		if foundR - 1 == foundP and sum(arr) % len(arr) == 0:
			print(max(richest - poorest, 0))
		elif foundR - 1 == foundP and sum(arr) % len(arr) > 0:
			print(max(richest - poorest, 1))
		elif sum(arr) % len(arr) == 0:
			print(0)
		else:
			print(1)
	else:
		print(richest - poorest)