N,M = map(int,input().split()) C = list(map(int,input().split())) C = sorted(C) counter = 0 answer = 0 while counter < M+1 : C[0] = C[0] - 1 counter += 1 if C[0] == 0 and counter < M: C.pop(C[0]) answer += 1 else : C = sorted(C) if len(C) == 0 : break print(answer)