Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.

There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get a_{i} votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.

Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?


-----Input-----

The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.

The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.

Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.


-----Output-----

Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.


-----Examples-----
Input
5
5 1 11 2 8

Output
4

Input
4
1 8 8 8

Output
6

Input
2
7 6

Output
0



-----Note-----

In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.

In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.

In the third sample Limak is a winner without bribing any citizen.

Difficulty: EASY
Tags: Implementation

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
n = int(input())
v = list(map(int, input().split()))
c = v[0]
v[0] = -1
v = sorted(v)
v = v[::-1]
b = 0
while c <= v[0]:
	x = v[0]
	for i in range(n):
		if c <= v[i] and v[i] == x:
			c += 1
			v[i] -= 1
			b += 1
print(b)


n = int(input())
(a, *bs) = list(map(int, input().split()))
bs.sort(reverse=True)
r = 0
while a <= bs[0]:
	bs[0] -= 1
	a += 1
	r += 1
	if len(bs) > 1 and bs[0] < bs[1]:
		bs.sort(reverse=True)
print(r)


n = int(input())
a = list(map(int, input().split()))
(l, o) = (a[0], a[0])
a = a[1:]
while l <= max(a):
	a[a.index(max(a))] -= 1
	l += 1
print(l - o)


n = int(input())
a = list(map(int, input().split()))
a0 = a[0]
a = sorted(a[1:])
cnt = 0
while a0 <= a[-1]:
	cnt += 1
	a0 += 1
	a[-1] -= 1
	a.sort()
print(cnt)


n = int(input())
ar = list(map(int, input().split()))
k = ar.pop(0)
bribes = 0
ar.sort()
while k <= ar[n - 2]:
	k += 1
	bribes += 1
	ar[n - 2] -= 1
	ar.sort()
print(bribes)


n = int(input())
arr = list(map(int, input().split()))
mx = arr.pop(0)
ans = mx
arr.sort(reverse=True)
while mx <= arr[0]:
	mx += 1
	arr[0] -= 1
	arr.sort(reverse=True)
print(mx - ans)


def inp(s):
	j = 0
	a = []
	for i in range(len(s)):
		if s[i] == ' ':
			a.append(int(s[j:i]))
			j = i + 1
		if i == len(s) - 1:
			a.append(int(s[j:]))
	return a
n = int(input())
s = input()
a = inp(s)
x = a[0]
a.remove(a[0])
m = max(a)
i = 0
while x <= m:
	n = a.index(m)
	a[n] = a[n] - 1
	x = x + 1
	i = i + 1
	m = max(a)
print(i)


n = int(input())
cand = [int(i) for i in input().split()]
opp = cand[1:]
opp.sort()
lemak = cand[0]
result = 0
while lemak <= opp[-1]:
	lemak += 1
	opp[-1] -= 1
	result += 1
	opp.sort()
print(result)


from sys import stdin, stdout
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
l = a[0]
ans = 0
ops = a[1:]
ops = sorted(ops, reverse=True)
max_op = ops[0]
while l <= max_op:
	for (i, el) in enumerate(ops):
		if el == max_op:
			ops[i] -= 1
			ans += 1
			l += 1
			if l > max_op:
				break
	max_op = max(ops)
stdout.write(str(ans))


import unittest
import sys

class Bear:

	def __init__(self, test_inputs=None):
		it = iter(test_inputs.split('\n')) if test_inputs else None

		def uinput():
			return next(it) if it else sys.stdin.readline().rstrip()
		self.n = int(uinput())
		self.nums = list(map(int, uinput().split()))

	def calculate(self):
		lamak = self.nums[0]
		srt = sorted(self.nums[1:])
		result = 0
		while lamak <= srt[-1]:
			srt[-1] -= 1
			lamak += 1
			result += 1
			srt = sorted(srt)
		return str(result)

class unitTests(unittest.TestCase):

	def test_single_test(self):
		test = '5\n5 1 11 2 8'
		d = Bear(test)
		self.assertEqual(d.n, 5)
		self.assertEqual(d.nums, [5, 1, 11, 2, 8])
		self.assertEqual(Bear(test).calculate(), '4')
		test = '4\n1 8 8 8'
		self.assertEqual(Bear(test).calculate(), '6')
		test = '2\n7 6'
		self.assertEqual(Bear(test).calculate(), '0')
		test = '4\n0 1 1 1'
		self.assertEqual(Bear(test).calculate(), '2')
		self.time_limit_test(100)

	def time_limit_test(self, nmax):
		import random
		import timeit
		test = str(nmax) + '\n'
		test += '0 '
		nums = [1000 for i in range(nmax - 1)]
		test += ' '.join(map(str, nums)) + '\n'
		start = timeit.default_timer()
		d = Bear(test)
		calc = timeit.default_timer()
		d.calculate()
		stop = timeit.default_timer()
		print('\nTimelimit Test: ' + '{0:.3f}s (init {1:.3f}s calc {2:.3f}s)'.format(stop - start, calc - start, stop - calc))
sys.setrecursionlimit(100000)
if sys.argv[-1] == '-ut':
	unittest.main(argv=[' '])
sys.stdout.write(Bear().calculate())


n = int(input())
mas = list(map(int, input().split()))
li = mas[0]
li1 = li
mas = mas[1:]
while li <= max(mas):
	mas = sorted(mas)
	mas[-1] -= 1
	li += 1
print(li - li1)


t = int(input())
voters = list(map(int, input().split()))
j = max(voters)
if voters.index(j) == 0 and voters.count(j) == 1:
	print(0)
else:
	k = voters.count(j)
	first = voters[0]
	voters.remove(first)
	count = 0
	while max(voters) >= first:
		j = max(voters)
		p = voters.index(j)
		first += 1
		voters[p] -= 1
		count += 1
	print(count)


input()
a = list(map(int, input().split()))
from queue import PriorityQueue

class MaxHeapElement(object):

	def __init__(self, x):
		self.x = x

	def __lt__(self, other):
		return self.x > other.x

	def __str__(self):
		return str(self.x)
q = PriorityQueue()
for i in range(1, len(a)):
	q.put(MaxHeapElement(a[i]))
total = 0
while not q.empty():
	el = int(str(q.get()))
	if a[0] > el:
		break
	elif a[0] == el:
		total += 1
		a[0] += 1
	else:
		total += 1
		a[0] += 1
		q.put(MaxHeapElement(el - 1))
print(total)


cCount = int(input())
cList = [int(x) for x in input().split()]
count = 0
while 1:
	m = 0
	for i in range(1, cCount):
		if cList[i] >= cList[m]:
			m = i
	if m == 0:
		break
	cList[m] -= 1
	cList[0] += 1
	count += 1
print(count)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
n = int(input())
votes = list(map(int, input().split()))
(focus, cnt) = (votes[0], 0)
while True:
	if focus == max(votes) and votes.count(focus) == 1:
		break
	votes.sort()
	index = votes.index(focus)
	focus += 1
	votes[index] += 1
	votes[-1] -= 1
	cnt += 1
print(cnt)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	t = a[0]
	z = max(a)
	while t <= max(a[1:]):
		t += 1
		a[a[1:].index(max(a[1:])) + 1] -= 1
	print(t - a[0])
	return
try:
	solve()
except:
	pass


n = int(input())
a = list(map(int, input().split()))
p = a[0]
a = a[1:]
w = 0
maxv = max(a)
while p <= maxv:
	p += 1
	a.remove(maxv)
	a.append(maxv - 1)
	w += 1
	maxv = max(a)
print(w)


trash = input()
str = input()
str = str.split()
str = [int(i) for i in str]
limak = str[0]
str.pop(0)
str.sort(reverse=True)
count = 0
i = 0
while str[0] >= limak:
	str[i] -= 1
	limak += 1
	str.sort(reverse=True)
	count += 1
print(count)


n = input()
arr = list(map(int, input().split()))
lum = arr[0]
del arr[0]
count = 0
arr = sorted(arr)[::-1]
while lum <= arr[0]:
	lum += 1
	arr[0] -= 1
	count += 1
	arr = sorted(arr)[::-1]
print(count)


n = int(input())
all_v = list(map(int, input().split()))
v = all_v[1:]
v_for_L = all_v[0]
v = sorted(v)[::-1]
count = 0
while v_for_L <= v[0]:
	v[0] -= 1
	v = sorted(v)[::-1]
	count += 1
	v_for_L += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
(ct, m, best) = (0, a.pop(0), max(a))
while m <= best:
	m += 1
	ct += 1
	a[a.index(best)] -= 1
	best = max(a)
print(ct)


n = int(input())
s = [int(i) for i in input().split(' ')]
a = s.pop(0)
m = max(s)
k = 0
while a <= m:
	a += 1
	s[s.index(m)] -= 1
	k += 1
	m = max(s)
print(k)


import sys
import io
from collections import Counter

def solve(n, vs):
	assert 2 <= n <= 1000
	for v in vs:
		assert 1 <= v <= 1000
	(lv, rvs) = (vs[0], vs[1:])
	cnt = 0
	votes = Counter(rvs)
	max_vote = max(rvs)
	while lv <= max_vote:
		votes[max_vote] -= 1
		votes[max_vote - 1] += 1
		if votes[max_vote] == 0:
			max_vote -= 1
		lv += 1
		cnt += 1
	return cnt

def getinput():

	def getint():
		return int(input())

	def getints_line():
		return list(map(int, input().split(' ')))

	def getints(n):
		return [getint() for _ in range(n)]

	def getints_lines(n):
		return [getints_line() for _ in range(n)]
	return [getint(), getints_line()]

def iosolve():
	return str(solve(*getinput()))

def main():
	if sys.stdin.isatty():
		test()
	stdin_lines = getstdin_lines()
	sys.stdin = io.StringIO('\n'.join(stdin_lines))
	if stdin_lines:
		print(iosolve())
	else:
		test()

def test():
	IO_TEST_CASES = [('5\n5 1 11 2 8\n            ', '4\n            '), ('4\n1 8 8 8\n            ', '6\n            '), ('2\n7 6\n            ', '0\n            ')]
	TEST_CASES = []
	import unittest
	import sys
	import io

	class Assert(unittest.TestCase):

		def equal(self, a, b):
			self.assertEqual(a, b)

		def float_equal(self, actual, expect, tolerance):
			self.assertTrue(expect - tolerance < actual < expect + tolerance)
	art = Assert()
	for (inputs, expect) in TEST_CASES:
		art.equal(solve(*inputs), expect)
	for (stdin, expect) in IO_TEST_CASES:
		sys.stdin = io.StringIO(stdin.strip())
		art.equal(iosolve(), expect.strip())

def getstdin_lines():
	stdin = []
	while 1:
		try:
			stdin.append(input())
		except EOFError:
			break
	return stdin

def __starting_point():
	main()
__starting_point()


import sys
from collections import Counter
lines = sys.stdin.read().split('\n')
n = int(lines[0])
nums = list(map(int, lines[1].split(' ')))
s = nums[0]
os = nums[0]
nums = nums[1:]
nums.sort()

def get_max_n(nums):
	pre = nums[-1]
	for i in range(len(nums) - 1, -1, -1):
		if nums[i] != pre:
			break
	if nums[i] != pre:
		return (len(nums) - (i + 1), pre, nums[i])
	else:
		return (len(nums), pre, 0)
while True:
	(a, b, c) = get_max_n(nums)
	if a * (b - c) + s > c:
		if s > b:
			break
		for i in range(1, b - c + 1):
			if i * a + s > b - i:
				if i * a + s - 1 <= b - i + 1:
					s = i * a + s
					break
				else:
					subdown = min(a - 2, i * a + s - 1 - (b - i + 1) - 1)
					subdown = 0 if subdown < 0 else subdown
					s = i * a + s - 1 - subdown
					break
		break
	else:
		s += a * (b - c)
		nums[-a:] = [c] * a
print(s - os)


n = int(input())
l = list(map(int, input().split()))
c = 0
while l[0] <= max(l):
	try:
		x = l[1:].index(max(l))
		l[x + 1] = l[x + 1] - 1
		c = c + 1
		l[0] = l[0] + 1
	except:
		break
print(c)


import heapq
n = int(input())
votes = [-int(e) for e in input().split()]
limak = -votes[0]
votes = votes[1:]
heapq.heapify(votes)
ans = 0
while limak <= -votes[0]:
	cur = heapq.heappop(votes)
	cur += 1
	limak += 1
	ans += 1
	heapq.heappush(votes, cur)
print(ans)


count = input()
full_candidates = [int(candidate) for candidate in input().split(' ')]
limak = full_candidates[0]
bribe = 0
candidates = full_candidates[1:]
while max(candidates) >= limak + bribe:
	candidates[candidates.index(max(candidates))] -= 1
	bribe += 1
print(bribe)


n = int(input())
a = list(map(int, input().split(' ')))
lim = a[0]
a[1:] = sorted(a[1:])
t = 0
while a[0] <= a[-1]:
	a[0] = a[0] + 1
	a[-1] = a[-1] - 1
	t += 1
	a[1:] = sorted(a[1:])
print(t)


def main(n, a):

	def s(n, a, i):
		a[0] += i
		amt = 0
		for j in range(1, n):
			while a[j] >= a[0] and amt < i:
				a[j] -= 1
				amt += 1
		return a[1:]
	a[1:] = reversed(sorted(a[1:]))
	for i in range(1000000):
		x = list(filter(lambda x: x >= a[0] + i, s(n, list(a), i)))
		if len(x) == 0:
			return i
	return -1
print(main(int(input()), list(map(int, input().split(' ')))))


n = int(input())
a = list(map(int, input().split()))
mx = 0
while a[0] <= max(a[1:n]):
	a[a[1:].index(max(a[1:n])) + 1] -= 1
	a[0] += 1
	mx += 1
print(mx)


n = int(input(''))
a = input('')
a = [int(x) for x in a.split()]

def bribe(a):
	ans = 0
	if a[0] == max(a) and a.count(max(a)) != 1:
		ans += 1
	while a[0] + ans <= max(a) and a[0] != max(a):
		ans += 1
		a[a.index(max(a))] -= 1
	return ans
print(bribe(a))


import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
a0 = a[0]
b = [i + 1 - a0 for i in a[1:] if i >= a0]

def getNb(l):
	if len(l) == 0:
		return 0
	m = max(l)
	if m <= 0:
		return m
	idx = l.index(m)
	l[idx] -= 1
	l2 = [i - 1 for i in l if i > 1]
	return 1 + getNb(l2)
print(getNb(b))


n = int(input())
a = list(map(int, input().split()))
q = a.pop(0)
a.sort()
sum = 0
while q <= a[-1]:
	a[-1] -= 1
	a.sort()
	q += 1
	sum += 1
print(sum)


no_candidates = int(input())
voters_inp_list = input().split()
voters_list = [int(num) for num in voters_inp_list]
limak_votes = voters_list[0]
voters_list.sort(reverse=True)
if limak_votes == voters_list[0]:
	if limak_votes > voters_list[1]:
		print(0)
		exit()
	else:
		print(1)
		exit()
else:
	count = 0
	while limak_votes <= voters_list[0]:
		limak_votes += 1
		voters_list[0] -= 1
		voters_list.sort(reverse=True)
		count += 1
	print(count)


input()
(A, k) = (list(map(int, input().split())), 0)
while True:
	i = A.index(max(A))
	if i == 0:
		print(k + (A.count(A[0]) > 1))
		break
	(A[0], A[i], k) = (A[0] + 1, A[i] - 1, k + 1)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
while True:
	l = max(arr)
	max_index = arr.index(l)
	if max_index == 0:
		zz = False
		for i in range(1, n):
			if arr[i] == arr[0]:
				print(ans + 1)
				zz = True
				break
		if zz == False:
			print(ans)
		break
	arr[0] += 1
	arr[max_index] -= 1
	ans += 1


n = int(input())
a = list(map(int, input().split()))
l = a[0]
a = a[1:]
res = 0
x = max(a)
y = a.index(x)
while l <= x:
	l += 1
	a[y] -= 1
	res += 1
	x = max(a)
	y = a.index(x)
print(res)


def check(m, a):
	for i in range(1, len(a)):
		if a[i] >= m:
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
ms = a[0]
ans = 0
while check(a[0], a):
	pos = a[1:].index(max(a)) + 1
	a[pos] -= 1
	a[0] += 1
	ans += 1
print(ans)


n = int(input())
c = []
values = list(map(int, input().split()))
t = values[0]
for i in range(n - 1):
	c.append(values[i + 1])
c.sort(reverse=True)
ans = 0
while True:
	if t > c[0]:
		break
	t += 1
	c[0] -= 1
	ans += 1
	c.sort(reverse=True)
print(ans)


import math
import string

def find_max_index(_list):
	i = 0
	l = len(_list)
	maximum = 0
	max_index = 0
	while i < l:
		if maximum < _list[i]:
			maximum = _list[i]
			max_index = i
		i = i + 1
	return max_index
n = int(input())
input_str = input()
input_list = input_str.split(' ')
vote = []
for s in input_list:
	vote.append(int(s))
v = vote[0]
r = []
for i in vote[1:]:
	if i >= v:
		r.append(i)
stolen = 0
if len(r) > 0:
	while v <= max(r):
		i = find_max_index(r)
		r[i] = r[i] - 1
		v = v + 1
		stolen = stolen + 1
print(stolen)


n = int(input())
x = list(map(int, input().split()))
c = x[0]
x[0] = -1
x.sort(reverse=True)
x.insert(0, c)
while x[0] <= x[1]:
	for i in range(1, n):
		k = x[1]
		if i == 1 and x[1] >= x[2]:
			x[0] += 1
			x[1] -= 1
		elif x[i] < k:
			break
		elif x[0] < x[i]:
			x[0] += 1
			x[i] -= 1
for i in range(1, n):
	if x[0] <= x[i]:
		c -= 1
		break
print(x[0] - c)


def solve():
	n = int(input())
	(a, *bs) = map(int, input().split())
	bs.sort(reverse=True)
	cnt = 0
	while a <= bs[0]:
		bs[0] -= 1
		a += 1
		cnt += 1
		if len(bs) > 1 and bs[0] < bs[1]:
			bs.sort(reverse=True)
	print(cnt)
solve()


n = int(input())
l = list(map(int, input().split()))
x = l[0]
s = l[0]
l = l[1:]
while s <= max(l):
	l = sorted(l)
	l[-1] -= 1
	s += 1
print(s - x)


u = [0] * 10000
a = int(input())
b = input().split()
start = int(b[0])
me = int(b[0])
mx = 0
for i in b[1:]:
	i = int(i)
	u[i] += 1
	mx = max(mx, i)
i = mx
while me < i:
	z = u[i]
	while u[i] and me <= i:
		me += 1
		u[i] -= 1
	u[i - 1] += z
	i -= 1
if me == i:
	me += 1
print(me - start)


def main():
	input()
	l = list(map(int, input().split()))
	x = x0 = l.pop(0)
	y = max(l)
	c = [0] * (y + 1)
	for t in l:
		c[t] += 1
	c[0] = 1
	while True:
		while not c[y]:
			y -= 1
		if y < x:
			break
		t = c[y]
		if x + t > y:
			x = y + 1
			break
		y -= 1
		c[y] += t
		x += t
	print(x - x0)
main()


N = int(input())
votes = [int(x) for (i, x) in enumerate(input().split())]
my = votes[0]
old = my
votes = sorted(votes[1:], key=lambda x: -x)
while my <= votes[0]:
	votes[0] -= 1
	my += 1
	i = 0
	while i + 1 < len(votes) and votes[i] < votes[i + 1]:
		(votes[i], votes[i + 1]) = (votes[i + 1], votes[i])
		i += 1
print(my - old)


n = int(input())
voices = input().split()
for i in range(n):
	voices[i] = int(voices[i])
k = 0
M = sorted(voices[1:])
if voices[0] <= M[n - 2]:
	while voices[0] <= M[n - 2]:
		voices[0] += 1
		M[n - 2] -= 1
		k += 1
		M = sorted(M)
print(k)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect, insort
from time import perf_counter
from fractions import Fraction
import copy
from copy import deepcopy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(*ele, end='\n')
n = L()[0]
A = L()
x = A[0]
y = A[1:]
cost = 0
while x <= max(y):
	y.sort()
	y[-1] -= 1
	x += 1
	cost += 1
print(cost)
endtime = time.time()


n = int(input())
a = list(map(int, input().split()))
cnt = 0
while not max(a) == a[0]:
	a[a.index(max(a))] -= 1
	a[0] += 1
	cnt += 1
if a.count(max(a)) != 1:
	cnt += 1
print(cnt)


__author__ = 'suvasish'
candidates = int(input())
votes = input().split(' ')
limak_vote_count = 0
votes = list(map(int, votes))
while votes[0] <= max(votes[1:]):
	indx = votes[1:].index(max(votes[1:]))
	votes[0] += 1
	limak_vote_count += 1
	votes[indx + 1] -= 1
print(limak_vote_count)


input()
c = [int(x) for x in input().split()]
l = c.pop(0)
c.sort(reverse=True)
ans = 0
while l <= c[0]:
	ans += 1
	l += 1
	c[0] -= 1
	c.sort(reverse=True)
print(ans)


n = int(input())
a = list(map(int, input().split()))
main = a[0]
rest = sorted(a[1:], reverse=True)
counter = 0
while True:
	rest = sorted(rest, reverse=True)
	if main > rest[0]:
		print(counter)
		exit()
	else:
		main += 1
		rest[0] -= 1
		counter += 1


import sys

def chkrange(list, a, b):
	while a > 0:
		if list[a] == list[a - 1]:
			a -= 1
		else:
			break
	return (a, b)
n = int(input())
list2 = input().split()
list = []
for item in list2:
	list.append(int(item))
vote = list[0]
del list[0]
list.sort()
a = len(list) - 1
b = a
(a, b) = chkrange(list, a, b)
ans = 0
i = a
while vote <= list[len(list) - 1]:
	list[i] -= 1
	i += 1
	ans += 1
	vote += 1
	if i == len(list):
		(a, b) = chkrange(list, a, b)
		i = a
print(ans)


import sys
n = int(input())
arr = list(map(int, input().split(' ')))
val = arr[0]
cnt = 0
se = max(arr)
if val == se and arr.count(val) == 1:
	print(0)
	sys.exit(0)
while val <= se:
	for x in range(len(arr)):
		if arr[x] == se:
			arr[x] -= 1
			val += 1
			cnt += 1
		if val > se:
			print(cnt)
			sys.exit(0)
	se = max(arr)
print(cnt)


n = int(input())
arr = list(map(int, input().split()))
cond = []
max1 = arr[1]
lam = arr[0]
for i in range(1, n):
	if arr[i] >= lam:
		if arr[i] > max1:
			max1 = arr[i]
			cond.reverse()
			cond.append(max1)
			cond.reverse()
		else:
			cond.append(arr[i])
ans = 0
while cond:
	cond[0] -= 1
	ans += 1
	lam += 1
	cond1 = []
	max1 = cond[0]
	for i in range(len(cond)):
		if cond[i] >= lam:
			if cond[i] > max1:
				max1 = cond[i]
				cond1.reverse()
				cond1.append(max1)
				cond1.reverse()
			else:
				cond1.append(cond[i])
	cond = cond1
print(ans)


import heapq
n = int(input())
As = list(map(int, input().split()))
Bs = [-a for a in As]
a = Bs[0]
pq = Bs[1:]
heapq.heapify(pq)
b = 0
while pq[0] <= a - b:
	b += 1
	heapq.heapreplace(pq, pq[0] + 1)
print(b)


n = int(input()) - 1
l = list(map(int, input().split()))
lim = l[0]
l = sorted(l[1:], reverse=True)
it = 0
res = 0
while lim <= l[it % n]:
	lim += 1
	l[it % n] -= 1
	res += 1
	if l[it % n] < l[(it + 1) % n]:
		it += 1
	elif it % n > 0 and l[it % n] <= l[(it - 1) % n]:
		it = 0
print(res)


import math
n = int(input())
v = list(map(int, input().split()))
v[1:] = sorted(v[1:])
ans = 0
while v[0] <= v[-1]:
	v[-1] -= 1
	v[0] += 1
	ans += 1
	v[1:] = sorted(v[1:])
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
k = 0
while True:
	j = max(range(1, n), key=lambda i: a[i])
	if a[0] <= a[j]:
		a[0] += 1
		a[j] -= 1
		k += 1
	else:
		break
print(k)


n = int(input())
votes = [int(vote) for vote in input().split()]
limak = votes[0]
votes = votes[1:]
votes.sort()
votes = votes[::-1]
candies = 0
index = 0
while limak <= votes[index]:
	candies += 1
	votes[index] -= 1
	limak += 1
	if index + 1 < n - 1:
		if votes[index + 1] > votes[index]:
			index += 1
		if votes[0] > votes[index]:
			index = 0
	elif votes[0] > votes[index]:
		index = 0
print(candies)


def main():
	n = int(input())
	a = [int(c) for c in input().split()]
	(my, other) = (a[0], a[1:])
	res = 0
	while True:
		(mx, mx_idx) = (-1, -1)
		for (i, e) in enumerate(other):
			if e > mx:
				mx = e
				mx_idx = i
		if my <= mx:
			my += 1
			res += 1
			other[mx_idx] -= 1
		else:
			print(res)
			return
main()


n = int(input())
line = list(map(int, input().split()))
kamil = line[0]
line = line[1:]
line.sort()
count = 0
while kamil <= line[-1]:
	line[-1] -= 1
	kamil += 1
	count += 1
	line.sort()
print(count)


n = int(input())
arr = list(map(int, input().split()))
m = max(arr)
k = arr[0]
while arr[0] != max(arr):
	h = arr.index(max(arr))
	arr[0] += 1
	arr[h] -= 1
if arr.count(max(arr)) > 1:
	print(arr[0] - k + 1)
else:
	print(arr[0] - k)


n = int(input())
s = [int(i) for i in input().split()]
cnt = 0
r = s[0]
a = sorted(s[1:])
a = a[::-1]
b = s[0]
j = 0
while s[0] <= max(a):
	if j == len(a):
		j = 0
	if s[0] > max(a):
		break
	while a[j] == max(a) and a[j] != 0:
		if s[0] > max(a):
			break
		cnt += 1
		s[0] += 1
		a[j] -= 1
	j += 1
print(s[0] - r)


n = int(input())
l = list(map(int, input().split()))
b = l.pop(0)
cont = 0
while b <= max(l):
	b += 1
	cont += 1
	l[l.index(max(l))] -= 1
print(cont)


a = int(input())
strinp = str(input()).split()
list = []
z = int(strinp[0])
q = 0
for h in range(a):
	list.append(int(strinp[h]))
z = list[0]
list.remove(list[0])
while z < max(list):
	z = z + 1
	g = max(list)
	list[list.index(g)] = list[list.index(g)] - 1
	q = q + 1
c = max(list)
if c == z:
	print(q + 1)
else:
	print(q)


n = int(input())
a = [int(i) for i in input().split()]
k = 0
while a[0] <= max(a[1:]):
	a[1 + a[1:].index(max(a))] -= 1
	a[0] += 1
	k += 1
print(k)


n = int(input())
a = list(map(int, input().split()))
s = a[0]
cnt = 0
while True:
	flag = 1
	idx = 0
	for i in range(1, n):
		if a[idx] <= a[i]:
			idx = i
			flag = 0
	if flag:
		break
	a[0] = a[0] + 1
	a[idx] -= 1
	cnt = cnt + 1
print(cnt)


a = int(input())
b = input().split()
for i in range(a):
	b[i] = int(b[i])
bribes = 0
while b[0] != max(b):
	for i in range(1, len(b)):
		if b[i] == max(b):
			b[i] -= 1
			b[0] += 1
			bribes += 1
			break
true = False
for i in range(1, len(b)):
	if b[i] == max(b):
		true = True
if true:
	bribes += 1
print(bribes)


n = int(input())
arr = list(map(int, input().split()))
k = 0
while arr[0] <= max(arr[1:]):
	arr[1 + arr[1:].index(max(arr))] -= 1
	arr[0] += 1
	k += 1
print(k)


n = int(input())
l = list(map(int, input().split()))
b = l[0]
l1 = l[1:]
l1.sort()
while b <= l1[-1]:
	b += 1
	l1[-1] -= 1
	l1.sort()
print(b - l[0])


import heapq
N = int(input())
s = input().split(' ')
a = [-int(x) for x in s[1:]]
me = int(s[0])
heapq.heapify(a)
ans = 0
while me <= -a[0]:
	x = -heapq.heappop(a)
	x -= 1
	me += 1
	ans += 1
	heapq.heappush(a, -x)
print(ans)


import sys
n = int(input())
a = [int(i) for i in input().split()]
start = a[0]
while max(a[1:]) >= a[0]:
	t = max(a[1:])
	for i in range(1, n):
		if a[i] == t:
			(a[i], a[0]) = (a[i] - 1, a[0] + 1)
			break
print(a[0] - start)


cCount = int(input())
cList = [int(x) for x in input().split()]
oldl = cList.pop(0)
l = oldl
while max(cList) >= l:
	n = cList.count(max(cList))
	d = max(cList) - l
	if d < n:
		l += d + 1
		for x in range(d + 1):
			cList[cList.index(max(cList))] -= 1
	else:
		d -= n
		l += n
		for x in range(n):
			cList[cList.index(max(cList))] -= 1
print(l - oldl)


def max_range(a, l, r):
	p = l
	for i in range(l + 1, r + 1):
		if a[i] > a[p]:
			p = i
	return p
n = eval(input())
a = [0] * n
a = list(map(int, input().split()))
k = 0
while max(a) != a[0]:
	k += 1
	x = max_range(a, 1, n - 1)
	a[0] += 1
	a[x] -= 1
for i in range(1, n):
	if a[i] == a[0]:
		k += 1
		break
print(k)


candidates = int(input())
votes = [int(x) for x in input().split(' ')]
k = votes[0]
change = [v for v in votes if v >= k]
num = 0
if change.count(max(change)) == 1 and k == max(change):
	modifier = 1
else:
	modifier = 0
while k <= max(change):
	k += 1
	change[change.index(max(change))] -= 1
	num += 1
print(num - modifier)


n = int(input()) - 1
v = list(map(int, input().split()))
val = v.pop(0)
ans = 0
v.sort()
while val <= v[n - 1]:
	ans += 1
	val += 1
	v[n - 1] -= 1
	v.sort()
print(ans)


import heapq
input()
votes = [-int(i) for i in input().split()]
n = init = -votes[0]
votes = votes[1:]
heapq.heapify(votes)
while n <= -votes[0]:
	f = heapq.heappop(votes)
	heapq.heappush(votes, f + 1)
	n += 1
print(n - init)


n = int(input())
s = list(map(int, input().split()))
maxes = s[1:]
number = s[0]
dif = abs(s[0] - max(s))
ans = 0
while number <= max(maxes):
	maxes.sort(reverse=True)
	maxes[0] -= 1
	ans += 1
	number += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
ctr = 0
while True:
	mx = a[1]
	imx = 1
	for i in range(1, n):
		if a[i] > mx:
			mx = a[i]
			imx = i
	if mx >= a[0]:
		a[0] += 1
		a[imx] -= 1
		ctr += 1
	else:
		break
print(ctr)


n = int(input())
arr = list(map(int, input().split()))
cnt = 0
arr[1:] = sorted(arr[1:])
while arr[0] <= arr[-1]:
	arr[-1] -= 1
	arr[0] += 1
	cnt += 1
	arr[1:] = sorted(arr[1:])
print(cnt)


n = int(input())
liste = [int(x) for x in input().split()]
liste = liste[::-1]
compteur = 0
while True:
	roger = liste.index(max(liste))
	if roger == n - 1:
		break
	else:
		liste[n - 1] += 1
		liste[roger] -= 1
		compteur += 1
print(compteur)


(_, (limak, *votes)) = (input(), map(int, input().split()))
start = limak
votes = sorted((x for x in votes if x >= limak))
if not votes:
	print(0)
	exit()
while votes[-1] >= limak:
	(limak, votes[-1]) = (limak + 1, votes[-1] - 1)
	votes = sorted(votes)
print(limak - start)


n = int(input())
votes = list(map(int, input().split()))
temp = votes[0]
while True:
	votes[1:] = sorted(votes[1:])
	if temp > votes[-1]:
		break
	else:
		votes[-1] -= 1
		temp += 1
print(temp - votes[0])


a = int(input())
(x, *y) = map(int, input().split())
s = 0
y = list(y)
while x + s <= max(y):
	s += 1
	y += [y.pop(y.index(max(y))) - 1]
print(s)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List, Mapping
sys.setrecursionlimit(999999)
n = int(input())
arr = list(map(int, input().split()))
a = arr.pop(0)
arr.sort()
ans = 0
while a <= arr[-1]:
	t = arr.pop()
	bisect.insort(arr, t - 1)
	a += 1
	ans += 1
print(ans)


import sys
n = int(input())
an = list(map(int, sys.stdin.readline().split()))
ans = 0
while True:
	max_i = 0
	for i in range(1, n):
		if an[i] >= an[max_i]:
			max_i = i
	if max_i is 0:
		break
	else:
		an[0] += 1
		an[max_i] -= 1
		ans += 1
print(ans)


import sys, math
n = int(input())
arr = list(map(int, input().split(' ')))
num = arr[0]
while max(arr) > arr[0]:
	m = max(arr[1:])
	arr[arr[1:].index(m) + 1] -= 1
	arr[0] += 1
if arr[0] in arr[1:]:
	m = max(arr[1:])
	arr[arr[1:].index(m) + 1] -= 1
	arr[0] += 1
print(arr[0] - num)


n = int(input())
a = list(map(int, input().split()))
b = a[1:]
m = max(b)
ans = 0
mi = b.index(max(b))
while a[0] <= m:
	b[mi] -= 1
	ans += 1
	a[0] += 1
	m = max(b)
	mi = b.index(max(b))
print(ans)


n = int(input())
x = list(map(int, input().split()))
x_init = x[0]
while x[0] != max(x):
	x[0] += 1
	if x[0] == max(x):
		x[x[1:].index(max(x)) + 1] -= 1
		break
	x[x.index(max(x))] -= 1
if x.count(x[0]) > 1:
	x[0] += 1
print(x[0] - x_init)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
ans = 0
while l[0] <= max(l[1:]):
	l[l[1:].index(max(l[1:])) + 1] -= 1
	l[0] += 1
	ans += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
bribe = 0
flag = 'True'
while flag:
	max = a[0]
	k = 0
	for i in range(1, n):
		if a[i] >= max:
			max = a[i]
			k = i
	if k != 0 and max >= a[0]:
		a[0] += 1
		a[k] -= 1
		bribe += 1
	else:
		flag = 0
print(bribe)


n = int(input())
a = list(map(int, input().split(' ')))
l = a[0]
c = a[1:]
result = 0
while True:
	c.sort(reverse=True)
	if l > c[0]:
		break
	else:
		l += 1
		c[0] -= 1
		result += 1
print(result)


import sys
N = input()
votes = list(map(int, input().split()))

def num_bribes():
	bribes = 0
	while True:
		(most_votes, winner) = sorted(((votes, idx) for (idx, votes) in enumerate(votes)))[-1]
		my_votes = votes[0]
		num_most_voted = sum((1 for vote in votes if vote == most_votes))
		if my_votes == most_votes and num_most_voted == 1:
			return bribes
		else:
			diff = 1
			bribes += diff
			votes[0] += diff
			votes[winner] -= diff
print(num_bribes())


n = int(input())
cands = input()
cands = cands.split(' ')
cands = list(map(int, cands))
counter = 0
while cands[0] <= max(cands[1:]):
	idxMax = cands.index(max(cands[1:]))
	if cands[0] == max(cands[1:]):
		cands[0] += 1
		cands[idxMax] -= 1
		counter += 1
		break
	else:
		cands[idxMax] -= 1
		cands[0] += 1
		counter += 1
print(counter)


from sys import stdin, stdout, stderr, setrecursionlimit
setrecursionlimit(100000)

def debug(*e):
	if not __debug__:
		print(*e, file=stderr)

def dd(*vals):
	import inspect, re
	frame = inspect.getframeinfo(inspect.stack()[1][0])
	vs = re.search('dd\\((.+)\\)', frame.code_context[0]).group(1).split(',')
	if vs:
		debug(','.join(('{0} = {1}'.format(vs[i], v) for (i, v) in enumerate(vals))))

def trace(f):

	def traced(*args, **kw):
		debug('calling {} with args {}, {}'.format(f.__name__, args, kw))
		return f(*args, **kw)
	return traced

def read():
	return stdin.readline().rstrip()

def readarr(sep=None, maxsplit=-1):
	return read().split(sep, maxsplit)

def readint():
	return int(read())

def readintarr(sep=None, maxsplit=-1):
	return [int(a) for a in readarr(sep, maxsplit)]

def write(*args, **kwargs):
	sep = kwargs.get('sep', ' ')
	end = kwargs.get('end', '\n')
	stdout.write(sep.join((str(a) for a in args)) + end)

def writearr(arr, sep=' ', end='\n'):
	stdout.write(sep.join((str(a) for a in arr)) + end)
import math
from heapq import *
n = readint()
xs = readintarr()
b = xs[0]
r = 0
q = [-x for x in xs[1:]]
heapify(q)
while q:
	x = -heappop(q)
	if x < b:
		break
	r += 1
	b += 1
	heappush(q, -x + 1)
write(r)


n = int(input()) - 1
line = list(map(int, input().split()))
note = tar = line[0]
line = line[1:]
line.sort()
while tar <= line[-1]:
	tar += 1
	line[-1] -= 1
	line.sort()
print(tar - note)


n = int(input())
l = list(input().split(' '))
l = [int(x) for x in l]
gano = False
sobornos = 0

def buscar_mayor(l):
	pos_mayor = 1000
	valor_mayor = -1
	for i in reversed(range(len(l))):
		if l[i] > valor_mayor:
			valor_mayor = l[i]
			pos_mayor = i
	return pos_mayor
while gano == False:
	if buscar_mayor(l) == 0:
		gano = True
	else:
		m = buscar_mayor(l)
		l[0] += 1
		l[m] -= 1
		sobornos += 1
print(sobornos)


n = int(input())
l = list(map(int, input().split()))
k = 0
t = l[0]
l[0] = 0
l = sorted(l)
while t <= l[n - 1]:
	t = t + 1
	l[n - 1] = l[n - 1] - 1
	l = sorted(l)
	k = k + 1
print(k)


s = input()
(l, *v) = (int(x) for x in input().split())
v.sort(reverse=True)
count = 0
while l <= v[0]:
	if len(v) == 1 or v[0] > v[1]:
		count += 1
		l += 1
		v[0] -= 1
	else:
		count += 1
		l += 1
		v[0] -= 1
		v.sort(reverse=True)
print(count)


n = int(input())
arr = [int(i) for i in input().split()]
limak = arr.pop(0)
ans = 0
arr.sort()
while arr[n - 2] >= limak:
	arr[n - 2] -= 1
	limak += 1
	ans += 1
	arr.sort()
print(ans)


n = int(input())
a = list(map(int, input().split()))
x = a[0]
k = 0
while True:
	maxi = 1
	maxe = a[1]
	for i in range(1, n):
		if a[i] > maxe:
			maxi = i
			maxe = a[i]
	if maxe >= x:
		a[maxi] -= 1
		x += 1
		k += 1
	else:
		break
print(k)


import queue
n = int(input())
values = input().split()
t = int(values[0])
q = queue.PriorityQueue()
for i in range(n - 1):
	q.put(-int(values[i + 1]))
ans = 0
while True:
	cur = -q.get()
	if t > cur:
		break
	t += 1
	cur -= 1
	ans += 1
	q.put(-cur)
print(ans)


from queue import PriorityQueue as PQ
n = int(input())
(r, *a) = [int(x) for x in input().split()]
pq = PQ()
for x in a:
	pq.put((-x, x))
ans = 0
while True:
	(p, x) = pq.get()
	if x < r:
		break
	r += 1
	x -= 1
	pq.put((-x, x))
	ans += 1
print(ans)


n = int(input())
L = list(map(int, input().rstrip().split()))
initial = L[0]
bear = L[0]
L = L[1:]
while bear <= max(L):
	bear += 1
	x = L.index(max(L))
	L[x] -= 1
print(bear - initial)


n = int(input())
s = list(map(int, input().split()))
max1 = max(s[1:])
ind = s[1:].index(max1) + 1
schet = 0
while s[0] <= max1:
	s[0] += 1
	s[ind] -= 1
	max1 = max(s[1:])
	ind = s[1:].index(max1) + 1
	schet += 1
print(schet)


n = int(input())
(my, *l) = map(int, input().split())
res = 0
while True:
	l.sort(reverse=True)
	if my <= l[0]:
		res += 1
		l[0] -= 1
		my += 1
	else:
		break
print(res)


n = int(input())
a = list(map(int, input().split()))
b = a[0]
a = a[1:]
a.sort()
a = a[::-1]
p = 0
while len(a) > 0:
	i = 0
	while True:
		if i >= len(a):
			break
		x = a[i]
		if x < b:
			break
		a[i] -= 1
		p += 1
		b += 1
		a.sort()
		a = a[::-1]
	if i == 0:
		break
print(p)


__author__ = 'JohnHook'
n = list(map(int, input().split()))[0]
a = list(map(int, input().split()))
(k, a[0]) = (a[0], 0)
ans = 0
while k <= max(a):
	k += 1
	ans += 1
	a[a.index(max(a))] -= 1
print(ans)


import heapq
n = map(int, input().split())
votes = list(map(int, input().split()))
limak = votes[0]
votes = [-x for x in votes[1:]]
heapq.heapify(votes)
ans = 0
while limak <= -votes[0]:
	vote = -heapq.heappop(votes)
	vote -= 1
	limak += 1
	ans += 1
	heapq.heappush(votes, -vote)
print(ans)


n = int(input())
(b, *l) = map(int, input().split())
l = sorted(l)
ans = 0
while b <= l[-1]:
	l[-1] -= 1
	b += 1
	ans += 1
	l = sorted(l)
print(ans)


n = int(input())
a = list(map(int, input().split()))
t1 = a[0]
b = sorted(a, reverse=True)

def fn(t1, b):
	if t1 == b[0] and b[0] > b[1]:
		return 0
	i = 0
	while t1 <= b[0]:
		t1 += 1
		b[0] -= 1
		b = sorted(b, reverse=True)
		i += 1
	return i
print(fn(t1, b))


n = int(input())
l = list(map(int, input().split()))
ans = 0
while True:
	ma = l[0]
	k = 0
	for i in range(1, n):
		if l[i] >= ma:
			ma = l[i]
			k = i
	if k != 0 and ma >= l[0]:
		l[0] += 1
		l[k] -= 1
		ans += 1
	else:
		break
print(ans)


import time
n = int(input())
A = [int(i) for i in input().split()]
start = time.time()
l0 = A[0]
l = A[0]
A = A[1:]
m = max(A)
while l <= m:
	l += 1
	A[A.index(m)] -= 1
	m = max(A)
print(l - l0)
finish = time.time()


x = int(input(''))
rida = input('')
rida = rida.split()
nrs = list(map(int, rida))
count = nrs.pop(0)
startcount = count
while count <= max(nrs):
	ind = nrs.index(max(nrs))
	nrs[ind] = nrs[ind] - 1
	count = count + 1
print(str(count - startcount))


n = int(input())
a = [int(i) for i in input().split()]
p = a[0]
a = a[1:]
w = 0
while p <= max(a):
	p += 1
	q = max(a)
	a.remove(q)
	a.append(q - 1)
	w += 1
print(w)


n = int(input())
l1 = list(map(int, input().split()))
l = []
candy = 0
for i in range(1, n):
	if l1[i] >= l1[0]:
		l.append(l1[i])
if len(l) >= 1:
	mm = max(l)
	while mm >= l1[0]:
		candy += 1
		l[l.index(mm)] = mm - 1
		l1[0] = l1[0] + 1
		mm = max(l)
print(candy)


n = int(input())
a = list(map(int, input().split()))
i = -1
k = 0
lim = a[0]
a[0] = 0
a = sorted(a)
n -= 1
while lim <= a[-1]:
	while lim <= a[-1] and a[-1] >= a[-2]:
		lim += 1
		a[-1] -= 1
		k += 1
	while i > -n and a[i] < a[i - 1]:
		(a[i], a[i - 1], i) = (a[i - 1], a[i], i - 1)
	i = -1
print(k)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import add
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
(x, *arr) = rr()
ans = 0
while 1:
	c = max(arr)
	if c < x:
		break
	x += 1
	ans += 1
	arr.remove(c)
	arr.append(c - 1)
print(ans)


n = int(input())
l = list(map(int, input().split()))
ans = 0
while l[0] < max(l):
	l[l.index(max(l))] -= 1
	l[0] += 1
	ans += 1
if l.count(max(l)) > 1:
	print(ans + 1)
else:
	print(ans)


n = int(input())
a = input().split()

def max(aa):
	mi = 1
	for i in range(n - 1):
		if int(aa[i + 1]) > int(aa[mi]):
			mi = i + 1
	return mi
s = 0
while int(a[0]) <= int(a[max(a)]):
	s += 1
	mi = max(a)
	a[0] = str(int(a[0]) + 1)
	a[max(a)] = str(int(a[max(a)]) - 1)
print(s)


n = int(input())
s = list(map(int, input().split()))
cnt = 0
while True:
	b = s[0]
	s = s[1:]
	a = max(s)
	s.reverse()
	s.append(b)
	s.reverse()
	b = s.index(a)
	if s[0] < a:
		cnt += 1
		s[0] += 1
		s[b] -= 1
	elif s[0] == a:
		cnt += 1
		break
	else:
		break
print(cnt)


n = int(input())
votes = list(map(int, input().split()))
bear = votes[0]
votes.pop(0)
votes.sort()
votes.reverse()
result = 0
while bear <= votes[0]:
	top = votes[0]
	for (i, vote) in enumerate(votes):
		if bear > votes[i] or votes[i] != top:
			break
		votes[i] -= 1
		result += 1
		bear += 1
print(result)


n = int(input())
a = [int(i) for i in input().split()]
x = 0
while a[0] < max(a):
	a[a.index(max(a))] -= 1
	a[0] += 1
	x += 1
if a.count(max(a)) > 1:
	print(x + 1)
else:
	print(x)


n = int(input())
mylist = list(map(int, input().split()))
count = 0
while mylist[0] <= max(mylist[1:]):
	mylist[mylist[1:].index(max(mylist)) + 1] -= 1
	mylist[0] += 1
	count += 1
print(count)


N = int(input())
A = list((int(i) for i in input().split()))

def getMax():
	global A
	maxv = -1
	maxi = -1
	for i in range(len(A)):
		if A[i] >= maxv:
			maxv = A[i]
			maxi = i
	return maxi
bribes = 0
k = getMax()
while k != 0:
	A[k] -= 1
	A[0] += 1
	bribes += 1
	k = getMax()
print(bribes)


n = int(input())
nums = input().split()
limak = int(nums[0])
nums.pop(0)
has_win = False
count = 0
for i in range(0, len(nums)):
	nums[i] = int(nums[i])
while has_win == False:
	nums.sort(reverse=True)
	if limak <= int(nums[0]):
		limak = limak + 1
		nums[0] = nums[0] - 1
		count = count + 1
	else:
		has_win = True
print(count)


__author__ = 'artyom'

def main():
	n = read() - 2
	t = read(3)
	z = t[0]
	a = sorted(t[1:])[::-1]
	c = i = 0
	while z <= a[i]:
		z += 1
		c += 1
		a[i] -= 1
		if i < n and a[i + 1] > a[i]:
			i += 1
		else:
			i = 0
	return c

def read(mode=1, size=None):
	if mode == 0:
		return input().strip()
	if mode == 1:
		return int(input().strip())
	if mode == 2:
		return input().strip().split()
	if mode == 3:
		return list(map(int, input().strip().split()))
	a = []
	for _ in range(size):
		a.append(read(3))
	return a

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, tuple) or isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='\n')
write(main())


from collections import *

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input())
n = val()
l = li()
tot = 0
for i in range(n):
	l[i] = [l[i], i]
l.sort(reverse=1)
while l[0][1] != 0:
	ind = 0
	for i in range(len(l)):
		if l[i][1] == 0:
			ind = i
			break
	l[ind][0] += 1
	l[0][0] -= 1
	tot += 1
	l.sort(reverse=1)
print(tot)


n = int(input())
a = list(map(int, input().split()))
mx = a[0]
c = 0
while 1:
	if max(a) == mx and a.count(mx) == 1:
		break
	a.sort()
	ind = a.index(mx)
	mx += 1
	a[ind] += 1
	c += 1
	a[-1] -= 1
print(c)


n = int(input(''))
a = list(map(int, input().split()))
v0 = a[0]
a = a[1:]
a.sort(reverse=True)
cnt = 0
while v0 <= a[0]:
	a[0] -= 1
	cnt += 1
	v0 += 1
	a.sort(reverse=True)
print(cnt)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	n = I()
	a = LI()
	t = a[0]
	b = sorted(a[1:], reverse=True)
	for i in range(b[0] + 2):
		k = t + i
		u = 0
		for c in b:
			if k > c:
				break
			u += c - k + 1
		if u <= i:
			return i
	return -1
print(main())


def main():
	n = int(input())
	(v, *a) = [int(i) for i in input().split()]
	for i in range(1002):
		t = max(a)
		if v > t:
			print(i)
			break
		a.append(t - 1)
		a.remove(t)
		v += 1
main()


n = int(input())
a = list(map(int, input().split()))
cnt = 0
while max(a[1:]) >= a[0]:
	i = 0
	for j in range(len(a)):
		if a[j] == max(a):
			i = j
	a[i] -= 1
	a[0] += 1
	cnt += 1
print(cnt)


n = int(input())
arr = list(map(int, input().split()))
st = arr[0]
ind = arr.index(max(arr))
while arr[0] < max(arr[1:]):
	arr[ind] -= 1
	arr[0] += 1
	ind = arr.index(max(arr))
if arr[0] == max(arr[1:]):
	k = arr[0] + 1
else:
	k = arr[0]
print(k - st)


n = int(input())
l = [int(i) for i in input().split()]
ct = 0
while max(l) > l[0]:
	y = l.index(max(l))
	l[y] -= 1
	l[0] += 1
	ct += 1
for i in range(1, len(l)):
	if l[i] == l[0]:
		ct += 1
		break
print(ct)


n = int(input())
arr = list(map(int, input().split()))
mishka = arr[0]
arr[0] = -1
ans = 0
while mishka < max(arr) and mishka != max(arr):
	mishka += 1
	arr[arr.index(max(arr))] = max(arr) - 1
	ans += 1
if mishka == max(arr):
	ans += 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
c = 0
while l[0] <= max(l[1:]):
	l[l[1:].index(max(l)) + 1] -= 1
	l[0] += 1
	c += 1
print(c)


input()
lst = list(map(int, input().split()))
limak = lst[0]
bonus = 0
lst = lst[1:]
while limak + bonus <= max(lst):
	bonus += 1
	lst[lst.index(max(lst))] -= 1
print(bonus)


n = int(input())
a = []
maximum = -1
for x in input().split():
	vote = int(x)
	maximum = max(maximum, vote)
	a.append(vote)
for bribe in range(0, maximum - a[0] + 2):
	cnt = a[0] + bribe
	delta = 0
	for i in range(1, n):
		if a[i] >= cnt:
			delta += a[i] - cnt + 1
	if delta <= bribe:
		print(bribe)
		break


def check(n, lst, lk):
	lst[0] += lk
	for i in range(1, n):
		if lst[i] >= lst[0]:
			lk -= lst[i] - lst[0] + 1
	if lk >= 0:
		return True
	return False
n = int(input())
lst = list(map(int, input().split()))
left = -1
right = 100000
while left + 1 < right:
	middle = (left + right) // 2
	if check(n, lst.copy(), middle):
		right = middle
	else:
		left = middle
print(right)


n = int(input())
a = list(map(int, input().split()))
z = a[0]
y = a[0]
if max(a) == z and a.count(z) == 1:
	print(0)
elif max(a) == z:
	print(1)
else:
	while z != max(a):
		i = a.index(max(a))
		z += 1
		a[0] = z
		a[i] -= 1
	if a.count(z) != 1:
		z += 1
	print(z - y)


n = int(input())
(b, *l) = list(map(int, input().split()))
l = sorted(l)
a = 0
while b <= l[-1]:
	l[-1] -= 1
	b += 1
	a += 1
	l = sorted(l)
print(a)


n = int(input())
c = list(map(int, input().split()))
count = 0
while True:
	maxx = c[0]
	point = 0
	for i in range(len(c)):
		if c[i] >= maxx:
			maxx = c[i]
			point = i
	if point != 0:
		c[0] += 1
		c[point] -= 1
		count += 1
	else:
		break
print(count)


from heapq import heappush, heappop
n = int(input(''))
p = [-1 * int(x) for x in input('').split()]

def comparacion(a, b):
	return a < b
lizy = p[0]
heap = []
for i in p[1:]:
	heappush(heap, i)
maximo = heappop(heap)
if comparacion(lizy, maximo):
	print(0)
else:
	count = 0
	while not comparacion(lizy, maximo):
		maximo += 1
		lizy -= 1
		count += 1
		heappush(heap, maximo)
		maximo = heappop(heap)
	print(count)


n = int(input())
a = list(map(int, input().split()))
c = a[0]
a = a[1:]
y = max(a)
ans = 0
while y >= c:
	if c <= y:
		c += 1
		ans += 1
		a[a.index(y)] -= 1
	y = max(a)
print(ans)


n = int(input())
j = 0
s = input()
a = s.split()
for i in range(n):
	a[i] = int(a[i])

def findmax():
	global max1, max2, imax1, imax2, a
	max2 = 0
	imax2 = imax1 = 0
	max1 = max(a)
	for i in range(n - 1, -1, -1):
		if a[i] == max1:
			imax1 = i
			break
	for i in range(n):
		if (max1 >= a[i] >= max2) & (i != imax1):
			max2 = a[i]
			imax2 = i
	return 1
c = 1
findmax()
if (max1 == a[0]) & (max1 not in a[1:n]):
	pass
else:
	while c == 1:
		findmax()
		while (max1 >= max2) & (max1 >= a[0]):
			max1 -= 1
			a[imax1] -= 1
			a[0] += 1
			j += 1
		else:
			if (a[0] > max1) & (a[0] > max2):
				c = 0
print(j)


n = int(input())
a = list(map(int, input().split()))
limak = a[0]
a = a[1:]
c = [0] * 1001
for votes in a:
	c[votes] += 1
ans = 0
cur_max = max(a)
while limak <= cur_max:
	limak += 1
	ans += 1
	c[cur_max] -= 1
	c[cur_max - 1] += 1
	if c[cur_max] == 0:
		cur_max -= 1
print(ans)


input()
(a, *b) = map(int, input().split())
b = sorted(b, reverse=True)
c = 0
while b[0] >= a:
	a += 1
	b[0] -= 1
	c += 1
	b = sorted(b, reverse=True)
print(c)


n = int(input())
votes = [int(i) for i in input().split()]
me = votes[0]
rest = votes[1:]
rest.sort(reverse=True)
answer = 0
while me <= rest[0]:
	me += 1
	rest[0] -= 1
	answer += 1
	rest.sort(reverse=True)
print(answer)


(n, count) = (input(), 0)
votes = [int(x) for x in input().split()]
hero = votes.pop(0)
while hero <= max(votes):
	hero += 1
	count += 1
	votes[votes.index(max(votes))] -= 1
print(count)


n = int(input())
candidates = list(map(int, input().split()))
count = 0
while True:
	limak = candidates[0]
	pretendent = max(candidates[1:])
	if limak < pretendent:
		candidates[candidates.index(pretendent)] = pretendent - 1
		candidates[0] = limak + 1
		count += 1
	elif limak == pretendent:
		candidates[candidates.index(pretendent)] = pretendent - 1
		candidates[0] = limak + 1
		count += 1
		break
	else:
		break
print(count)


n = int(input())
k = [int(s) for s in input().split()]
l = k[0]
k.remove(l)
k.sort(reverse=1)
res = 0
while l <= k[0]:
	res += 1
	l += 1
	k[0] -= 1
	i = 0
	if len(k) > 1:
		while k[i] < k[i + 1]:
			g = k[i]
			k[i] = k[i + 1]
			k[i + 1] = g
			i += 1
			if i >= n - 2:
				break
print(res)


def check(l):
	if l.index(max(l)) == len(l) - 1:
		return False
	else:
		return True
n = int(input())
l = list(map(int, input().split()))
ans = 0
l.append(l[0])
l.remove(l[0])
while check(l):
	t = l.index(max(l))
	l[t] = l[t] - 1
	l[-1] += 1
	ans += 1
print(ans)


n = int(input())
l_v = list(map(int, input().split()))
t = 0
while l_v[0] != max(l_v) or l_v.count(l_v[0]) > 1:
	m_i = 0
	m = l_v[0]
	for i in range(n - 1):
		if l_v[1 + i] >= m:
			m = l_v[1 + i]
			m_i = i + 1
	l_v[m_i] -= 1
	l_v[0] += 1
	t += 1
print(t)


n = int(input())
votes = list(map(int, input().split()))
limak = votes[0]
others = list(reversed(sorted(votes[1:])))
bribes = 0
while limak <= others[0]:
	limak += 1
	others[0] -= 1
	bribes += 1
	if len(others) > 1:
		if others[0] < others[1]:
			for i in range(2, len(others)):
				if others[i] <= others[0]:
					(others[i - 1], others[0]) = (others[0], others[i - 1])
					break
			else:
				(others[0], others[-1]) = (others[-1], others[0])
print(bribes)


n = int(input())
a = list(map(int, input().split()))
ans = 0
a[1:] = sorted(a[1:], reverse=True)
while a[0] <= a[1]:
	a[1] -= 1
	ans += 1
	a[0] += 1
	a[1:] = sorted(a[1:], reverse=True)
print(ans)


c = int(input())
lst = list(map(int, input().split()))
limak = lst[0]
org = lst[0]
del lst[0]
while limak <= max(lst):
	lst.append(max(lst) - 1)
	lst.remove(max(lst))
	limak += 1
print(limak - org)


n = int(input())
L = list(map(int, input().split()))
p = 0
f = tuple(L)
k = 0
for m in range(1, 5000):
	for i in range(0, n):
		if L[i] >= p:
			p = L[i]
			k = i
	if k == 0:
		print(L[0] - f[0])
		break
	L[k] = L[k] - 1
	L[0] = L[0] + 1
	p = 0


n = int(input())
a = list(map(int, input().split()))
ans = 0
limak = a[0]
a = a[1:]
a.sort()
mx_add = sum(a)
for i in range(mx_add + 1):
	cur_add = 0
	for j in range(len(a)):
		if a[j] >= limak + i:
			cur_add += a[j] - (limak + i - 1)
	if cur_add <= i:
		print(i)
		exit(0)


l = int(input())
votes = list(map(int, input().split()))
vneed = 0
while True:
	vmax = sorted(votes, reverse=True)[0]
	vlim = votes[0]
	if vlim >= vmax and votes.count(vlim) == 1:
		break
	votes[votes[1:].index(vmax) + 1] -= 1
	votes[0] += 1
	vneed += 1
print(vneed)


from sys import stdin, stdout
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
v = a[0]
a = sorted(a[1:], reverse=True)
ans = 0
if len(a) == 1:
	a += [0]
while v <= a[0]:
	v += 1
	ans += 1
	a[0] -= 1
	if a[0] < a[1]:
		a.sort(reverse=True)
stdout.write(str(ans))


n = int(input())
votes = list(map(int, input().split()))
Limak_vote = votes[0]
cand = votes[1:]
out = 0
while True:
	if max(cand) >= Limak_vote:
		Limak_vote += 1
		cand[cand.index(max(cand))] -= 1
		out += 1
	elif max(cand) < Limak_vote:
		break
print(out)


import time
SIZE = 26
matrix = [[0] * SIZE for i in range(SIZE)]
arr = [0] * SIZE

def timer(func, *pargs, **kargs):
	start = time.time()
	func(*pargs, **kargs)
	return time.time() - start
n = int(input())
a = list(map(int, input().split()))
(value, c) = (a[0], 0)
a = a[1:]
while True:
	a.sort(reverse=True)
	if value > a[0]:
		break
	value += 1
	c += 1
	a[0] -= 1
print(c)


input()
s = list(map(int, input().split()))
k = 0
while s[0] <= max(s[1:]):
	s[1 + s[1:].index(max(s))] -= 1
	s[0] += 1
	k += 1
print(k)


import sys

def main():
	l1 = sys.stdin.readline().strip()
	l2 = sys.stdin.readline().strip()
	lines = [l1, l2]
	result = str(solve(lines))
	sys.stdout.write(result + '\n')

def solve(lines):
	votes = [int(v) for v in lines[1].split()]
	if len(votes) == 1:
		return 0
	base_vote = votes[0]
	votes = votes[1:]
	votes = [v for v in votes if v >= base_vote]
	votes_stolen = 0
	while any((base_vote <= x for x in votes)):
		i = max_vote_index(votes)
		votes[i] -= 1
		base_vote += 1
		votes_stolen += 1
	return votes_stolen

def max_vote_index(votes):
	index_of_max = 0
	if len(votes) == 1:
		return 0
	for i in range(1, len(votes)):
		if votes[i] > votes[index_of_max]:
			index_of_max = i
	return index_of_max
main()


N = int(input())
x = [int(i) for i in input().split()]
import operator
iters = 0
while True:
	(i, v) = max(enumerate(x[1:]), key=operator.itemgetter(1))
	if x[0] > max(x[1:]):
		print(iters)
		break
	x[i + 1] -= 1
	x[0] += 1
	iters += 1


n = int(input())
a = input().split()
l = int(a[0])
for i in range(len(a)):
	a[i] = int(a[i])
if max(a) == l:
	c = 0
	for i in range(len(a)):
		if a[i] == l:
			c += 1
		if c == 2:
			break
	if c == 2:
		print(1)
	else:
		print(0)
else:
	f = True
	c = 0
	while f:
		a[a.index(max(a))] = a[a.index(max(a))] - 1
		l += 1
		c += 1
		if l == max(a):
			e = 0
			for i in range(len(a)):
				if a[i] == l:
					e = 1
					break
			if e == 0:
				f = False
		elif l > max(a):
			f = False
	print(c)


n = int(input())
a = list(map(int, input().split()))
k = a[0]
if len(set(a)) == 1:
	print(1)
else:
	a.remove(k)
	a.sort(reverse=True)
	co = 0
	while True:
		a.sort(reverse=True)
		if k > a[0]:
			print(co)
			break
		else:
			k += 1
			a[0] -= 1
			co += 1


counter = 0
amount = int(input())
votes = map(int, input().split())
vote_list = list(votes)
bear = vote_list[0]
del vote_list[0]
vote_list.sort(reverse=True)
while bear <= vote_list[0]:
	bear += 1
	vote_list[0] -= 1
	counter += 1
	vote_list.sort(reverse=True)
print(counter)


n = input()
(me, *cans) = list(map(int, input().split(' ')))
c = 0
while True:
	m = max(enumerate(cans), key=lambda x: x[1])
	if m[1] < me:
		break
	cans[m[0]] -= 1
	me += 1
	c += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
ans = 0
while True:
	v = max(a[1:])
	p = a[1:].index(v) + 1
	if a[0] > v:
		break
	a[p] -= 1
	a[0] += 1
	ans += 1
print(ans)


import heapq
n = int(input())
a = list(map(int, input().split()))
b = a[0]
a = a[1:]
for i in range(n - 1):
	a[i] = -a[i]
heapq.heapify(a)
ans = 0
while True:
	cur = heapq.heappop(a)
	cur = -cur
	if cur < b:
		break
	ans += 1
	b += 1
	cur -= 1
	cur = -cur
	heapq.heappush(a, cur)
print(ans)


n = int(input())
a = list(map(int, input().split()))
k = a[0]
k2 = a[0]
del a[0]
cnt = 0
while k <= max(a):
	a.append(max(a) - 1)
	a.remove(max(a))
	k += 1
	cnt += 1
print(cnt)


(_, a) = (input(), list(map(int, input().split())))
l = a[0]
b = [0] * 1001
s = l
for i in range(1, len(a)):
	b[a[i]] += 1
for i in range(len(b) - 1, -1, -1):
	if l > i:
		print(0)
		break
	elif b[i] > 0:
		if s + b[i] > i:
			print(i - l + 1)
			break
		s += b[i]
		b[i - 1] += b[i]


n = int(input())
arr1 = list(map(int, input().split()))
ok = arr1[0]
arr1 = arr1[1:]
arr1.sort()
count = 0
while ok <= arr1[-1]:
	arr1[-1] -= 1
	ok += 1
	count += 1
	arr1.sort()
print(count)