#include <bits/stdc++.h>
using namespace std;
int n, a[100005], top, s[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  top = -1;
  for (int i = n - 1; i >= 0; --i) {
    s[++top] = a[i];
    while (top > 0 && s[top] == s[top - 1]) ++s[--top];
  }
  cout << s[0] - top;
  return 0;
}
