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
#include <stdio.h>
#include <algorithm>

constexpr int MAX = 210000;

void add(int a, char* coins, int& maxCoin)
{
    if (a > maxCoin)
        maxCoin = a;
    coins[a]++;
    while(coins[a] >= 2)
    {
        coins[a] = 0;
        a++;
        coins[a]++;
        if (a > maxCoin)
            maxCoin = a;
    }
}

int main(int, char **)
{
    int n, a, maxCoin = 0;
    char coins[MAX];
    std::fill_n(coins, MAX, 0);
    scanf("%d", &n);
    for (int i=0; i<n; ++i)
    {
        scanf("%d ", &a);
        add(a, coins, maxCoin);
    }
    printf("%d\n", maxCoin);
    return 0;
}