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
#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int tab [300000];
ll a, p, W, n, maks = -1, pom, wsk;

int main()
{
    scanf("%lld", &n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%lld", &a);
        ++tab[a];
        if(a > maks) maks = a;
    }
    for(int i = 0; i <= maks; ++i)
    {
        p = tab[i] / 2;
        tab[i+1] += p;
        if(tab[i] > 0) W = i;
    }
    if(tab[maks+1] > 0)
    {
        pom = tab[maks+1];
        wsk = maks + 1;
        while(pom > 1)
        {
            ++wsk;
            pom /= 2;
        }
    W = wsk;
    }

    printf("%lld\n", W);

return 0;
}