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

#define MAX	201718

int main()
{
	int n;
	int v;
	int counts[MAX + 1];

	for (int i = 0; i <= MAX; i++)
		counts[i] = 0;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", &v);
		counts[v]++;
	}

	int remaining = 0;
	int max = 0;

	for(int i = 0; i <= MAX; i++)
	{
		remaining += counts[i];
		counts[i] = remaining % 2;
		remaining /= 2;

		if (counts[i] != 0 || remaining != 0)
			max = i;
	}

	while (remaining != 0)
	{
		remaining /= 2;
		max++;
	}

	printf("%d", max);

	return 0;
}