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
#include <iostream>
#include <map>
using namespace std;

int main() {
  long n;
  cin >> n;

  map<long, long> portfel;

  long k;
  for(long i = 0; i < n; i++) {
    cin >> k;
//    if(portfel.contains(k))
      portfel[k]++;
//    else
//      portfel[k] = 1;
  }
  
  while(portfel.size() > 1 || portfel.begin()->second > 1) {
    auto it = portfel.begin();
    long nominal = it->first;
    long liczba = it->second;
    portfel.erase(it);
  
    if(liczba > 1)
      portfel[nominal+1] += liczba/2;  
    
//    if(portfel.find(nominal+1))
//      portfel[nominal+1] += liczba/2;
//    else
//      porftel[nominal+1] = liczba/2;
  }
  
  cout << portfel.begin()->first << endl;
  
  return 0;
}