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 or (D[i] >= 1 and N - D[i] >= 1): ans = i + 1 N -= D[i] else: break print(ans)