N = int(input()) A = list(map(int, input().split())) M = 101010 D = [0] * M for a in A: D[a] += 1 ans = 0 for i in range(M): if D[i] >= 2: ans = i + 1 N -= min(2, D[i]) elif D[i] >= 1 and N - D[i] >= 1: ans = i + 1 N -= min(2, D[i]) D[i + 1] -= 1 else: break print(ans)