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
#include <cstdio>

using namespace std;

const int size = 250000;

short arr[size] = {0};

int main() {
    int n;
    int index;
    int max_index = 0;

    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &index);

        if(max_index < index) {
            max_index = index;
        }

        // binary add
        while (arr[index] != 0) {
            arr[index] = 0;
            index++;
        }
        arr[index] = 1;
    }

    // find max non-zero index
    index = max_index + 20;
    while (arr[index] == 0) {
        index--;
    }

    printf("%d\n", index);

    return 0;
}