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
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int biggestPower(int n) {
    int i = 0;
    int power = 1;
    while (power <= n) {
        i++;
        power *= 2;
    }
    i--;
    return i;
}

int main(int argc, char** argv) {

    long input[220000];
    long n;
    long currentInput;
    long power;
    
    cin >> n;
    
    for (int i=0; i<220000; i++) {
        input[i] = 0;
    }
    
    for (int i=0; i<n; i++) {
        cin >> currentInput;
        input[currentInput] = input[currentInput] + 1;
    }   

    for (int i=0; i<220000; i++) {
        if (input[i] > 1) {
            power = biggestPower(input[i]);
            input[i] = input[i] - ((long) pow(2, power));
            input[i + power]++;
        }
    }
    
    for (int i=219999; i>=0; i--) {
        if (input[i] > 0) {
            cout << i << endl;
            break;
        }
    }
    
    return 0;
}