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

void toBin(int num, char binary[]) {
    for(int i=0; i<20; ++i) {
        binary[i] = 0;
    }
    int i=0;
    while(num != 0) {
        if(num % 2) {
            binary[i] = 1;
        }
        num >>= 1;
        ++i;
    }
}

int main() {
    constexpr int size = 201738;
    int powOf2[size];
	int maxPow = 0;
	char binary[20]; //LSB, ..., MSB
	for(int i=0; i<size; ++i) {
        powOf2[i] = 0;
    }
    int count;
    int buff;
    scanf("%d", &count);
	for(int i=0; i<count; ++i) {
        scanf("%d", &buff);
		++powOf2[buff];
    }
	for(int i=0; i<size; ++i) {
        if(powOf2[i] > 1) {
            toBin(powOf2[i], binary);
		    powOf2[i] = 0;
		    for(int j=0; j<20; ++j) {
                if(binary[j] != 0) {
                    powOf2[i+j] += 1;
			    }
		    }
		} else {
            if(powOf2[i] == 1) {
                maxPow = i;
			}
		}
    }
	printf("%d\n", maxPow);
    return 0;
}