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
#include<cstdio>
#define MAX 201719
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORD(i,n) for(int i=0;i<=n;++i)
#define REP(i,n) FOR(i,0,n)
 
int n, a;
int dane[MAX];
int dokad = -9999;
 
int main() {
    scanf("%d", &n);
    REP(i, MAX)dane[i] = 0;
 
    REP(i, n)
    {
        scanf("%d", &a);
        dane[a]++;
        dokad = a > dokad ? a : dokad;
    }
 
    REP(i, dokad)
    {
        if(dane[i] > 1)
        {
            int mnoznik = dane[i] / 2;
            dane[i+1] += mnoznik;
        }
 
        if(i+1 >= dokad && dane[i+1] > 1)
        {
            dokad++;
        }
    }
 
    int wynik = 0;
 
    FORD(i, dokad)
    {
        wynik = dane[i] != 0 ? i : wynik;
    }
    printf("%d\n", wynik);
}