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
#include<stdio.h>
#include<iostream>
using namespace std; 


const long MAX_POSSIBLE_RECEIVED_POWER = 201718L;
long counters[MAX_POSSIBLE_RECEIVED_POWER + 1] {0};

long getCounter(long power){
	if(power<=MAX_POSSIBLE_RECEIVED_POWER){
		return counters[power];
	}else{
		return 0;
	}
}

int main()
{
	cin.sync_with_stdio(false);
	long n; 
	cin >> n;
	
	long maxReceivedPower = 0;
	for(int i = 0; i<n; ++i){
		long power;
		cin >> power; 
		counters[power]++;
		if(power > maxReceivedPower){
			maxReceivedPower = power;
		}
	}
	
	long currentPower = 0;
	long buffer = 0;
	long maxPowerWithLeftover;
	do{
		long totalCounterCurrentPower = buffer + getCounter(currentPower);
		if(totalCounterCurrentPower == 1){
			maxPowerWithLeftover = currentPower;
		}
		currentPower++;
		buffer = totalCounterCurrentPower / 2;
		
	}while(buffer>0 || currentPower<=maxReceivedPower);

	cout << maxPowerWithLeftover << "\n";
	
	return 0;
}