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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

const int N = 250000;

int count[N];

int main() {
    ios_base::sync_with_stdio(0);

    int n, a, maxi = 0;

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> a;

        maxi = max(maxi, a);
        count[a]++;
    }

    int i = 0;
    int b = 0;
    while (true) {
        if (count[i] == 1) {
            b = max(b, i);

            if (b >= maxi) {
                break;
            }
        }
        else if (count[i] > 1) {
            int upgrade = count[i]/2;
            count[i + 1] += upgrade;
        } else if (b >= maxi) {
            break;
        }

        i++;
    }

    cout << b;

    return 0;
}