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<bits/stdc++.h>

using namespace std;

const int I = 201718;

bool tab[I + 5];

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

    int b, c, m = 0;
    for(int x = 0; x < a; x++)
    {
        cin >> b;

        if(b > m)
        m = b;

        c = b;
        if(!tab[b])
            tab[b] = 1;
        else
        {

         while(c <= I && tab[c])
               {
                tab[c] = 0;
                c++;
               }

            tab[c] = 1;

            if(c <= I && c > m)
            m = c;
        }
    }

    cout << m;
    return 0;
}