#include <bits/stdc++.h>
int N, p[200000];
int s, ans;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", p + i);
  s = 0;
  ans = p[1];
  for (int i = 1, pos = 0; i <= N;) {
    pos = p[i];
    while (i < N && p[i + 1] == p[i]) i++, s++;
    i++;
    s++;
    if (i > N) break;
    while (s && pos < p[i]) {
      if ((s & 1) == 0) ans++;
      s >>= 1;
      pos++;
    }
    ans += p[i] - pos;
  }
  while (s != 0) {
    if ((s & 1) == 0) ans++;
    s >>= 1;
  }
  printf("%d\n", ans);
}
