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
47
#include <iostream>
#include <vector>

using namespace std;

int solution() {
    int count;
    const int MAX = 1000000;
    vector<int> container(MAX);

    cin >> count;
    for (int i = 0; i < count; i++) {
        int num;
        cin >> num;
        container[num] += 1;
    }

    int lastNonZeroIdx = -1;
    for (int i = 0; i < MAX - 1; i++) {
        int value = container[i];
        if (value > 0) {
            lastNonZeroIdx = i;
            container[i + 1] += value / 2;
        }
    }

    int finalValue = container[MAX - 1];
    if (finalValue == 0) {
        return lastNonZeroIdx;
    } else if (finalValue == 1) {
        return MAX;
    }

    lastNonZeroIdx = MAX - 1;
    while (finalValue > 1) {
        finalValue /= 2;
        lastNonZeroIdx += 1;
    }
    return lastNonZeroIdx;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << solution() << endl;
    return 0;
}