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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <unordered_map>
#include <vector>
#include <utility> 
#include <algorithm>

int main() {

  std::vector<int> input;

  int num;
  scanf("%d\n", &num);
  for (int i=0;i<num;++i) {
  	int a;
  	scanf("%d", &a);
  	input.push_back(a);
  }

  std::unordered_map<int, int> nominal_to_count;
  for (int coin : input) {
  	nominal_to_count[coin]++;
  }

  std::vector<std::pair<int, int>> sorted_nom_to_count;
  for (auto& kv : nominal_to_count) {
  	sorted_nom_to_count.push_back(std::make_pair(kv.first, kv.second));
  }
  std::sort(sorted_nom_to_count.begin(), sorted_nom_to_count.end());

  std::pair<int, int> cache {0,0};
  std::pair<int, int> last;
  for (auto p : sorted_nom_to_count) {
  	int nominal = p.first;
  	int cnt = p.second;
  	if (cache.first != 0) {
  	  while (cache.first < nominal) cache = {cache.first + 1, cache.second / 2};
  	  if (cache.first == nominal) cnt += cache.second;
  	}
  	if (cnt > 1) {
  	  cache = {nominal + 1, cnt / 2};
  	} else {
  	  cache = {0,0};
  	}
  	last = p;
  }
  if (cache.first != 0) {
  	while (cache.second > 1) {
      cache = {cache.first + 1, cache.second / 2};
  	}
  	printf("%d\n", cache.first);
  } else {
  	printf("%d\n", last.first);
  }

  return 0;
}