def read(*fns):
return [fn(x) for fn, x in zip(fns, input().split(" "))]
n, k = read(int, int)
tree = read(*([int] * n))
tree.sort()
cut = tree[:k]
cut.reverse()
d = cut[0]
curr = d
for t in cut:
d += max(0, t - curr)
curr += max(0, t - curr)
curr -= 1
print(d)