#include <bits/stdc++.h>
using namespace std;
const int M = 100000 + 10;
long long int a[M];
priority_queue<int> pq;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    pq.push(-a[i]);
  }
  int cnt = 0;
  while (pq.size() >= 2) {
    int v1 = -pq.top();
    pq.pop();
    int v2 = -pq.top();
    pq.pop();
    if (v1 == v2) {
      pq.push(-v1 - 1);
    } else {
      cnt++;
      pq.push(-v2);
    }
  }
  int v = -pq.top();
  int ans = v - cnt;
  cout << ans << endl;
  return 0;
}
