#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int sum = a[0], count = 0, maxi = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] == a[i - 1])
      count++;
    else {
      if (count) {
        if ((count + 1) & 1)
          ;
        else
          sum++;
        int t = a[i] - a[i - 1] - 1;
        count++;
        count /= 2;
        while (t-- && count) {
          if (count % 2 == 0) sum++;
          count /= 2;
        }
        if (t != -1) sum += t + 1;
        maxi = a[i];
      } else {
        sum += a[i] - a[i - 1] - 1;
      }
    }
  }
  if (count) {
    count++;
    if (count % 2 == 0) sum++;
    while (count /= 2) {
      if (count % 2 == 0) sum++;
    }
  }
  cout << sum;
  return 0;
}
