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
48
49
50
51
52
53
54
55
56
#include<cstdio>
#define MAX 201750

int data[MAX];

void init();
int getData(int n);

int main() {
    int n;
    scanf("%d", &n);


    init();
    int countOfNumbers = getData(n);

    for(int i = 0; i < countOfNumbers; i++) {
        if(data[i] > 1) {
            int factor = data[i] / 2;
            data[i+1] += factor;
            data[i] -= factor*2;
        }

        if(i+1 >= countOfNumbers && data[i+1] > 1) {
            countOfNumbers++;
        }
    }

    int result = 0;
    for(int i = 0; i <= countOfNumbers; i++) {
        if(data[i] != 0) result = i;
    }

    printf("%d\n", result);
}

void init() {
    for(int i = 0; i < MAX; i++) {
        data[i] = 0;
    }
}

int getData(int n) {

    int number;
    int max = -1;

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

        data[number]++;
        if(number > max) max = number;
    }

    return max;
}