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
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

int main() {
  priority_queue<int, std::vector<int>, std::greater<int>> Q;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int coin;
    scanf("%d", &coin);
    Q.push(coin);
  }
  int a = -1, b = -1;
  while (!Q.empty()) {
    if (a == -1) {
      a = Q.top();
      Q.pop();
    } else if (b == -1) {
      b = Q.top();
      Q.pop();
    } else if (a == b) {
      Q.push(++a);
      a = -1;
      b = -1;
    } else {
      a = b;
      b = -1;
    }
  }
  printf("%d\n", a == b ? a + 1 : max(a, b));
  return 0;
}