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

const int MAX = 201718;
using namespace std;
int map[MAX + 1];

int main()
{
	ios::sync_with_stdio(false);
	int n, solution, max_pow = 0, min_pow = MAX + 1;
	std::fill(map, map + MAX, 0);
	cin >> n;

	while(n--)
	{
		cin >> solution;
		++map[solution];
		if(solution > max_pow)
		{
			max_pow = solution;
		}
		if(solution < min_pow)
		{
			min_pow = solution;
		}
	}

	solution = min_pow;
	for(int i = min_pow; i < MAX; ++i)
	{
		map[i + 1] += map[i] / 2;

		if (map[i + 1] > 0) 
		{
			solution = i + 1;
		}
		else if(i >= max_pow)
		{
			cout << solution << endl;
			return 0;
		}
	}

	// continue work...
	while (map[MAX] >= 2)
	{
		map[MAX] /= 2;
		++solution;
	}

	cout << solution << endl;
	return 0;
}