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