1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

main() {
    int T[201800] = {0};
    int n; cin >> n;
    int maxi = 0;
    for(int i = 0; i < n; i++) {
        int a; cin >> a;
        T[a]++;
        if(T[a] == 2) {
            T[a+1]++;
            T[a] = 0; 
            maxi = max(maxi, a+1); 
        }
        maxi = max(maxi, a);
    }
    for(int i = maxi; i < 201800; i++) {
        if(T[i] > 1) {
            T[i+1] += T[i]/2;
            maxi = i+1;
        } else {
            break;
        }
    }
    cout << maxi;
}