#include <bits/stdc++.h>
using namespace std;
priority_queue<long long> h;
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    h.push(-a);
  }
  int has = 0;
  while (h.size() > 1) {
    int tmp = h.top();
    h.pop();
    if (h.top() == tmp) {
      h.pop();
      h.push(tmp - 1);
    } else
      has++;
  }
  cout << -h.top() - has << endl;
}
