1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int n, tab[500013], a, wyn, lm;
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a);
        tab[a]++;
        lm = max(lm, a);
    }
    for (int i = 0; i < 2*lm; i++)
    {
        tab[i+1] += tab[i]/2;
        tab[i] = tab[i]%2;
        if (tab[i] == 1) wyn = i;
    }
    printf("%d", wyn);
}