#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = (int)2e5 + 5;
int p, q;
int n;
int a[MAXN];
set<int> s;
int max1 = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= (n); i++) {
    cin >> a[i];
    if (s.count(a[i])) {
      int temp = a[i];
      while (s.count(temp)) {
        s.erase(temp);
        temp++;
      }
      s.insert(temp);
      if (temp > max1) max1 = temp;
    } else {
      s.insert(a[i]);
      if (a[i] > max1) max1 = a[i];
    }
  }
  printf("%d\n", max1 - s.size() + 1);
  return 0;
}
