You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface.

The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$.

There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ 

Find the maximum number of blocks you can remove such that the views for both the cameras would not change.

Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.


-----Input-----

The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right.


-----Output-----

Print exactly one integer — the maximum number of blocks that can be removed.


-----Examples-----
Input
5 6
3 3 3 3 3

Output
10
Input
3 5
1 2 4

Output
3
Input
5 5
2 3 1 4 4

Output
9
Input
1 1000
548

Output
0
Input
3 3
3 1 1

Output
1


-----Note-----

The following pictures illustrate the first example and its possible solution.

Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Implementation

Greedy algorithms


Solutions:
def solve(xs):
	if len(xs) == 1:
		return 0
	heights = sorted(xs)[::-1]
	used = 0
	needed = heights[0]
	for i in range(len(heights)):
		current = heights[i]
		if needed <= current:
			used += 1
			needed -= 1
		else:
			used += needed - current
			used += 1
			needed = current - 1
	if needed > 0:
		used += needed
	return sum(xs) - used
(n, m) = map(int, input().split())
xs = list(map(int, input().split()))[:n]
print(solve(xs))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
temp = 1
top = 0
side = 0
total = sum(a)
for i in range(n):
	if i == 0:
		top += 1
		side += 1
		total -= 1
	elif a[i] == temp:
		top += 1
		total -= 1
	elif a[i] > temp:
		side += 1
		top += 1
		temp += 1
		total -= 1
if side < a[n - 1]:
	total -= a[n - 1] - side
if n == 1:
	print(0)
else:
	print(total)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
s = sum(a)
need = 0
a.sort()
j = 1
flag = 0
k = max(a)
if n == 1:
	print(0)
else:
	for i in range(n):
		if a[i] < j:
			flag = 1
		else:
			flag = 0
		if a[i] == 1:
			need += 1
		elif a[i] >= j and i != n - 1:
			need += 1
		elif a[i] >= j and i == n - 1 and (j <= k):
			need += k - j + 1
		else:
			need += 1
		if flag != 1:
			j += 1
	print(s - need)


(n, m) = map(int, input().split())
a = sorted([int(x) for x in input().split()])
total = 0
m = max(a)
for i in range(n):
	if a[i] > total:
		total += 1
print(sum(a) - n - m + total)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
x = n
h = 0
for i in range(n):
	if a[i] > h:
		h += 1
y = a[-1] - h
print(sum(a) - x - y)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
s = sum(a)
h = 0
req = 0
m_h = a[-1]
if a[0] != 0:
	req = 1
	h = 1
for i in range(1, n):
	if a[i] > a[i - 1]:
		h += 1
		req += 1
	elif a[i] != 0:
		if h < a[i]:
			req += 1
			h += 1
		else:
			req += 1
req += max(0, m_h - h)
print(s - req)


(x, y) = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
t = z = 0
for i in range(x):
	z += a[i] - t
	if z <= i:
		z += 1
	t = a[i]
print(sum(a) - z)


R = lambda : map(int, input().rstrip().split())
(n, m) = R()
a = list(sorted(R()))
x = 0
prev = 0
for i in range(1, n + 1):
	x += 1
	if a[i - 1] > prev:
		prev += 1
x += a[n - 1] - prev
print(sum(a) - x)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
h = max(a)
a.sort()
a.reverse()
a.append(0)
ans = sum(a)
for i in range(n):
	if a[i + 1] >= a[i]:
		a[i + 1] = a[i] - 1
	ans -= max(a[i] - a[i + 1], 1)
print(ans)


(n, m) = [int(x) for x in input().split(' ')]
z = [int(x) for x in input().split(' ')]
z.sort()
(a, b, ans) = (0, 0, 0)
for i in range(n - 1):
	if z[i] <= 1:
		if z[i] == 1:
			a = 1
		continue
	if z[i] > 1:
		if z[i] > a:
			ans += z[i] - 1
		else:
			ans += a - 1
	a += 1
	a = min(a, z[i])
if z[-1] > a:
	ans += a
else:
	ans += z[-1] - 1
print(ans)


line1 = input().split()
(row, col) = (int(line1[0]), int(line1[1]))
blocks = [int(x) for x in input().split()]

def count_removable(blocks):
	rows = len(blocks)
	cols = max(blocks)
	blocks.sort()
	can_replace = 1
	for i in range(1, len(blocks)):
		block = blocks[i]
		if block > can_replace:
			can_replace += 1
	keep = rows + cols - can_replace
	return sum(blocks) - keep
print(count_removable(blocks))


import collections
(x, y) = map(int, input().split())
blocks = [(k, v) for (k, v) in collections.Counter(map(int, input().split())).items()]
blocks.sort(key=lambda x: x[0], reverse=True)
total = 0
next_h = blocks[0][0]
for (h, c) in blocks:
	if next_h > h:
		total -= next_h - h
	total += h * c - c
	next_h = max(0, min(h, next_h) - c)
total -= next_h
print(total)


(n, m) = [int(x) for x in input().split()]
a = [0] + [int(x) for x in input().split()]
a.sort()
asum = sum(a)
amax = a[n]
d = 0
for i in range(1, n + 1):
	if a[i] + d < i:
		d = i - a[i]
print(asum - (n + (amax - n) + d))


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
c = 0
maxi = 0
for i in range(n):
	if maxi + 1 > a[i]:
		c += a[i] - 1
		pass
	else:
		maxi += 1
		c += a[i] - 1
print(c - a[-1] + maxi)


import math
import sys
DEBUG = False

def inp():
	return sys.stdin.readline().rstrip()

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)

def solve(N, M, A):
	A.sort(reverse=True)
	lh = A[0]
	cnt = 1
	for a in A[1:]:
		if lh == 1:
			cnt += 1
		elif lh - 1 <= a:
			cnt += 1
			lh -= 1
		else:
			cnt += lh - a
			lh = a
	cnt += lh - 1
	return sum(A) - cnt

def main():
	(N, M) = [int(e) for e in inp().split()]
	A = [int(e) for e in inp().split()]
	assert len(A) == N
	print(solve(N, M, A))
main()


a = input()
n = int(a.split()[0])
m = int(a.split()[1])
b = input()
l = list(map(int, b.split()))
l.sort()
h = 0
k = 0
for i in range(n):
	if h < l[n - 1] and l[i] >= h + 1:
		h = h + 1
		k = k + 1
	else:
		k = k + 1
k = k + l[n - 1] - h
print(sum(l) - k)


import sys
(n, m, a) = (0, 0, 0)

def removeFromStack(arr):
	arr.sort()
	(cnt, last, sum) = (0, 0, 0)
	for i in range(len(arr)):
		sum += arr[i]
		cnt += 1
		if arr[i] > last:
			last += 1
	cnt += arr[len(arr) - 1] - last
	return sum - cnt

def main():
	words = nextLineWords()
	n = int(words[0])
	m = int(words[1])
	words = nextLineWords()
	a = []
	for word in words:
		a.append(int(word))
	print(removeFromStack(a))

def nextLineWords():
	return sys.stdin.readline().split()
main()


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
res = 0
pre = l[0] - 1
for i in range(len(l) - 1):
	if l[i] < l[i + 1]:
		res += l[i]
		pre += l[i + 1] - l[i] - 1
	else:
		res += l[i] - 1
		if pre > 0:
			pre -= 1
			res += 1
print(res)


import math
n = [int(i) for i in input().split(' ')]
data = [int(i) for i in input().split(' ')]
data.sort()
data.reverse()
(p, s) = (0, 0)
for i in range(0, n[0] - 1):
	if data[i] <= data[i + 1] and data[i] > 1:
		s += data[i + 1] + 1 - data[i]
		data[i + 1] = data[i] - 1
	if data[i] == 1:
		s += data[i + 1] - 1
		data[i + 1] = 1
for i in range(n[0] - 1):
	if data[i] > 1:
		s += data[i + 1]
print(s)


import sys
import math
line1 = sys.stdin.readline().strip()
line2 = sys.stdin.readline().strip()
(a, b) = line1.split()
(w, h) = (int(a), int(b))
(w, h)
heights = line2.split()
heights = list(map(lambda l: int(l), heights))
h = min(max(heights), h)
heights = list(filter(lambda h: h > 0, heights))
w = min(len(heights), w)
wtc = w
htc = h
needed = 0
if w == 1:
	print(0)
else:
	heights.sort(reverse=True)
	for i in range(len(heights)):
		if i == len(heights) - 1:
			curr_h = heights[i]
			if htc <= 0:
				needed += 1
			else:
				needed += htc
		else:
			dh = htc - heights[i + 1]
			if dh > 0:
				htc -= dh
				needed += dh
			elif dh == 0:
				htc -= 1
				needed += 1
			else:
				htc -= 1
				needed += 1
	print(sum(heights) - needed)


(n, m) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
(sum1, sum2) = (sum(arr), 0)
arr = sorted(arr)
maxi = arr[n - 1]
sum2 = 0
count = 0
if n == 1:
	print(0)
else:
	for i in range(n):
		if i == n - 1:
			sum2 += arr[n - 1] - count
		elif count + 1 == maxi:
			sum2 += 1
		elif count + 1 <= arr[i]:
			sum2 += 1
			count += 1
		else:
			sum2 += 1
	print(sum1 - sum2)


(n, m) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
total = sum(a)
selected = 0
for i in range(0, n):
	if a[i] > selected:
		selected += 1
left = a[-1] - selected
print(total - left - n)


import math
(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
res = 0
nr = 1
f = 1
for i in range(len(a) - 1):
	if a[i] != 0:
		if a[i] == 1:
			nr = 2
			continue
		if nr > a[i]:
			res += max(0, max(0, nr - 1) - 1)
		else:
			res += max(0, a[i] - nr) + max(0, nr - 1)
		if a[i] - nr >= 0:
			f = 0
		if f == 0:
			nr += 1
			f = 1
if nr > a[-1]:
	res += max(0, max(0, nr - 1) - 1)
else:
	res += max(0, nr - 1)
print(res)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
right = arr[0]
c = 0
for i in range(n):
	if arr[i] >= right:
		c += 1
		right -= 1
	else:
		c += 1
		right = arr[i] - 1
	if right == 0:
		break
print(sum(arr) - (n - c) - arr[0])


(n, m) = map(int, input().split())
arr = [int(x) for x in input().split()]
tot = sum(arr)
ma = max(arr)
arr.sort()
for i in range(n):
	if arr[i] == 0:
		del arr[i]
l = len(arr)
if True:
	h = 0
	left = 0
	for i in range(l - 1):
		if arr[i] > h:
			h += 1
		left += 1
	if h == ma:
		left += 1
	left += ma - h
print(tot - left)


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')
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
m = max(arr)
arr.sort()
var = m - 1
if var == 0:
	print(sum(arr) - n)
else:
	ans = 0
	for i in range(n - 1):
		if arr[i] > ans:
			ans += 1
	f = m - 1 - ans
	print(sum(arr) - n if ans >= m - 1 else sum(arr) - n - f)


(n, m) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
arr = sorted(arr, reverse=True)
ak = []
for i in range(n):
	ak.append(arr[i])
for i in range(n - 1):
	if arr[i] == min(arr[i], arr[i + 1]):
		arr[i + 1] = max(1, arr[i] - 1)
ans = 0
for i in range(n):
	ans += ak[i] - arr[i]
for i in range(n - 1):
	if arr[i] != 1:
		ans += arr[i + 1]
if len(arr) > 1:
	if arr[n - 1] != 1:
		ans += arr[n - 1] - arr[n - 1]
else:
	ans = 0
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = sum(a)
a.sort()
lastlevel = 0
level = 0
got = 0
for i in a:
	got = max(got, i)
	level = min(level + 1, got)
	if i > 0:
		ans -= 1
		lastlevel = level
ans -= got - level
print(ans)


input()
sl = map(int, input().split())
sl = sorted(sl)
h = t = 0
for s in sl[:-1]:
	if s > h:
		h += 1
	t += s - 1
if h == sl[-1]:
	t += h - 1
else:
	t += h
print(t)


(n, m) = map(int, input().split())
a = [int(s) for s in input().split()]
a = sorted(a)
current = a[n - 1]
k = 0
if n == 1:
	print(0)
	exit()
for i in range(n - 1, -1, -1):
	if i > 0 and a[i - 1] < current - 1:
		a[i] -= current - 1 - a[i - 1]
		k += a[i] - 1
		current = a[i - 1]
	elif a[i] >= current:
		k += a[i] - current
		if i != 0:
			k += max(current - 1, 0)
		current -= 1
	current = max(current, 1)
print(k)


(n, m) = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
D = {}
for i in L:
	if i in D:
		D[i] += 1
	else:
		D[i] = 1
S = sorted(D.keys(), reverse=True) + [0]
height = S[0]
extra = 0
for i in range(0, len(S) - 1):
	height -= max(D[S[i]], max(0, min(S[i], height) - S[i + 1]))
print(sum(L) - S[0] + height)


I = input
(n, m) = map(int, I().split())
a = list(map(int, I().split()))
a.sort()
l = 0
h = a[n - 1]
ans = 0
for i in range(n):
	if l == h:
		ans += a[i] - 1
	elif a[i] < l + 1:
		if a[i] > 1:
			ans += a[i] - 1
	elif a[i] >= l + 1:
		if i != n - 1:
			if a[i] > 1:
				ans += a[i] - 1
			l += 1
		elif a[i] > 1:
			ans += l
print(ans)


from functools import reduce
import math
from decimal import *

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def sol():
	(w, h) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	if w == 1:
		print(0)
		return 0
	t = 0
	s = sum(arr)
	arr.sort()
	arr.reverse()
	ind = 0
	out = []
	while ind < w - 1:
		if arr[ind] <= arr[ind + 1]:
			out.append(1)
			arr[ind + 1] = max(arr[ind] - 1, 1)
		ind += 1
	for i in range(w - 1):
		t += max(arr[i] - arr[i + 1], 1)
	t += arr[-1]
	print(s - t)
sol()


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 0
need = max(a)
for col in a:
	if col == 1 and need == 1:
		ans += 1
	else:
		ans += max(1, need - col + 1)
		need = max(1, min(need - 1, col - 1))
if need != 1:
	ans += need
print(sum(a) - ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = 0
l.sort()
k = 0
for i in range(n - 1, 0, -1):
	if l[i] > l[i - 1]:
		s += l[i - 1]
	elif l[i] == l[i - 1] and l[i] > 1:
		s += l[i]
		l[i - 1] = l[i - 1] - 1
	else:
		s += l[i - 1] - l[i]
		if l[i] > 1:
			s += l[i]
			l[i - 1] = l[i] - 1
		else:
			l[i - 1] = l[i]
print(s)


(n, m) = map(int, input().split())
seq = sorted(map(int, input().split()))
ans = 0
for i in range(n):
	ans += seq[i] - 1
wait = 0
last = seq[-1]
for i in range(n - 1, 0, -1):
	wait += 1
	if last - seq[i - 1] > wait:
		ans -= last - seq[i - 1] - wait
		wait = 0
		last = seq[i - 1]
if last - 1 > wait:
	ans -= last - 1 - wait
	wait = 0
print(ans)


(n, m) = map(int, input().split())
L = sorted(map(int, input().split()))
ma = 0
c = 0
for i in range(n):
	if L[i] > ma:
		c += 1
		ma += 1
print(sum(L) - n - max(L) + c)


def main():
	(n, m) = map(int, input().split())
	a = list(reversed(sorted(map(int, input().split()))))
	res = 0
	h = a[0]
	b = []
	for x in a:
		if x > h:
			res += x - h
		h = min(h, x)
		b.append(h)
		if h > 1:
			h -= 1
	for i in range(len(b) - 1):
		if b[i] > b[i + 1]:
			res += b[i + 1]
	print(res)
main()


input1 = [int(x) for x in input().split()]
n = input1[0]
m = input1[1]
_arr = [int(x) for x in input().split()]
arr = sorted(_arr)
result = 0
height = 0
for i in range(n - 1):
	if arr[i] > height:
		height += 1
	result += 1
if arr[-1] == height:
	result += 1
else:
	result += arr[-1] - height
print(sum(arr) - result)


(n, m) = map(int, input().split())
listik = sorted(list(map(int, input().split())))
summa = 0
last = 0
for i in range(n):
	if listik[i] > last:
		last += 1
		summa += 1
print(sum(listik) - n - listik[-1] + summa)


[n, m] = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
a = [i for i in a if i > 0]
needLeft = 1
j = 1
for i in range(n):
	if i == n - 1:
		needLeft += a[i] - j
	elif a[i + 1] > j:
		j += 1
		needLeft += 1
	else:
		needLeft += 1
canDelete = sum(a) - needLeft
print(canDelete)


import sys
(n, m) = map(int, input().split())
A = list(map(int, input().split()))
if n == 1:
	print(0)
	sys.exit()
A.sort(reverse=True)
NOW = A[0]
ANS = 1
i = 1
while i < n:
	if A[i] == A[i - 1]:
		NOW = max(1, NOW - 1)
		ANS += 1
	elif A[i] >= NOW - 1:
		ANS += 1
		NOW = max(NOW - 1, 1)
	else:
		ANS += max(1, NOW - A[i])
		NOW = A[i]
	i += 1
ANS += NOW - 1
print(sum(A) - ANS)


nm = input().split(' ')
(n, m) = list(map(lambda s: int(s), nm))
stacksS = input().split(' ')
stacks = list(map(lambda s: int(s), stacksS))
sumStacks = sum(stacks)
stacks = sorted(stacks)
i = 0
for s in stacks:
	if s > i:
		i += 1
rightView = stacks[n - 1] - i
print(sumStacks - rightView - n)


import sys
readints = lambda : map(int, input().strip('\n').split())
(n, m) = readints()
a = list(readints())
a = list(reversed(sorted(a)))
if n == 1:
	print(0)
	sys.exit(0)
ans = sum(a)
need = a[0]
for x in a:
	if x > need:
		ans -= 1
		need -= 1
	elif x == need:
		ans -= 1
		need -= 1
	elif x < need:
		ans -= 1
		ans -= need - x
		need = x - 1
ans -= max(0, need)
print(ans)


(n, m) = map(int, input().split())
a = [int(o) for o in input().split()]
a = sorted(a)
prev = 0
d = 0
for i in range(n):
	d += a[i] - prev
	if d <= i:
		d += 1
	prev = a[i]
print(sum(a) - d)


import os
import sys
import math
import collections
import cmath

def mp():
	return map(int, input().split())

def inp():
	return input()
(n, m) = mp()
ar = list(mp())
ar.sort()
j = 0
count = ar[0] - 1
if m == 1 or n == 1:
	print(0)
else:
	for i in range(n):
		if ar[i] > j:
			j += 1
	print(sum(ar) - n - max(ar) + j)


ar1 = str(input()).split(' ')
ar2 = sorted(str(input()).split(' '))
ar3 = []
for el in ar2:
	ar3.append(int(el))
ar4 = sorted(ar3)
n = int(ar1[0])
m = int(ar1[1])
summ1 = 0
for i in range(len(ar2)):
	summ1 = summ1 + int(ar2[i])
summ = 0
num = 0
j = 1
for i in range(len(ar4)):
	if j > int(ar4[i]):
		num += 1
		j -= 1
	j += 1
if num > 0:
	print(summ1 - num - int(ar4[len(ar4) - 1]))
else:
	print(summ1 - ar4[len(ar4) - 1])


import sys
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
cnt = a[0] + n - 1
ma = a[0] - 1
for i in range(1, n):
	if ma == 0:
		break
	if ma < a[i]:
		cnt -= 1
		ma -= 1
	else:
		ma = a[i] - 1
		cnt -= 1
print(sum(a) - cnt)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
if n == 1 or m == 1:
	print(0)
else:
	ans = sum(l)
	l.sort(reverse=True)
	for i in range(n - 1):
		if l[i] <= l[i + 1]:
			l[i + 1] = l[i] - 1
	l.append(0)
	for i in range(n):
		ans -= max(1, l[i] - l[i + 1])
	print(ans)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
a.sort()
sm = sum(a)
b = []
for elem in a:
	if elem != 0:
		b.append(elem)
ans = 0
sh = 0
for i in range(len(b)):
	if b[i] > sh:
		sh += 1
print(sm - max(a) - len(b) + sh)


(n, m) = map(int, input().split())
a = sorted([int(i) for i in input().split()])
ans = 0
c = a[n - 1]
if n == 1:
	print(0)
	raise SystemExit
for i in range(n - 1, -1, -1):
	if i > 0 and a[i - 1] < c - 1:
		a[i] -= c - 1 - a[i - 1]
		ans += a[i] - 1
		c = a[i - 1]
	elif a[i] >= c:
		ans += a[i] - c
		if i:
			ans += max(c - 1, 0)
		c -= 1
	c = max(c, 1)
print(ans)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
j = 1
for i in a:
	if i >= j:
		j += 1
print(max(sum(a) - n - max(max(a) - j + 1, 0), 0))


import sys
import math
fileoperation = 0
if fileoperation:
	orig_stdout = sys.stdout
	orig_stdin = sys.stdin
	inputfile = open('W:/Competitive Programming/input.txt', 'r')
	outputfile = open('W:/Competitive Programming/output.txt', 'w')
	sys.stdin = inputfile
	sys.stdout = outputfile
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
t = 1
for tt in range(t):
	(n, k) = map(int, sys.stdin.readline().split(' '))
	a = list(map(int, sys.stdin.readline().split(' ')))
	a.sort()
	ans = 0
	c = 1
	for i in range(n):
		if c > a[n - 1] or c > a[i]:
			ans += a[i] - 1
			continue
		if i != n - 1:
			ans += a[i] - 1
			c += 1
		else:
			ans += c - 1
	print(ans)
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
k = max(A)
s = sum(A)
x = 0
a = 0
A.sort()
for i in range(0, n):
	if A[i] > x:
		x += 1
		a += 1
	else:
		a += 1
if k > x:
	a += k - x
print(s - a)


import math

def solve(n, m, a):
	sz = len(a)
	lo = 0
	min_nid = 0
	su = 0
	lis = sorted(a)
	for i in lis:
		min_nid += 1
		su += i
		if i > lo:
			lo += 1
	min_nid += lis[-1] - lo
	return su - min_nid

def wew(l1, l2):
	(n, m) = list(map(int, l1.split()))
	a = list(map(int, l2.split()))
	return solve(n, m, a)
from sys import stdin

def main():
	print(wew(stdin.readline(), stdin.readline()))
main()


def run():
	(n, mmm) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	for i in range(n):
		if arr[i] > ans:
			ans += 1
	left = arr[n - 1] - ans
	sol = sum(arr) - n - left
	print(sol)
run()


import math
(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
if n == 1:
	print(0)
	exit()
else:
	a.sort()
	_max = max(a)
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] >= _max:
			ans += a[i] - 1
			_max -= 1
		elif a[i] < _max:
			ans -= abs(a[i] - _max)
			ans += a[i] - 1
			_max = a[i] - 1
	if _max > 0:
		ans -= _max
	print(ans)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
last = 0
total = 0
for i in range(n - 1):
	if a[i] > 0:
		total += a[i] - 1
		last = min(last + 1, a[i])
print(total + max(a[n - 1] - max(1, a[n - 1] - last), 0))


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
if n == 1:
	print(0)
	exit(0)
k = 0
for i in a:
	if i > 0:
		k += 1
a.sort()
a = a[::-1]
c = max(a)
d = 0
for i in a:
	if c > i:
		c = i
	if c <= 1:
		break
	d += 1
	c -= 1
ans = sum(a) - (max(a) + k - 1) + d
if d == n and min(a) > 1:
	ans -= 1
print(ans)


_ = input()
a = sorted(map(int, input().split()))
i = s = 0
for x in a:
	s += x - 1
	i = min(x, i + 1)
print(s - a[-1] + i)


(n, m) = map(int, input().split())
a = sorted(map(int, input().split()))
ans = 0
cur = 0
for b in a:
	if b > cur:
		ans += 1
		cur += 1
	else:
		ans += 1
print(sum(a) - (ans + max(a) - cur))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
a.sort()
mx = a[n]
ans = 0
for i in range(n, 0, -1):
	cnt = max(1, mx - a[i - 1])
	ans += a[i] - cnt
	mx -= cnt
print(ans)


(n, t) = input().split()
t = int(t)
n = int(n)
l = list(map(int, input().split()))
m = max(l)
s = sum(l)
l.sort()
j = 1
c = 0
for i in l:
	if i >= j:
		j += 1
	c += 1
c += m - j + 1
print(s - c)


from collections import Counter, defaultdict
(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort()
sumo = sum(l)
c = 0
ans = 0
for i in l:
	ans += 1
	if c < i:
		c += 1
ans += l[-1] - c
print(sumo - ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
h = 0
ans = 0
for i in range(n - 1):
	ans += a[i] - 1
	if a[i] > h:
		h += 1
if h < max(a):
	ans += h
else:
	ans += a[-1] - 1
print(ans)


import math
import sys
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cnt = 0
cut = 1
f = 0
for i in a:
	if i == cut or i > cut:
		cnt += i - 1
		cut += 1
		f = 1
	elif i < cut:
		cnt += i - 1
		cut = i + 1
		f = -1
if f == 1:
	cut -= 1
if a[-1] > cut:
	cnt -= a[-1] - cut
print(cnt)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
mx = a[n - 1]
removE = 0
c = 0
for i in range(n):
	if a[i] > c:
		c += 1
	removE += a[i] - 1
print(removE - (mx - c))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
total = 0
ans = 0
for i in range(n):
	total += a[i]
a.sort()
x = 0
y = 0
for i in range(n):
	x += 1
	if a[i] > y:
		y += 1
x += a[n - 1] - y
ans = total - x
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	l.sort()
	k = 0
	ans = 0
	for i in range(n):
		ans += 1
		if l[i] > k:
			k += 1
	ans += l[n - 1] - k
	print(sum(l) - ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort(reverse=True)
m = a[0]
ct = 0
for i in range(n):
	if m == 0:
		ct += n - i
		break
	ct += 1
	if a[i] < m:
		ct += m - a[i]
		m = a[i]
	m -= 1
print(sum(a) - (m + ct))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
from itertools import accumulate as ac
from random import randint as ri
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = 1
for _ in range(t):
	(n, m) = inp()
	arr = list(inp())
	arr.sort(reverse=True)
	require = max(arr)
	ans = 0
	for i in range(n):
		if require == 0:
			ans += 1
		elif require == arr[i]:
			require -= 1
			ans += 1
		elif require > arr[i]:
			ans += require - arr[i]
			require = arr[i]
			require -= 1
			ans += 1
		else:
			require -= 1
			ans += 1
	ans += require
	ans = sum(arr) - ans
	print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
su = sum(a)
ma = max(a)
if n == 1:
	print(0)
else:
	a.sort()
	c = 0
	for i in range(n - 1, 0, -1):
		if a[i] == 1:
			c += 1
		elif a[i - 1] < a[i]:
			x = min(a[i], ma - c)
			if x <= a[i - 1]:
				c += 1
			else:
				c += x - a[i - 1]
		else:
			c += 1
	if ma - c > 0:
		c += ma - c
	else:
		c += 1
	print(su - c)


(n, m) = map(int, input().split())
l = sorted(map(int, input().split()))
level = 0
for v in l:
	level = min(v, level + 1)
print(sum(l) - n - l[-1] + level)


(n, m) = input().split()
a = [int(i) for i in input().split()]
a.sort()
if 1 in a:
	c = 1
else:
	c = 0
ans = cc = 0
for i in range(int(n)):
	if a[i] == c and cc == 0 or a[i] == c + 1:
		ans += a[i] - 1
	elif a[i] == c and cc > 0:
		ans += c
		cc -= 1
	else:
		ans += c
		cc += a[i] - c - 1
	c = a[i]
if n == '1':
	print(0)
else:
	print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
p = 0
c = n
for i in range(n):
	if l[i] > p:
		p += 1
c += l[len(l) - 1] - p
print(sum(l) - c)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
mx = a[-1]
t = 0
ans = 0
for i in a:
	if i > 0:
		if i > t:
			t += 1
		ans += i - 1
ans -= mx - t
print(ans)


(n, m) = map(int, input().split())
total = red = prev = 0
a = []
for i in map(int, input().split()):
	total += i
	a.append(i)
a.sort()
for i in a:
	red += 1
	if i > prev:
		prev += 1
red += a[-1] - prev
ans = total - red
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
ans = 0
a.sort(reverse=True)
p = a[0]
for j in range(n - 1):
	if p <= 0:
		ans += a[j] - 1
		continue
	if a[j + 1] >= p - 1:
		ans += a[j] - 1
		p -= 1
	else:
		k = p - a[j + 1]
		ans += a[j] - k
		p -= k
if p < 0:
	p = 0
ans += a[-1] - max(p, 1)
print(ans)


s = input().split()
n = int(s[0])
m = int(s[1])
stack = list(map(int, input().split()))
stack.sort()
h = 0
ans = 0
for i in stack:
	ans += 1
	if i > h:
		h += 1
ans += stack[-1] - h
print(sum(stack) - ans)


inp = input().split()
n = int(inp[0])
m = int(inp[1])
inp = input().split()
a = []
for i in inp:
	a.append(int(i))
res = 0
a.sort(reverse=True)
tr = a[0]
for i in range(0, len(a) - 1):
	if tr - 1 > a[i + 1]:
		res += tr - a[i + 1]
		tr = a[i + 1]
	else:
		res += 1
		tr = max(1, tr - 1)
res += tr
print(sum(a) - res)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
s = sum(a)
temp = sorted(a)
res = temp[0]
for x in range(n - 1):
	if temp[x + 1] != temp[x]:
		res = res + (temp[x + 1] - temp[x])
	if x + 2 > res:
		res = res + 1
print(s - res)


(n, m) = map(int, input().split())
A = sorted(map(int, input().split()))
cnt = 0
prev = 0
for a in A:
	cnt += 1
	if a > prev:
		prev += 1
cnt += A[-1] - prev
print(sum(A) - cnt)


(n, m) = list(map(int, input().strip().split(' ')))
a = list(map(int, input().strip().split(' ')))
a.sort()
x = 0
y = 0
count = 0
for i in range(0, n):
	count += a[i]
	if a[i] > y:
		y += 1
	x += 1
	if i == n - 1 and a[i] > y:
		x += a[i] - y
print(count - x)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
a.append(0)
(j, s) = (a[0], 0)
for i in range(n):
	if j > 0:
		if j - 1 <= a[i + 1] and j - 1 > 0:
			j -= 1
			s += 1
		elif j - 1 > a[i + 1]:
			s += j - a[i + 1]
			j = a[i + 1]
		elif a[i + 1] > 0:
			j = 1
			s += 1
		elif a[i + 1] == 0:
			j = 0
			s += 1
			break
	else:
		break
print(sum(a) - s)


R = lambda : list(map(int, input().split()))
(n, m) = R()
a = R()
a.sort()
i = n - 1
re = 0
now = a[i]
while i > 0:
	if now > a[i - 1]:
		now = a[i - 1]
		re += now
	elif now == a[i - 1] and now > 1:
		now -= 1
		re += now + 1
	else:
		if now > 1:
			now -= 1
			re += now
		re += a[i - 1] - now
	i -= 1
print(re)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = 10 ** 6
(r, c) = R()
a = sorted(L(), reverse=True) + [0]
tp = a[0]
ans = 0
for i in range(1, r + 1):
	bt = max(0, min(tp - 1, a[i]))
	ans += min(a[i - 1] - 1, a[i - 1] - (tp - bt))
	tp = bt
print(ans)


(n, k) = map(int, input().split())
arr = list(sorted(map(int, input().split())))
mx = 0
sm = sum(arr)
for v in arr:
	if v > mx:
		mx += 1
reserved = n + arr[-1] - mx
print(sm - reserved)


def solve():
	(N, M) = map(int, input().split())
	A = [int(k) for k in input().split()]
	A.sort()
	height = 0
	for i in range(1, N + 1):
		if A[i - 1] > height:
			height += 1
	res = N + (A[-1] - height)
	print(sum(A) - res)
solve()


import math
from collections import deque
import sys
sys.setrecursionlimit(10 ** 4)

def Divisors(n):
	l = []
	i = 2
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l

def SieveOfEratosthenes(n):
	l = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			l.append(p)
	return l

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(n)
	return l

def Factors(n):
	result = []
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if i == n / i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	result.append(1)
	result.append(n)
	return result

def maxSubArraySum(a):
	max_so_far = 0
	max_ending_here = 0
	size = len(a)
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < abs(max_ending_here):
			max_so_far = max_ending_here
	return max_so_far

def longestsubarray(arr, n, k):
	current_count = 0
	max_count = 0
	for i in range(0, n, 1):
		if arr[i] % k != 0:
			current_count += 1
		else:
			current_count = 0
		max_count = max(current_count, max_count)
	return max_count

def main():
	(n, m) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	l.sort()
	m = 1
	c = 0
	for j in range(0, n - 1):
		if l[j] >= m:
			c += 1
			m += 1
		else:
			c += 1
	if l[n - 1] < m:
		c += 1
	else:
		c += l[n - 1] - m + 1
	print(sum(l) - c)
t = 1
for i in range(0, t):
	main()


(n, m) = map(int, input().split())
a = list(sorted(map(int, input().split())))
s = 0
h = 1
for i in range(len(a) - 1):
	if a[i] >= h:
		h += 1
	s += a[i] - 1
s += h - 1 if h <= a[len(a) - 1] else a[len(a) - 1] - 1
print(s)


(n, m) = input().split()
a = sorted(map(int, input().split()))
tot = 0
last_fill = 0
reqd = 0
for x in a:
	tot += x
	if x > last_fill:
		last_fill += 1
		reqd += 1
	else:
		reqd += 1
reqd += a[-1] - last_fill
print(tot - reqd)


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
num_1 = 0
j = 1
A.sort()
for i in range(n - 1):
	num_1 += 1
	if j + 1 <= A[i + 1]:
		j += 1
num_1 += A[-1] - j + 1
print(sum(A) - num_1)


import sys

def solve(io):
	N = io.read_int()
	M = io.read_int()
	A = io.read_int_array(N)
	A = list(sorted(A))
	H = max(A)
	S = sum(A)
	want = 1
	rem = 0
	for v in A:
		if want <= v:
			want += 1
	keep = H + N - (want - 1)
	rem = S - keep
	io.println(rem)

class IO:
	in_stream = None
	out_stream = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, input_stream, output_stream):
		self.in_stream = input_stream
		self.out_stream = output_stream

	def read_to_buffer(self):
		self.raw = self.in_stream.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def read_string(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def read_int(self):
		return int(self.read_string())

	def read_float(self):
		return float(self.read_string())

	def read_string_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_string())
		return arr

	def read_int_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_int())
		return arr

	def read_float_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_float())
		return arr

	def read_line(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		if self.pos > 0:
			raise ValueError('Cannot call read_line in the middle of a line.')
		return self.raw

	def print(self, s):
		self.out_stream.write(str(s))

	def println(self, s):
		self.print(s)
		self.print('\n')

	def println_array(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flush_output(self):
		self.out_stream.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flush_output()


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
sum = 0
for i in range(n):
	sum += a[i]
a.sort()
a.reverse()
now = a[0]
cnt = 1
for i in range(1, n):
	while now > a[i] + 1:
		cnt = cnt + 1
		now = now - 1
		if now == 1:
			break
	if now == 1:
		cnt = cnt + n - i
		break
	now = now - 1
	cnt = cnt + 1
while now > 1:
	now = now - 1
	cnt = cnt + 1
print(sum - cnt)


from math import inf as inf
from math import *
from collections import *
import sys
input = sys.stdin.readline
t = 1
while t:
	t -= 1
	(n, h) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	re = 1
	s = 0
	for i in range(n):
		if i == n - 1:
			if re > a[i]:
				s += a[i] - 1
			else:
				s += a[i] - (a[i] - re + 1)
			break
		if re <= a[i]:
			re += 1
		s += a[i] - 1
	print(s)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	count = 0
	a.sort()
	total = sum(a)
	ma = a[n - 1]
	y = 0
	count = 0
	for i in range(n):
		count += 1
		if a[i] > y:
			y += 1
	print(total - (ma - y) - count)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
(summ, minn) = (0, 0)
for i in range(n):
	summ += arr[i] - 1
	minn = min(minn + 1, arr[i])
print(summ - arr[-1] + minn)


[n, m] = [int(x) for x in input().split(' ')]
A = [int(x) for x in input().split(' ')]
A.sort()
maxRemove = 0
maxHeight = 0
for i in range(n):
	if i == n - 1:
		if A[i] > maxHeight:
			maxRemove += maxHeight
		else:
			maxRemove += A[i] - 1
	elif i == 0:
		maxHeight = 1
		maxRemove += A[i] - 1
	else:
		maxRemove += A[i] - 1
		if A[i] > maxHeight:
			maxHeight += 1
print(maxRemove)


from collections import Counter as C
(n, m) = map(int, input().split())
l = [*map(int, input().split())]
c = sorted(C(l).items())
res = 0
j = 0
for (hi, ni) in c:
	h = min(hi - j, ni) + j
	res += (hi - 1) * ni
	if h > j:
		j = h
m = max(l)
if j < m:
	res -= m - j
print(res)


(n, m) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
sofar = 0
for i in range(n):
	if A[i] > sofar:
		sofar += 1
print(sum(A) - n - (A[-1] - sofar))


from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def main():
	(n, m) = mi()
	a = li()
	a.sort()
	ans = 0
	s = 0
	for i in range(n - 1):
		if a[i] > s:
			ans += a[i] - 1
			s += 1
		else:
			ans += a[i] - 1
	if a[n - 1] > s:
		x = a[n - 1] - s
		ans += a[n - 1] - x
	else:
		ans += a[n - 1] - 1
	print(ans)
main()


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
c = max(l)
low = 0
ans = 0
for i in range(n - 1):
	if l[i] > low:
		ans += l[i] - 1
		low += 1
	else:
		ans += l[i] - 1
if low == c:
	ans += low - 1
else:
	ans += low
print(ans)


sum = 0
(n, m) = map(int, input().split())
ara = list(map(int, input().split()))
ara.sort()
for i in range(n):
	sum += ara[i]
cnt = 0
p = 0
for i in range(n):
	cnt += 1
	if ara[i] > p:
		p += 1
cnt += ara[n - 1] - p
print(sum - cnt)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
c = 0
d = 0
s = sum(a)
for i in range(n):
	c += 1
	if a[i] > d:
		d += 1
c += a[n - 1] - d
print(s - c)


(n, m) = list(map(int, input().rstrip().split()))
data = list(map(int, input().rstrip().split()))

def mergesort(l):
	if len(l) == 1:
		return l
	llower = []
	lupper = []
	for i in range(0, int(len(l) / 2)):
		llower.append(l[i])
	for i in range(int(len(l) / 2), len(l)):
		lupper.append(l[i])
	slower = mergesort(llower)
	supper = mergesort(lupper)
	b = 0
	u = 0
	sort = []
	while b < len(slower) and u < len(supper):
		if slower[b] <= supper[u]:
			sort.append(slower[b])
			b = b + 1
		elif supper[u] < slower[b]:
			sort.append(supper[u])
			u = u + 1
	if b == len(slower):
		for i in range(u, len(supper)):
			sort.append(supper[i])
	elif u == len(supper):
		for i in range(b, len(slower)):
			sort.append(slower[i])
	return sort
sort = mergesort(data)
height = sort[n - 1]
total = 0
for i in range(0, n - 1):
	if sort[n - i - 1] == sort[n - i - 2]:
		height += -1
		total = total + 1
	elif height > sort[n - i - 2]:
		total = total + height - sort[n - i - 2]
		height = sort[n - i - 2]
	else:
		height = height - 1
		total = total + 1
total = total + max(1, height)
print(sum(data) - total)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
(l, p, k) = (a[n - 1], 0, 0)
for i in range(n - 1):
	if p < l:
		if a[i] > p:
			k += a[i] - 1
			p += 1
		else:
			k += a[i] - 1
	else:
		k += a[i] - 1
if p >= l:
	print(k + (a[n - 1] - 1))
else:
	print(k + (a[n - 1] - (l - p)))


class IO:

	def get(reader=str):
		return reader(input().strip())

	def gets(reader=str, delim=None):
		return [reader(x) for x in input().strip().split(delim)]

	def tostr(raw, writer=str, delim=' '):
		return delim.join((writer(x) for x in raw))
(n, m) = IO.gets(int)
a = IO.gets(int)
a.sort()
pos = 0
for roof in a:
	pos = min(pos + 1, roof)
need = len(a) + a[-1] - pos
ans = sum(a) - need
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
t = 1
ans = 0
for i in range(n - 1):
	if l[i] >= t and t < l[n - 1]:
		t += 1
	ans += l[i] - 1
ans += l[n - 1] - (l[n - 1] - t + 1)
print(ans)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
s = sum(A)
if n == 1:
	print(0)
else:
	ans = 0
	A.sort(reverse=True)
	for i in range(n - 1):
		A[i] = min(A[i], m)
		if A[i] > A[i + 1]:
			ans += A[i] - A[i + 1]
			m = min(m, A[i + 1])
		else:
			ans += 1
			m = max(1, min(m, A[i] - 1))
	ans += m
	print(s - ans)


def main():
	(n, m) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	s = 0
	for i in range(len(a)):
		s += a[i]
	for i in range(n - 2, -1, -1):
		if a[i + 1] > a[i]:
			s -= a[i + 1] - a[i]
		else:
			s -= 1
			a[i] = max(a[i + 1] - 1, 1)
	print(s - a[0])
main()


(n, h) = map(int, input().split())
lst = list(map(int, input().split()))
lst.sort(reverse=True)
ans = 0
for i in range(1, n):
	if lst[i - 1] == 1:
		ans += lst[i] - 1
		lst[i] = 1
	elif lst[i - 1] <= lst[i]:
		ans += lst[i] - lst[i - 1] + 1
		lst[i] = lst[i - 1] - 1
for i in range(n - 1):
	if lst[i] != lst[i + 1]:
		ans += lst[i + 1]
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
X = 0
prev = 0
for i in range(1, n + 1):
	X += 1
	if a[i - 1] > prev:
		prev += 1
X += a[n - 1] - prev
print(sum(a) - X)


(n, m) = map(int, input().split())
mas = list(map(int, input().split()))
mas.sort()
(res, a) = (0, mas[n - 1])
for i in range(n - 2, -1, -1):
	(q, w) = (mas[i + 1], mas[i])
	if q == w:
		res += q - 1
		a -= 1
	elif a - 1 <= w:
		res += q - 1
		a -= 1
	else:
		res += q - (a - w)
		a = w
res += min(mas[0] - 1, mas[0] - a)
print(res)


(n, m) = list(map(int, input().split()))
in_list = list(map(int, input().split()))
in_list.sort()
prev = 0
min_block = 0
for i in range(n):
	min_block = min_block + in_list[i] - prev
	if min_block <= i:
		min_block = min_block + 1
	prev = in_list[i]
print(sum(in_list) - min_block)


(a, b) = map(int, input().split())
l = list(map(int, input().split()))
if a == 1:
	print(0)
else:
	ll = []
	l.sort()
	(s, p) = (0, 0)
	for i in range(len(l)):
		s += 1
		if l[i] > p:
			p += 1
	s += max(l) - p
	print(sum(l) - s)


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
(w, h) = viread()
(ph, h) = (-1, -1)
(keep, ttl) = (0, 0)
for s in sorted(viread() + [0], reverse=True):
	ttl += s
	if h == -1:
		h = s
	elif h == 0:
		keep += 1
	elif s == ph:
		h -= 1
		keep += 1
	else:
		keep += h - min(h - 1, s)
		h = min(h - 1, s)
	ph = s
print(ttl - keep)


def exponat(lst):
	(result, count) = (0, 1)
	for i in range(len(lst)):
		if count > lst[len(lst) - 1] or count > lst[i]:
			result += lst[i] - 1
			continue
		if i != len(lst) - 1:
			result += lst[i] - 1
			count += 1
		else:
			result += count - 1
	return result
(n, m) = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
print(exponat(sorted(a)))


(n, m) = input().split()
a = [int(i) for i in input().split()]
a.sort()
c = 0
ans = cc = 0
for i in range(int(n)):
	if cc > 1:
		ans += cc - 1
		cc += -1 + a[i] - c
		ans += a[i] - cc
	else:
		cc = max(1, a[i] - c)
		ans += a[i] - cc
	c = a[i]
print(ans)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
c = sum(a)
if n == 1:
	print(0)
	exit(0)
a.sort()
res = 0
pocl = a[n - 1]
f = False
for i in range(n - 2, -1, -1):
	if pocl > 1:
		if a[i] >= pocl:
			res += 1
			pocl -= 1
			res += a[i] - 1
		else:
			f = True
			pocl = a[i]
			res += 1
			res += a[i] - 1
			pocl -= 1
	elif pocl == 1:
		if f:
			res += 1
		res += a[i] - 1
		pocl -= 1
	else:
		res += a[i] - 1
print(res)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
(lol, sum_) = (0, 0)
for i in range(n):
	if i < n - 1:
		sum_ += 1
		lol = min(lol + 1, arr[i])
	else:
		sum_ += max(arr[i] - lol, 1)
print(sum(arr) - sum_)


from sys import stdin

def myfunction(arr, n):
	my_sum = 0
	my_min = 0
	if n == 1:
		return 0
	for i in range(n - 1):
		if arr[i] > my_min:
			my_min += 1
		my_sum += arr[i] - 1
	if arr[-1] == my_min:
		my_sum += arr[i] - 1
	else:
		my_sum += my_min
	return my_sum
(n, m) = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
arr.sort()
print(myfunction(arr, n))


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
sum = 0
A.sort()
for i in A:
	sum += i
choosen = 0
for i in range(n):
	if A[i] > choosen:
		choosen += 1
not_choosen = A[-1] - choosen
print(sum - not_choosen - n)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
worst = 0
maxi = a[0]
a.append(0)
for i in range(n + 1):
	bad = maxi - a[i] - i
	worst = max(worst, bad)
print(sum(a) - n - worst)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
m = max(a)
c = 0
lv = 0
for i in a:
	c += 1
	if i > lv:
		lv += 1
c += m - lv
print(sum(a) - c)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N, M) = getIntList()
za = getIntList()
za.sort(reverse=True)
re = 0
for i in range(N - 1):
	a = za[i]
	b = za[i + 1]
	g = b
	if g >= a:
		t = a - 1
		if t < 1:
			t = 1
		re += g - t
		za[i + 1] = t
		re += a - 1
	else:
		re += g
print(re)


(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
p = sum(a)
i = n - 1
while i > 0:
	a[i - 1] = min(a[i] - 1, a[i - 1])
	p -= a[i] - a[i - 1]
	i -= 1
print(p - max(a[0], 1))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
a.sort(reverse=True)
all_s = sum(a)
s = 0
for i in range(1, len(a)):
	if a[i - 1] > a[i]:
		s += a[i - 1] - a[i]
	elif a[i - 1] == a[i]:
		s += 1
		a[i] -= 1
	else:
		s += 1
		a[i] = a[i - 1] - 1
print(all_s - s)


nos = list(map(int, input().split()))
n = nos[0]
data = list(map(int, input().split()))
suma = 0
for a in range(0, n):
	suma += data[a]
data.sort()
data.reverse()
ans = 0
data.append(0)
for i in range(0, n):
	if data[i] - data[i + 1] > 0:
		ans += data[i] - data[i + 1]
	else:
		ans += 1
		data[i + 1] = data[i] - 1
		if data[i + 1] is 0:
			data[i + 1] = 1
print(suma - ans)


R = lambda : map(int, input().split())
(n, m) = R()
i = 0
a = sorted(R())
for x in a:
	i = min(x, i + 1)
print(sum(a) - n - a[-1] + i)


def main():
	inl = input().strip().split()
	(n, m) = (int(inl[0]), int(inl[1]))
	stacks = []
	total_blocks = 0
	for (i, h) in enumerate(input().strip().split()):
		stacks.append((i, int(h)))
		total_blocks += int(h)
	stacks.sort(key=lambda s: s[1], reverse=True)
	blocks = 0
	hmax = stacks[0][1]
	for i in range(n - 1):
		c = stacks[i]
		if c[1] == 0:
			continue
		forced = min(c[1], hmax) - stacks[i + 1][1]
		if forced > 0:
			blocks += forced
			hmax = stacks[i + 1][1]
		else:
			blocks += 1
			hmax -= 1
	if stacks[n - 1][1] > 0:
		if hmax > 0:
			blocks += hmax
		else:
			blocks += 1
	print(total_blocks - blocks)
main()


(n, m) = map(int, input().split())
arr = [int(x) for x in input().split()]
arr = sorted(arr, reverse=True)
arr.append(0)
isum = sum(arr)
ans = []
top = arr[0]
for i in range(n):
	if arr[i] == 1:
		ans.append(1)
		arr[i + 1] = 1
		continue
	if arr[i + 1] > arr[i]:
		arr[i + 1] = arr[i]
	if arr[i] - arr[i + 1] == 0:
		ans.append(1)
		h = 1
	else:
		ans.append(arr[i] - arr[i + 1])
		h = arr[i] - arr[i + 1]
	top = arr[i] - h
	arr[i + 1] = top
print(isum - sum(ans))


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
a.sort()
count = 0
prev = 0
for i in range(n):
	count += 1
	if a[i] > prev:
		prev += 1
count += a[n - 1] - prev
print(sum(a) - count)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
ans = 0
height = 0
for i in range(n):
	if arr[i] > height:
		ans += 1
		height += 1
	else:
		ans += 1
if arr[-1] > height:
	ans += arr[-1] - height
print(sum(arr) - ans)


import sys
(n, m) = list(map(int, input().split()))
h = list(map(int, input().split()))
s = sum(h)
h.sort()
for i in range(n - 1, 0, -1):
	if h[i] == 1:
		h[i - 1] = 1
	elif h[i - 1] >= h[i]:
		h[i - 1] = h[i] - 1
ans = 0
for i in range(n):
	if i == 0:
		ans += h[i]
	elif h[i] == 1:
		ans += 1
	else:
		ans += h[i] - h[i - 1]
if n == 1:
	print(0)
else:
	print(s - ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
z = max(l)
ans = 0
su = sum(l)
l.sort()
h = 0
for i in l:
	ans = ans + i - 1
	h = min(h + 1, i)
if h < z:
	ans = ans - (z - h)
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
a.sort()
x = 0
b = 0
for i in range(1, n):
	if a[i] != b:
		b += 1
	x += 1
x += a[-1] - b
if a[-1] - b == 0:
	x += 1
print(sum(a) - x)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
used = a[0]
f = a[0] - 1
for i in range(1, n):
	if f > 0:
		f = min(f - 1, a[i] - 1)
	else:
		used += 1
print(sum(a) - used)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cur = 0
for i in range(n):
	cur = min(cur + 1, a[i])
cur = a[-1] - cur
ans = sum(a) - n - cur
print(ans)


(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))[::-1]
s = sum(a)
for i in range(1, n):
	a[i] = min(a[i], a[i - 1] - 1)
	s -= a[i - 1] - a[i]
print(s - max(a[-1], 1))


import math as ma
import sys
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

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

def num():
	return map(int, input().split())

def nu():
	return int(input())
(n, m) = num()
a = li()
ind = -1
z = 0
ll = sorted(a)
mx = ll[n - 1] - 1
cc = 0
for i in range(n - 2, -1, -1):
	if ll[i] == 0:
		continue
	if mx == 0:
		cc += ll[i] - 1
		continue
	if ll[i] >= mx:
		cc += 1
		mx -= 1
		cc += ll[i] - 1
		ll[i] = 1
	else:
		mx = ll[i]
		cc += 1
		mx -= 1
		cc += ll[i] - 1
		ll[i] = 1
print(cc)


import sys
input_file = sys.stdin
[n, m] = list((int(x) for x in input_file.readline().split()))
stacks = list((int(x) for x in input_file.readline().split()))
stacks.sort()
ans = 0
cur_stack = 0
cur_h = 0
while cur_stack < n:
	ans += 1
	if stacks[cur_stack] >= cur_h + 1:
		cur_h += 1
	cur_stack += 1
ans += stacks[-1] - cur_h
print(sum(stacks) - ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
res = 0
same = 0
p = 1
for h in a:
	if p <= h:
		p += 1
	else:
		same += 1
res = a[-1] + same
print(sum(a) - res)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
s = sum(arr)
arr.sort()
req = 0
prev = 0
for i in range(n):
	req += 1
	if arr[i] > prev:
		prev += 1
req += arr[n - 1] - prev
print(s - req)


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
h = a[-1]
height = 0
count = 0
for i in range(n - 1):
	if a[i] > height:
		height = height + 1
	count = count + 1
if height < h:
	print(sum(a) - (count + h - height))
else:
	print(sum(a) - (count + 1))


def func(a, n1):
	maxi = 0
	maxisum1 = 0
	if n1 == 1:
		return 0
	for i in range(n1 - 1):
		if a[i] > maxi:
			maxi += 1
		maxisum1 += a[i] - 1
	if a[n - 1] == maxi:
		maxisum1 += a[i] - 1
	else:
		maxisum1 += maxi
	return maxisum1
(n, m) = list(map(int, input().split()))
a = list(map(int, input().rstrip().split()))
a.sort()
print(func(a, n))


input()
A = sorted(list(map(int, input().split())))
keep = max(A)
up_to = 0
for a in A:
	if a > up_to:
		up_to += 1
	else:
		keep += 1
print(sum(A) - keep)


(n, _) = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)

def solve():
	if n == 1:
		return 0
	ret = a[0] - max(a[0] - a[1], 1)
	for i in range(1, n):
		fromTop = max(0, min(a[i] - a[i - 1] + 1, a[i] - 1))
		a[i] -= fromTop
		fromBottom = a[i] - max(1, a[i] - a[i + 1]) if i < n - 1 else 0
		ret += fromTop + fromBottom
	return ret
print(solve())


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
sum1 = sum(a)
delete = n
possible_height = 0
max_height = a[n - 1]
for i in range(n):
	if a[i] > possible_height:
		possible_height += 1
print(sum1 - delete + possible_height - max_height)


(n, m) = map(int, input().split())
a = [int(o) for o in input().split()]
a = sorted(a)
b = []
u = 0
s = sum(a)
l = 0
k = 0
d = 0
for i in range(n):
	l = a[i]
	d += a[i] - u
	if d <= i:
		d += 1
	u = a[i]
print(s - d)


from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10000000)

class Operation:

	def __init__(self, name, function, function_on_equal, neutral_value=0):
		self.name = name
		self.f = function
		self.f_on_equal = function_on_equal

def add_multiple(x, count):
	return x * count

def min_multiple(x, count):
	return x

def max_multiple(x, count):
	return x
sum_operation = Operation('sum', sum, add_multiple, 0)
min_operation = Operation('min', min, min_multiple, 1000000000.0)
max_operation = Operation('max', max, max_multiple, -1000000000.0)

class SegmentTree:

	def __init__(self, array, operations=[sum_operation, min_operation, max_operation]):
		self.array = array
		if type(operations) != list:
			raise TypeError('operations must be a list')
		self.operations = {}
		for op in operations:
			self.operations[op.name] = op
		self.root = SegmentTreeNode(0, len(array) - 1, self)

	def query(self, start, end, operation_name):
		if self.operations.get(operation_name) == None:
			raise Exception('This operation is not available')
		return self.root._query(start, end, self.operations[operation_name])

	def summary(self):
		return self.root.values

	def update(self, position, value):
		self.root._update(position, value)

	def update_range(self, start, end, value):
		self.root._update_range(start, end, value)

	def __repr__(self):
		return self.root.__repr__()

class SegmentTreeNode:

	def __init__(self, start, end, segment_tree):
		self.range = (start, end)
		self.parent_tree = segment_tree
		self.range_value = None
		self.values = {}
		self.left = None
		self.right = None
		if start == end:
			self._sync()
			return
		self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree)
		self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree)
		self._sync()

	def _query(self, start, end, operation):
		if end < self.range[0] or start > self.range[1]:
			return None
		if start <= self.range[0] and self.range[1] <= end:
			return self.values[operation.name]
		self._push()
		left_res = self.left._query(start, end, operation) if self.left else None
		right_res = self.right._query(start, end, operation) if self.right else None
		if left_res is None:
			return right_res
		if right_res is None:
			return left_res
		return operation.f([left_res, right_res])

	def _update(self, position, value):
		if position < self.range[0] or position > self.range[1]:
			return
		if position == self.range[0] and self.range[1] == position:
			self.parent_tree.array[position] = value
			self._sync()
			return
		self._push()
		self.left._update(position, value)
		self.right._update(position, value)
		self._sync()

	def _update_range(self, start, end, value):
		if end < self.range[0] or start > self.range[1]:
			return
		if start <= self.range[0] and self.range[1] <= end:
			self.range_value = value
			self._sync()
			return
		self._push()
		self.left._update_range(start, end, value)
		self.right._update_range(start, end, value)
		self._sync()

	def _sync(self):
		if self.range[0] == self.range[1]:
			for op in self.parent_tree.operations.values():
				current_value = self.parent_tree.array[self.range[0]]
				if self.range_value is not None:
					current_value = self.range_value
				self.values[op.name] = op.f([current_value])
		else:
			for op in self.parent_tree.operations.values():
				result = op.f([self.left.values[op.name], self.right.values[op.name]])
				if self.range_value is not None:
					bound_length = self.range[1] - self.range[0] + 1
					result = op.f_on_equal(self.range_value, bound_length)
				self.values[op.name] = result

	def _push(self):
		if self.range_value is None:
			return
		if self.left:
			self.left.range_value = self.range_value
			self.right.range_value = self.range_value
			self.left._sync()
			self.right._sync()
			self.range_value = None

	def __repr__(self):
		ans = '({}, {}): {}\n'.format(self.range[0], self.range[1], self.values)
		if self.left:
			ans += self.left.__repr__()
		if self.right:
			ans += self.right.__repr__()
		return ans

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result
TestCases = False
testingMode = False
optimiseForReccursion = False

def main():
	(n, m) = get_tuple()
	li = get_list()
	res = 0
	curr_height = 1
	li.sort()
	for (i, ele) in enumerate(li):
		if i == len(li) - 1:
			res += curr_height - 1 if curr_height < li[-1] else ele - 1
		else:
			if ele >= curr_height and curr_height < li[-1]:
				curr_height += 1
			res += ele - 1
	print(res)
if TestCases:
	for _ in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


import sys
from collections import deque
import heapq
input = sys.stdin.readline
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
ans = 0
maxi = 0
for i in range(n):
	if arr[i] == 1:
		maxi = 1
	elif arr[i] > maxi:
		maxi += 1
	ans += 1
ans += arr[-1] - maxi
print(sum(arr) - ans)


(n, m) = [int(x) for x in input().split()]
hei = [int(x) for x in input().split()]
total_blocks = 0
for x in hei:
	total_blocks += x
hei.sort(reverse=True)
if len(hei) == 1:
	print(0)
	exit()
used_blocks = 0
max_covered_hei = -1
for i in range(len(hei)):
	if max_covered_hei == -1:
		max_covered_hei = hei[i]
		used_blocks = 1
	else:
		diff_to_cur = max(0, max_covered_hei - hei[i])
		if diff_to_cur == 0:
			used_blocks += 1
			max_covered_hei -= 1
			max_covered_hei = max(0, max_covered_hei)
		else:
			used_blocks += diff_to_cur
			max_covered_hei = hei[i]
if max_covered_hei > 1:
	used_blocks += max_covered_hei - 1
print(total_blocks - used_blocks)


(n, m) = map(int, input().split())
num = [int(i) for i in input().split()]
num.sort()
ans = n + num[-1]
c = 0
for i in num:
	if c < i:
		c += 1
print(sum(num) - ans + c)


(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
ma = a[n - 1]
yr = 1
g = 1
import sys
if n == 1:
	print(0)
	sys.exit()
for i in range(1, n):
	if a[i] > yr:
		yr += 1
	g += 1
g += ma - yr
print(sum(a) - g)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a = a[::-1]
sum = int(0)
mx = int(0)
for i in range(n):
	sum += a[i]
	mx = max(mx, a[i])
kol = int(0)
for i in range(n):
	if i != n - 1:
		a[i + 1] = min(a[i + 1], a[i])
		if a[i] == a[i + 1] and a[i] != 1:
			mx -= 1
			kol += 1
		a[i + 1] = min(a[i + 1], mx)
		if a[i] == 1:
			kol += 1
		elif mx > 1 and mx > a[i + 1]:
			kol += mx - a[i + 1]
			mx -= mx - a[i + 1]
	else:
		kol += a[i]
ans = sum - kol
print(ans)


(a, b) = map(int, input().split())
list1 = [*map(int, input().split())]
list1.sort(reverse=True)
ans = 0
neededHeight = list1[0]
for (i, j) in enumerate(list1[1:]):
	neededHeight = max(min(j, list1[i] - 1), 1)
	ans += j - neededHeight
	if list1[i] > 1:
		ans += neededHeight
	list1[i + 1] = neededHeight
print(ans)


(N, _) = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
keep = max(A)
up_to = 0
for i in range(N):
	if A[i] > up_to:
		up_to += 1
	else:
		keep += 1
print(sum(A) - keep)


import math
from typing import Dict, List, Tuple

def input_lst() -> List[int]:
	return [int(x) for x in input().split()]

def print_out(res: List[int]):
	print(' '.join([str(x) for x in res]))

def get_primes(n: int) -> List[int]:
	max_n = n
	rng = list(range(max_n + 1))
	rng[0] = 1
	rng[1] = 1
	primes = []
	for i in range(2, max_n + 1):
		if rng[i] > 1:
			primes.append(i)
			for j in range(i + i, max_n + 1, i):
				rng[j] = 0
	return primes

def main():
	(n, m) = (int(x) for x in input().split())
	a = input_lst()
	block_count = sum(a)
	m = max(a)
	min_block_count = n
	sorted_a = sorted(a)[::-1]
	cur_block_height = m
	for el in sorted_a:
		if el < cur_block_height:
			min_block_count += cur_block_height - el
			cur_block_height = el
		if cur_block_height > 0:
			cur_block_height -= 1
	block_to_delete = block_count - (min_block_count + cur_block_height)
	print(block_to_delete)
main()


(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
total = 0
req = 0
ans = 0
prev = 0
for i in a:
	total += i
for i in a:
	req += 1
	if i > prev:
		prev += 1
req += a[-1] - prev
ans = total - req
print(ans)


s = input()
s = s.split()
for j in range(len(s)):
	s[j] = int(s[j])
n = s[0]
m = s[1]
s1 = input()
s1 = s1.split()
for j in range(n):
	s1[j] = int(s1[j])
s1.sort()
s1.reverse()
pointer = s1[0]
res = 0
flag = 0
if n == 1:
	print(0)
else:
	for i in range(n - 1):
		if s1[i] != 1:
			if flag == 1:
				res = res + s1[i] - 1
			elif s1[i] == s1[i + 1]:
				pointer = pointer - 1
				if pointer == 0:
					flag = 1
					pointer = s1[i + 1]
				res = res + s1[i] - 1
			elif pointer - s1[i + 1] > 1:
				res = res + (s1[i] - pointer) + s1[i + 1]
				pointer = s1[i + 1]
			else:
				pointer = pointer - 1
				res = res + s1[i] - 1
	if flag == 0:
		if s1[n - 1] != 1:
			res = res + s1[n - 1] - pointer
	if flag == 1:
		res = res + s1[n - 1] - 1
	print(res)


from collections import defaultdict
(n, m) = map(int, input().split())
b = defaultdict(int)
s = 0
for i in input().split():
	i = int(i)
	b[i] += 1
	s += i
a = sorted(b.items(), reverse=True)
p = 0
q = 0
d = len(a)
a.append((0, 0))
for i in range(d):
	l = a[i][0] - a[i + 1][0]
	x = a[i][1] + p
	if x >= l:
		p = x - l
	else:
		q = l - x
		p = 0
y = a[0][0]
print(s - (y + p))


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
b.sort()
tot = 1
h = 1
for i in range(n - 1):
	if b[i + 1] > h:
		tot += 1
		h += 1
	else:
		tot += 1
print(sum(b) - (tot + b[-1] - h))


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
sa = sum(l)
t = 0
for i in range(len(l) - 1):
	if l[i] != 1:
		if l[i] > l[i + 1]:
			t += l[i] - l[i + 1]
		elif l[i] == l[i + 1]:
			l[i + 1] -= 1
			t += 1
		else:
			l[i + 1] = l[i] - 1
			t += 1
	else:
		t += len(l) - 1 - i
		l[-1] = 1
		break
t += l[-1]
print(sa - t)


(n, m) = map(int, input().split())
inp = list(map(int, input().split()))
inp.sort()
pos = 1
for i in inp[0:n - 1]:
	if pos == inp[n - 1]:
		break
	elif i >= pos:
		pos += 1
print(sum(inp) - (n - 1) - (inp[n - 1] - pos + 1))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
ans = 0
cnt = 0
hash = defaultdict(int)
flag = 1
for i in range(1, n):
	ans += l[i] - 1
	hash[l[i]] += 1
	if hash[l[i]] < l[i] and flag and (cnt < l[0]):
		cnt += 1
	if hash[l[i]] == l[i] and flag and (cnt < l[0]):
		flag = 0
		cnt += 1
if cnt == l[0]:
	cnt -= 1
print(ans + cnt)