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
// Michal Stobierski, zad 1B
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;

    vector<int> values;
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        values.push_back(x);
    }
    sort(values.begin(), values.end(), std::greater<int>());

    vector<int> exchanged;
    for (int & x : values) {
        //cerr << "push " << x << endl;
        exchanged.push_back(x);
        
        while (exchanged.size() >= 2 && exchanged.back() == exchanged[exchanged.size()-2]) {
            int actVal = exchanged.back();
            exchanged.pop_back(); exchanged.pop_back();
            exchanged.push_back(actVal+1);
        }
    }
    cout << exchanged.front() << endl;

    return 0;
}