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
#include <iostream>
#include <string>
#include <queue>

using namespace std;
typedef pair<int, int> P;
priority_queue< P, vector<P>, greater<P> > q;

int main() {
    int N;
    cin >> N;

    for (int i = 0; i < N; i++) {
        long long pow;
        cin >> pow;
        q.push(std::make_pair(pow, 1L));
    }

    while (q.size() > 1) {
        std::pair<long long,long long> p1 = q.top();
        q.pop();
        std::pair<long long,long long> p2 = q.top();
        q.pop();
        if (p1.first == p2.first) {
            q.push(std::make_pair(p1.first, p1.second + p2.second));
        } else {
            q.push(p2);
            if (p1.second > 1) {
                q.push(std::make_pair(p1.first+1, p1.second/2));
            }
        }
    }

    std::pair<long long, long long> ans = q.top();
    q.pop();

    while(ans.second > 1){
        ans.first++;
        ans.second /= 2;
    }

    cout << ans.first<< endl;
    return 0;
}