Ivan and Powers of Two
Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers.
In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.In the second sample you need to add numbers 20, 21, 22.
The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.
Print a single integer — the answer to the problem.
n = int(input())
a = list(map(int, input().split()))
b = set()
for i in a:
    while i in b:
        b.remove(i)
        i += 1
    b.add(i)
print(max(b) - len(b) + 1)
from collections import defaultdict

def solve(arr) -> int:
    bit = defaultdict(int)
    for i in arr:
        while bit[i]:
            del bit[i]
            i += 1
        bit[i] = 1
    return max(bit.keys()) - len(bit) + 1

def main():
    n = int(input())
    arr = list(map(int, input().split()))
    print(solve(arr))

def test():
    print(solve([0, 1, 1, 1]))

def init():
    import sys
    import os
    if 'DOCKER_HOST' in os.environ:
        test()
        sys.stdin = open('test.txt')
if __name__ == '__main__':
    init()
    main()
n = int(input())
a = list(map(int, input().split()))
b = set()
for i in a:
    while i in b:
        b.remove(i)
        i += 1
    b.add(i)
print(max(b) - len(b) + 1)