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
#include<iostream>
#include<cstdio>

typedef unsigned long long int ull;
using namespace std;

int main () {
  ios_base::sync_with_stdio(0);
  const int M = 202013;

  int n; cin >> n;
  int tab[M]; int max_idx = 0;
  for (int i = 0; i < M; i++) tab[i] = 0;
  for (int i = 0; i < n; i++) {
    int a; cin >> a;
    max_idx = max(max_idx, a);
    tab[a] ++;
  }

  int max = 0;
  int j = 0; bool cont = false;
  while (j <= max_idx || cont) {
    if (tab[j] > 1) {
      tab[j+1] += tab[j] / 2;
      cont = true;
      max = j;
    } else if (tab[j] == 1) {
      cont = false;
      max = j;
    } else {
      cont = false;
    }
    j++;
  }
  cout << max << endl;

}