import static java.lang.System.*;
import static java.lang.Math.*;

import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;

public class C305 {
    public static Scanner sc=new Scanner(in);
    //public static Random sc=new Random();

    void run() {
        final int n=sc.nextInt();
        LinkedList<Long> as=new LinkedList<>();
        for(int i=0;i<n;i++)
            as.add(sc.nextLong());

        //calc
        for(int i=0;i<as.size();i++){
            long v=as.get(i);
            int dup=1;
            for(int j=i+1;j<as.size();j++){
                if(as.get(j)!=v)break;
                dup++;
            }
            for(int j=i;j<i+dup;j++){
                as.remove(i);//i times add
            }
            for(int j=0;j<dup/2;j++){
                as.add(v+1);//1/2 times add
            }
            if(dup%2==1)as.add(v);
            Collections.sort(as);
        }

        //get
        long pos=-1;
        int res=0;
        for(int i=0;i<as.size();i++){
            long v=as.get(i);
            res+=v-pos-1;
            pos=v;
        }
        ln(res);
    }

    public static void main(String[] args) {
        new C305().run();
    }

    //output lib
    static final String br=System.getProperty("line.separator");
    static final String[] asep=new String[]{""," ",br,br+br};

    static String str(Object o){
        int depth=_getArrayDepth(o);
        return depth>0?_strArray(o,depth):o.toString();
    }
    @Deprecated
    static int _getArrayDepth(Object o){
        if(!o.getClass().isArray() || Array.getLength(o)==0) return 0;
        return  1+_getArrayDepth(Array.get(o,0));
    }
    @Deprecated
    private static String _strArray(Object o,int depth){
        if(depth==0) return str(o);
        StringBuilder sb=new StringBuilder();
        for(int i=0,len=Array.getLength(o);i<len;i++){
            if(i!=0)sb.append(asep[depth]);
            sb.append(_strArray(Array.get(o,i),depth-1));
        }
        return sb.toString();
    }

    public static void pr(Object obj){
        out.print(str(obj));
    }
    public static void ln(){
        out.println();
    }
    public static void ln(Object... obj){
        for(Object o:obj){
            pr(o);ln();
        }
    }
}


import static java.lang.System.*;
import static java.lang.Math.*;

import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;

public class C305 {
    public static Scanner sc=new Scanner(in);
    //public static Random sc=new Random();

    void run() {
        final int n=sc.nextInt();
        int[] as=new int[n];
        for(int i = 0; i < n; i++) as[i]=sc.nextInt();

        int tcnt=0,tmax=0;
        for(int i=0;i<n;){
            int j =i;
            while (j+1 < n && as[i]==as[j+1]) j++;
            int cnt=j-i+1;
            if (cnt % 2 == 1){// is 1
                tcnt++;
                if (as[i] > tmax) tmax = as[i];
            }
            // carry up
            cnt/=2;
            i += cnt;
            for(int k = j; k>j-cnt;k--) as[k]++;
            i++;
        }
        ln(tmax-tcnt+1);
    }

    public static void main(String[] args) {
        new C305().run();
    }

    //output lib
    static final String br=System.getProperty("line.separator");
    static final String[] asep=new String[]{""," ",br,br+br};

    static String str(Object o){
        int depth=_getArrayDepth(o);
        return depth>0?_strArray(o,depth):o.toString();
    }
    @Deprecated
    static int _getArrayDepth(Object o){
        if(!o.getClass().isArray() || Array.getLength(o)==0) return 0;
        return  1+_getArrayDepth(Array.get(o,0));
    }
    @Deprecated
    private static String _strArray(Object o,int depth){
        if(depth==0) return str(o);
        StringBuilder sb=new StringBuilder();
        for(int i=0,len=Array.getLength(o);i<len;i++){
            if(i!=0)sb.append(asep[depth]);
            sb.append(_strArray(Array.get(o,i),depth-1));
        }
        return sb.toString();
    }

    public static void pr(Object obj){
        out.print(str(obj));
    }
    public static void ln(){
        out.println();
    }
    public static void ln(Object... obj){
        for(Object o:obj){
            pr(o);ln();
        }
    }
}


import static java.lang.System.*;
import static java.lang.Math.*;

import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;

public class C305 {
    //public static Scanner sc=new Scanner(in);
    public static Random sc=new Random();

    void run() {
        final int n=sc.nextInt(100000);
        ArrayList<Long> as=new ArrayList<>();
        for(int i=0;i<n;i++)
            as.add(abs(sc.nextLong()%1000000000L));

        Collections.sort(as);

        //calc
        for(int i=0;i<as.size();i++){
            long v=as.get(i);
            int dup=1;
            for(int j=i+1;j<as.size();j++){
                if(as.get(j)!=v)break;
                dup++;
            }
            if(dup>1){
                for(int j=i;j<i+dup;j++){
                    as.remove(i);//i times add
                }
                for(int j=0;j<dup/2;j++){
                    as.add(i,v+1);//1/2 times add
                }
                if(dup%2==1)as.add(i,v);
            }
        }

        //get
        long pos=-1;
        int res=0;
        for(int i=0;i<as.size();i++){
            long v=as.get(i);
            res+=v-pos-1;
            pos=v;
        }
        ln(res);
    }

    public static void main(String[] args) {
        new C305().run();
    }

    //output lib
    static final String br=System.getProperty("line.separator");
    static final String[] asep=new String[]{""," ",br,br+br};

    static String str(Object o){
        int depth=_getArrayDepth(o);
        return depth>0?_strArray(o,depth):o.toString();
    }
    @Deprecated
    static int _getArrayDepth(Object o){
        if(!o.getClass().isArray() || Array.getLength(o)==0) return 0;
        return  1+_getArrayDepth(Array.get(o,0));
    }
    @Deprecated
    private static String _strArray(Object o,int depth){
        if(depth==0) return str(o);
        StringBuilder sb=new StringBuilder();
        for(int i=0,len=Array.getLength(o);i<len;i++){
            if(i!=0)sb.append(asep[depth]);
            sb.append(_strArray(Array.get(o,i),depth-1));
        }
        return sb.toString();
    }

    public static void pr(Object obj){
        out.print(str(obj));
    }
    public static void ln(){
        out.println();
    }
    public static void ln(Object... obj){
        for(Object o:obj){
            pr(o);ln();
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C184
{
	public C184()
	{
		Scanner in = new Scanner(System.in);

		int n = Integer.parseInt(in.nextLine());

		StringTokenizer st = new StringTokenizer(in.nextLine());
		int[] a = new int[n];
		for(int x = 0; x < n; x++)
			a[x] = Integer.parseInt(st.nextToken());

		boolean change;
		do
		{
			change = false;
			for(int x = 0; x < n - 1; x++)
			{
				if(a[x] == a[x + 1] && a[x] != -1)
				{
					change = true;
					a[x] = -1;
					a[x + 1]++;
				}
			}
			Arrays.sort(a);
		}while(change);

		int target = a[n - 1] + 1;
		for(int x = 0; x < n; x++)
			if(a[x] != -1)
				target--;

		System.out.println(target);
	}

	public static void main(String[] args)
	{
		new C184();
	}
}


import java.io.*;
import java.util.*;

public class C184C
{

private static StringTokenizer st;

    public static void nextLine(BufferedReader br) throws IOException
    {
        st = new StringTokenizer(br.readLine());
    }

    public static int nextInt()
    {
        return Integer.parseInt(st.nextToken());
    }

    public static String next()
    {
        return st.nextToken();
    }

    public static long nextLong()
    {
        return Long.parseLong(st.nextToken());
    }

    public static double nextDouble()
    {
        return Double.parseDouble(st.nextToken());
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        nextLine(br);
        int n = nextInt();
        nextLine(br);
        long[] arr = new long[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = nextLong();
        }
        ArrayList<Long> list = new ArrayList<Long>();
        int pos = 0;
        int npos = 0;
        long val = 0;
        int count = 0;
        while (pos < n)
        {
            while (count > 0 && val < arr[pos])
            {
                if (count % 2 == 1)
                {
                    list.add(val);
                }
                val++;
                count /= 2;
            }
            npos = pos;
            while (npos + 1 < n && arr[npos+1] == arr[pos])
            {
                npos++;
            }
            int d = npos - pos + 1 + count;
            if (d % 2 == 1)
            {
                list.add(arr[pos]);
            }
            count = d / 2;
            val = arr[pos] + 1;
            pos = npos + 1;
        }
        while (count > 0)
        {
            if (count % 2 == 1)
            {
                list.add(val);
            }
            val++;
            count /= 2;
        }
        System.out.println(list.get(list.size() - 1) - list.size() + 1);
    }

}


import java.util.Set;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.Collection;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Scanner in = new Scanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        long[] numbers = new long[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = in.nextLong();
        }

        Set<Long> nums = new TreeSet<Long>();
        int k = 0;
        while (k < numbers.length) {
            Long tmp = numbers[k];
            if (!nums.contains(tmp)) {
                nums.add(tmp);
            } else {
                long cur = tmp;
                while (nums.contains(cur)) {
                    nums.remove(cur);
                    cur++;
                }
                nums.add(cur);
            }

            k++;
        }

        long max = Collections.max(nums);

        out.print(max + 1 - nums.size());

    }
}


import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    static void zap() throws IOException {
        int n = nextInt();
        SortedMap<Integer, Integer> my = Collections.synchronizedSortedMap(new TreeMap<Integer, Integer>());
        for(int i=0;i<n;i++)
        {
            int number = nextInt();
            if(my.containsKey(number))
                my.put(number, my.get(number) + 1);
            else
                my.put(number, 1);
        }
        for(Map.Entry<Integer, Integer> entry : my.entrySet())
        {
            int occur = entry.getValue();
            if(occur == 1)
                continue;
            int incrNext = occur / 2;
            int thisKey = entry.getKey();
            if(my.containsKey(thisKey+1))
                my.put(thisKey+1, my.get(thisKey+1) + incrNext);
            else
                my.put(thisKey+1, incrNext);
            if(occur % 2 == 0)
                my.remove(thisKey);
            else
                entry.setValue(1);
        }
        out.println(my.lastKey() + 1 - my.size());
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(System.out);
        zap();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    static int binarySearch(ArrayList<Integer> lstInts, int l, int r, int key)
    {
        int pos = -1;
        while(l <= r)
        {
            int mid = l + (r - l)/2;
            int midInt = lstInts.get(mid);
            if(midInt < key)
                l = mid + 1;
            else if(midInt > key)
                r = mid - 1;
            else
                return mid;
        }
        return pos;
    }
    static void zap() throws IOException {
        int n = nextInt();
        int maxInt = -1;
        ArrayList<Integer> lstInts = new ArrayList<Integer>(n);
        for(int i=0;i<n;i++)
        {
            int num = nextInt();
            int ind = binarySearch(lstInts, 0, lstInts.size()-1, num);
            if(ind == -1)
            {
                lstInts.add(num);
                maxInt = Math.max(maxInt, num);
            }
            else
            {
                lstInts.remove(ind);
                lstInts.add(num + 1);
                maxInt = Math.max(maxInt, num + 1);
            }
        }
        out.println(maxInt + 1 - lstInts.size());
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(System.out);
        zap();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


/**
 * Created with IntelliJ IDEA.
 * User: Venky
 */
import java.io.*;
import java.util.*;

public class Main {

    static void solve() throws IOException {
        int n = nextInt();
        int[] arr = new int[n];
        HashMap<Integer, Integer> my = new HashMap<Integer, Integer>();
        for(int i=0;i<n;i++)
            arr[i] = nextInt();
        Arrays.sort(arr);
        int c = 0;
        for(int i=0;i<arr.length-1;i++)
        {
            if(arr[i] == arr[i+1])
                c++;
            else
            {
                c++;
                my.put(arr[i], c);
                c = 0;
            }
        }
        if(arr.length > 1)
        {
            if(arr[arr.length-1] == arr[arr.length-2])
            {
                c++;
                my.put(arr[arr.length-1], c);
            }
            else
            {
                my.put(arr[arr.length-1], 1);
            }
        }
        else
        {
            my.put(arr[0], 1);
        }

        Iterator it = my.keySet().iterator();
        while(it.hasNext())
        {
            int key = (Integer)it.next();
            int copyKey = key;
            int count = my.get(key);
            int copyCount = count;
            while( count > 1 )
            {
                if(count % 2 == 1)
                {
                    if(my.containsKey(key))
                    {
                        my.put(key, my.get(key) + 1);
                    }
                    else my.put(key, 1);
                }
                count /= 2;
                key ++;
            }
            if(my.containsKey(key))
            {
                my.put(key, my.get(key) + 1);
            }
            else my.put(key, 1);
            if(my.get(copyKey) == copyCount)
                my.remove(copyKey);
            else my.put(copyKey, my.get(copyKey) - copyCount);
        }
        it = my.keySet().iterator();
        int maxVal = Integer.MIN_VALUE;
        while (it.hasNext())
        {
            int key = (Integer)it.next();
            if(key > maxVal)
                maxVal = key;
        }
        out.println(maxVal - my.size() + 1);
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        PrintStream output = System.out;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(output);
        solve();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


/**
 * Created with IntelliJ IDEA.
 * User: Venky
 */
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Main {

    static void solve() throws IOException {
        int n = nextInt();
        int[] arr = new int[n];
        ConcurrentHashMap<Integer, Integer> my = new ConcurrentHashMap<Integer, Integer>();
        for(int i=0;i<n;i++)
            arr[i] = nextInt();
        Arrays.sort(arr);
        int c = 0;
        for(int i=0;i<arr.length-1;i++)
        {
            if(arr[i] == arr[i+1])
                c++;
            else
            {
                c++;
                my.put(arr[i], c);
                c = 0;
            }
        }
        if(arr.length > 1)
        {
            if(arr[arr.length-1] == arr[arr.length-2])
            {
                c++;
                my.put(arr[arr.length-1], c);
            }
            else
            {
                my.put(arr[arr.length-1], 1);
            }
        }
        else
        {
            my.put(arr[0], 1);
        }

        Iterator it = my.keySet().iterator();
        while(it.hasNext())
        {
            int key = (Integer)it.next();
            int copyKey = key;
            int count = my.get(key);
            int copyCount = count;
            while( count > 1 )
            {
                if(count % 2 == 1)
                {
                    if(my.containsKey(key))
                    {
                        my.put(key, my.get(key) + 1);
                    }
                    else my.put(key, 1);
                }
                count /= 2;
                key ++;
            }
            if(my.containsKey(key))
            {
                my.put(key, my.get(key) + 1);
            }
            else my.put(key, 1);
            if(my.get(copyKey) == copyCount)
                it.remove();
            else my.put(copyKey, my.get(copyKey) - copyCount);
        }
        it = my.keySet().iterator();
        int maxVal = Integer.MIN_VALUE;
        while (it.hasNext())
        {
            int key = (Integer)it.next();
            if(key > maxVal)
                maxVal = key;
        }
        out.println(maxVal - my.size() + 1);
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        PrintStream output = System.out;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(output);
        solve();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


/**
 * Created with IntelliJ IDEA.
 * User: Venky
 */
import java.io.*;
import java.util.*;

public class Main {

    static void solve() throws IOException {
        int n = nextInt();
        int[] arr = new int[n];
        HashMap<Integer, Integer> my = new HashMap<Integer, Integer>();
        for(int i=0;i<n;i++)
            arr[i] = nextInt();
        Arrays.sort(arr);
        int c = 0;
        for(int i=0;i<arr.length-1;i++)
        {
            if(arr[i] == arr[i+1])
                c++;
            else
            {
                c++;
                my.put(arr[i], c);
                c = 0;
            }
        }
        if(arr.length > 1)
        {
            if(arr[arr.length-1] == arr[arr.length-2])
            {
                c++;
                my.put(arr[arr.length-1], c);
            }
            else
            {
                my.put(arr[arr.length-1], 1);
            }
        }
        else
        {
            my.put(arr[0], 1);
        }

        Iterator it = my.keySet().iterator();
        while(it.hasNext())
        {
            int key = (Integer)it.next();
            int copyKey = key;
            int count = my.get(key);
            int copyCount = count;
            while( count > 1 )
            {
                if(count % 2 == 1)
                {
                    if(my.containsKey(key))
                    {
                        my.put(key, my.get(key) + 1);
                    }
                    else my.put(key, 1);
                }
                count /= 2;
                key ++;
            }
            if(my.containsKey(key))
            {
                my.put(key, my.get(key) + 1);
            }
            else my.put(key, 1);
            if(my.get(copyKey) == copyCount)
                it.remove();
            else my.put(copyKey, my.get(copyKey) - copyCount);
        }
        it = my.keySet().iterator();
        int maxVal = Integer.MIN_VALUE;
        while (it.hasNext())
        {
            int key = (Integer)it.next();
            if(key > maxVal)
                maxVal = key;
        }
        out.println(maxVal - my.size() + 1);
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        PrintStream output = System.out;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(output);
        solve();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    static void zap() throws IOException {
        int n = nextInt();
        TreeMap<Integer, Integer> my = new TreeMap<Integer, Integer>();
        for(int i=0;i<n;i++)
        {
            int number = nextInt();
            if(my.containsKey(number))
                my.put(number, my.get(number) + 1);
            else
                my.put(number, 1);
        }
        for(Integer thisKey=my.firstKey();thisKey!=null;thisKey=my.higherKey(thisKey))
        {
            int occur = my.get(thisKey);
            if(occur == 1)
                continue;
            int incrNext = occur / 2;
            if(my.containsKey(thisKey+1))
                my.put(thisKey+1, my.get(thisKey+1) + incrNext);
            else
                my.put(thisKey+1, incrNext);
            if(occur % 2 == 0)
                my.remove(thisKey);
            else
                my.put(thisKey, 1);
        }
        out.println(my.lastKey() + 1 - my.size());
    }

    static BufferedReader br;
    static StringTokenizer st;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        InputStream input = System.in;
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(System.out);
        zap();
        out.close();
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int N = in.nextInt();
        int[] A = new int[N];
        int v = 0;

        for(int i = 0; i < N; i++) {
            A[i] = in.nextInt();
            v = Math.max(v, A[i]);
        }

        int ans = v;
        int num = 0;
        int prev = 0;
        int i = 0;
        for(; i < N; ) {
            while(prev < A[i] && num > 0) {
                if((num - 1) % 2 != 0) {
                    ans++;
                    num++;
                }
                num = (num - 1) / 2;
                prev++;
                ans--;
            }

            num++;
            i++;
            while(i < N && A[i] == A[i - 1]) {
                num++;
                i++;
            }

            prev = A[i - 1];
        }

        while(num > 1) {
            if((num - 1) % 2 != 0) {
                ans++;
                num++;
            }
            num = (num - 1) / 2;
            prev++;
        }

        ans = Math.max(ans, 0);
        out.println(ans);
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    }


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int N = in.nextInt();
        int[] A = new int[N];
        int v = 0;

        for(int i = 0; i < N; i++) {
            A[i] = in.nextInt();
            v = Math.max(v, A[i]);
        }

        int ans = v;
        int num = 0;
        int prev = -1;
        int i = 0;
        for(; i < N; ) {
            while(prev + 1 <= A[i] && num > 0) {
                num = (num - 1) / 2;
                prev++;
                ans--;
            }

            num++;
            i++;
            while(i < N && A[i] == A[i - 1]) {
                num++;
                i++;
            }

            prev = A[i - 1];
        }

        ans = Math.max(ans, 0);
        out.println(ans);
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    }


import java.io.IOException;
import java.util.TreeMap;
import java.util.InputMismatchException;
import java.util.Set;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int N = in.nextInt();
        int[] A = new int[N];
        int v = 0;

        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for(int i = 0; i < N; i++) {
            int x = in.nextInt();
            v = Math.max(v, x);
            if(map.containsKey(x))
                map.put(x, map.get(x) + 1);
            else
                map.put(x, 1);
        }

        int ans = v;
        int val = 0;
        int prev = -1;
        int last = -1;
        for(int x : map.keySet()) {
            while(prev + 1 <= x && val > 0) {
                prev++;
                val = Math.max(0, val - 1) / 2;
                ans--;
            }

            val = val + map.get(x);

            prev = x;
            last = x;
        }

        while(prev + 1 <= last && val > 0) {
            prev++;
            val = Math.max(0, val - 1) / 2;
            ans--;
        }

        ans = Math.max(ans, 0);
        out.println(ans);
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    }


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author artyom
 */
public class IvanAndPowersOfTwo {

    public static void main(String args[]) {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            int max = 0;
            Set<Integer> powers = new HashSet<>(n);
            for (int i = 0; i < n; i++) {
                int a = sc.nextInt();
                while (powers.contains(a)) {
                    powers.remove(a);
                    a++;
                }
                powers.add(a);
                if (max < a) {
                    max = a;
                }
            }
            System.out.println(max - powers.size() + 1);
        }
    }
}


import java.io.*;
import java.util.*;

public class C {
    public static void main(String[] args) throws Exception {
        new C().solve();
    }

    void solve() throws IOException {
        BufferedReader in = new BufferedReader(new
                InputStreamReader(System.in));
        String[] sp;
        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(in.readLine());
        sp = in.readLine().split(" ");
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long.parseLong(sp[i]);
        }

        int count = 0;
        long prevNum = 0;
        int prevCount = 0;
        for (int i = 0; i < n; i++) {
            while (prevNum < a[i] && prevCount > 0) {
                count += prevCount % 2;
                prevNum++;
                prevCount /= 2;
            }
            prevCount++;
            prevNum = a[i];
        }

        while (prevCount > 0) {
            count += prevCount % 2;
            prevNum++;
            prevCount /= 2;
        }

        System.out.println(prevNum - count);
    }
}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(p.v)) set.remove(p.v);
				else {
					set.add(p.v);
				}
			}
			que.add(new P(p.v<<1L, p.c/2));
		}
		long max = 0;
//		debug(set);
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size() + 1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			return v - p.v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(((Long)p.v))) set.remove(((Long)p.v));
				else set.add(p.v);
			}
			que.add(new P(p.v+1, p.c/2));
		}
		long max = 0;
//		debug(set);
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size() + 1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			return v - p.v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(((Long)p.v))) set.remove(((Long)p.v));
				else set.add(p.v);
			}
			que.add(new P(p.v<<1L, p.c/2));
		}
		long max = 0;
		debug(set);
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size() + 1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			return v - p.v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(((Long)p.v))) {
					set.remove(((Long)p.v));
					que.add(new P(p.v+1, 1));
				}
				else set.add(p.v);
			}
			que.add(new P(p.v+1, p.c/2));
		}
		long max = 0;
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size()+1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			return v - p.v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(((Long)p.v))) set.remove(((Long)p.v));
				else set.add(p.v);
			}
			que.add(new P(p.v+1, p.c/2));
		}
		long max = 0;
//		debug(set);
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size() + 1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			return p.v - v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class C {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] ar = new long[n];
		HashMap<Long, Long> map = new HashMap<Long, Long>();
		for(int i=0;i<n;i++) ar[i] = Integer.valueOf(sc.next());
		for(int i=0;i<n;i++) {
			if(map.containsKey(ar[i])) map.put(ar[i], map.get(ar[i])+1L);
			else map.put(ar[i], 1L);
		}
		HashSet<Long> set = new HashSet<Long>();
		PriorityQueue<P> que = new PriorityQueue<P>();
		for(Map.Entry<Long, Long> entry: map.entrySet()) que.add(new P(entry.getKey(), entry.getValue()));
		for(;!que.isEmpty();) {
			P p = que.remove();
			if(p.c==0) continue;
			if(p.c%2!=0) {
				if(set.contains(p.v)) set.remove(p.v);
				else {
					set.add(p.v);
				}
			}
			que.add(new P(p.v<<1L, p.c/2));
		}
		long max = 0;
//		debug(set);
		for(long v: set) max = max(max, v);
		System.out.println(max - set.size() + 1);
	}

	class P implements Comparable<P>{
		long v, c;
		P(long v, long c) {
			this.v = v;
			this.c = c;
		}
		public int compareTo(P p) {
			// TODO 自動生成されたメソッド・スタブ
			return v - p.v>0? 1:-1;
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new C().run();
	}
}


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; ++i)
            a[i] = in.nextInt();

        HashSet<Integer> have = new HashSet<>();
        have.add(a[0]);
        int max_el = a[0];
        for (int i = 1; i < n; ++i) {
            int tmp = a[i];
            while (have.contains(tmp)) {
                have.remove(tmp);
                ++tmp;
            }
            have.add(tmp);
            max_el = Math.max(max_el, tmp);
        }

        out.println(max_el - have.size() + 1);
    }
}

class InputReader {
    BufferedReader in;
    StringTokenizer st;

    public InputReader(InputStream stream) {
        in = new BufferedReader(new InputStreamReader(stream));
        eat("");
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public String next() {
        while (!st.hasMoreTokens())
            eat(nextLine());
        return st.nextToken();
    }

    public String nextLine() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public void eat(String str) {
        if (str == null) throw new InputMismatchException();
        st = new StringTokenizer(str);
    }
}


import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class C {

	void run() throws IOException {
		int n = ni();
		TreeMap<Long, Integer> t = new TreeMap<Long, Integer>();
		TreeMap<Long, Integer> q = new TreeMap<Long, Integer>();
		for (int i = 0; i < n; i++) {
			add(t, ni());
		}
		while (t.size() > 0) {
			Entry<Long, Integer> e = t.pollFirstEntry();
			long k = e.getKey(), v = e.getValue(), x = 0;
			// tr(k + " " + v);
			while (v > 0) {
				if (v % 2 == 1) {
					if (x != 0) {
						add(t, k + x);
					} else {
						add(q, k + x);
					}
				}
				x++;
				v /= 2;
			}
		}
		long ans = 0;
		// add(q, -1);
		// long max = q.pollLastEntry().getKey();
		long min = -1;
		while (q.size() > 0) {
			Entry<Long, Integer> e = q.pollFirstEntry();
			long k = e.getKey(), v = e.getValue();
			ans += k - min - 1;
			min = k;
			// tr(e.getKey() + " " + e.getValue());
		}
		pw.println(ans);
	}

	void add(TreeMap<Long, Integer> t, long k) {
		if (t.containsKey(k)) {
			int v = t.remove(k);
			t.put(k, v + 1);
		} else {
			t.put(k, 1);
		}
	}

	int[] na(int a_len) throws IOException {
		int[] _a = new int[a_len];
		for (int i = 0; i < a_len; i++)
			_a[i] = ni();
		return _a;
	}

	int[][] nm(int a_len, int a_hei) throws IOException {
		int[][] _a = new int[a_len][a_hei];
		for (int i = 0; i < a_len; i++)
			for (int j = 0; j < a_hei; j++)
				_a[i][j] = ni();
		return _a;
	}

	String next() throws IOException {
		while (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	int ni() throws IOException {
		return Integer.parseInt(next());
	}

	String nl() throws IOException {
		return br.readLine();
	}

	void tr(String debug) {
		if (!OJ)
			pw.println(" " + debug);
	}

	static PrintWriter pw;
	static BufferedReader br;
	static StringTokenizer st;
	static boolean OJ;

	public static void main(String[] args) throws IOException {
		long timeout = System.currentTimeMillis();
		OJ = System.getProperty("ONLINE_JUDGE") != null;
		pw = new PrintWriter(System.out);
		br = new BufferedReader(OJ ? new InputStreamReader(System.in) : new FileReader(new File("C.txt")));

		while (br.ready())
			new C().run();
		if (!OJ) {
			pw.println();
			pw.println(System.currentTimeMillis() - timeout);
		}
		br.close();
		pw.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class C {

	public static void main(String [] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(br.readLine());
		StringTokenizer tokenizer = new StringTokenizer(br.readLine());

		List<Long> a = new ArrayList<Long>();

		TreeMap<Long, Integer> map = new TreeMap<Long, Integer>();

		for(int i=0; i<N; i++) {
			long num =Integer.parseInt(tokenizer.nextToken());
			a.add(num);

			Integer val = map.get(num);

			if(val == null) val = 0;

			val++;

			map.put(num, val);
		}

		ArrayList<Long> sols = new ArrayList<Long>();
		for(int i=0; i<a.size(); i++) {

			long num = a.get(i);

			int counter = 0;

			boolean once = false;
			while((counter = map.get(num)) > 1) {

				Long nextNum = 2*num;

				if(num == 0) {
					nextNum = 1l;
				}

				Integer nextVal = map.get(nextNum);
				if(nextVal == null) nextVal = 0;

				nextVal++;
				map.put(nextNum, nextVal);
				map.put(num, counter-2);

				if(!once) {
					once = true;
					a.add(nextNum);
				}
			}

			if(map.get(num) == 0) {
				map.remove(num);
			}
			else {
				sols.add(num);
			}

			while(i < a.size() && a.get(i) == num) i++;

			i--;
		}

		Long firstEntry = map.firstKey();
		boolean hasZero = (firstEntry == 0);

		Long lastKey = 0l;
		if(!hasZero) {
			lastKey = -1l;
		}
		Long totalCounter = 0l;
		for(Long key : map.keySet()) {
			//System.out.println("key: " + key);
			if(key == 0) {
				continue;
			}
			totalCounter += key - 1 - lastKey;
			//System.out.println("key: " + key);
			lastKey = key;
		}

		System.out.println(totalCounter);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class C {

	public static void main(String [] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(br.readLine());
		StringTokenizer tokenizer = new StringTokenizer(br.readLine());

		List<Long> a = new ArrayList<Long>();

		TreeMap<Long, Integer> map = new TreeMap<Long, Integer>();

		for(int i=0; i<N; i++) {
			long num =Integer.parseInt(tokenizer.nextToken());
			a.add(num);

			Integer val = map.get(num);

			if(val == null) val = 0;

			val++;

			map.put(num, val);
		}

		ArrayList<Long> sols = new ArrayList<Long>();
		for(int i=0; i<a.size(); i++) {

			long num = a.get(i);

			int counter = 0;

			boolean once = false;
			while((counter = map.get(num)) > 1) {

				Long nextNum = num + 1;

				Integer nextVal = map.get(nextNum);
				if(nextVal == null) nextVal = 0;

				nextVal++;
				map.put(nextNum, nextVal);
				map.put(num, counter-2);

				if(!once) {
					once = true;
					a.add(nextNum);
				}
			}

			if(map.get(num) == 0) {
				map.remove(num);
			}
			else {
				sols.add(num);
			}

			while(i < a.size() && a.get(i) == num) i++;

			i--;
		}

		Long firstEntry = map.firstKey();
		boolean hasZero = (firstEntry == 0);

		Long lastKey = 0l;
		if(!hasZero) {
			lastKey = -1l;
		}
		Long totalCounter = 0l;
		for(Long key : map.keySet()) {
			//System.out.println("key: " + key);
			if(key == 0) {
				continue;
			}
			totalCounter += key - 1 - lastKey;
			//System.out.println("key: " + key);
			lastKey = key;
		}

		System.out.println(totalCounter);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class C {

	public static void main(String [] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(br.readLine());
		StringTokenizer tokenizer = new StringTokenizer(br.readLine());

		List<Long> a = new ArrayList<Long>();

		TreeMap<Long, Integer> map = new TreeMap<Long, Integer>();

		for(int i=0; i<N; i++) {
			long num =Integer.parseInt(tokenizer.nextToken());
			a.add(num);

			Integer val = map.get(num);

			if(val == null) val = 0;

			val++;

			map.put(num, val);
		}

		ArrayList<Long> sols = new ArrayList<Long>();
		for(int i=0; i<a.size(); i++) {

			long num = a.get(i);
			int counter = 0;

			boolean once = false;
			while((counter = map.get(num)) > 1) {

				Long nextNum = 2*num;

				Integer nextVal = map.get(nextNum);
				if(nextVal == null) nextVal = 0;

				nextVal++;
				map.put(nextNum, nextVal);
				map.put(num, counter-2);

				if(!once) {
					once = true;
					a.add(nextNum);
				}
			}

			if(map.get(num) == 0) {
				map.remove(num);
			}
			else {
				sols.add(num);
			}

			while(i < a.size() && a.get(i) == num) i++;

			i--;
		}

		Long firstEntry = map.firstKey();
		boolean hasZero = (firstEntry == 0);

		Long lastKey = 0l;
		if(!hasZero) {
			lastKey = -1l;
		}
		Long totalCounter = 0l;
		for(Long key : map.keySet()) {

			if(key == 0) {
				continue;
			}
			totalCounter += key - 1 - lastKey;
			//System.out.println("key: " + key);
			lastKey = key;
		}

		System.out.println(totalCounter);

	}

}


/*               ______  ______
 \      / |\   |    |    |       |    |  |   |
  \    /  | \  |    |    |____   |____|  |___|
   \  /   |  \ |    |        |   |    |  |   |
    \/    |   \|    |   _____|   |    |  |   |

  -----------IIT Indore----------*/
#include<bits/stdc++.h>
#define rep(i,start,lim) for(long long i=start;i<lim;i++)
#define repd(i,start,lim) for(long long i=start;i>=lim;i--)
#define scan(x) scanf("%lld",&x)
#define print(x) printf("%lld ",x)
#define sz(a) lld((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define tr(it,c) for(__typeof(c.begin()) it = c.begin(); it!=c.end(); ++it)
#define br printf("\n")
#define checkbit(n,b) ((n>>b)&1)
#define TRACE
#ifdef TRACE
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#else
#define trace1(x)                printf("")
#define trace2(x, y)             printf("")
#define trace3(x, y, z)          printf("")
#define trace4(a, b, c, d)       printf("")
#define trace5(a, b, c, d, e)    printf("")
#define trace6(a, b, c, d, e, f) printf("")
#endif
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
inline lld lcm(lld a,lld b){ return (a/__gcd(a, b))*b; }
template<typename T1, typename T2> inline pair<T1, T2> operator+(const pair<T1, T2> &p1 , const pair<T1, T2> &p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); }
template<typename T1, typename T2> inline pair<T1, T2> operator-(const pair<T1, T2> &p1 , const pair<T1, T2> &p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); }
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
template<typename T, typename T2>void printarray(T a[], T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; }
#define N 100005
mlld m;
vlld v;
int main()
{
	lld n,x;
	cin>>n;
	rep(i,0,n)
	{
		cin>>x;
		while(m[x]!=0) m[x]=0,x++;
		m[x]=1;
	}
	lld ans=0;
	for(auto i:m) v.eb(i.f);
	lld tmp=sz(v);
	rep(i,0,tmp)
	{
		if(i==0) ans+=(v[i]);
		else ans+=(v[i]-v[i-1]-1);
	}
	print(ans);
	return 0;
}


/*               ______  ______
 \      / |\   |    |    |       |    |  |   |
  \    /  | \  |    |    |____   |____|  |___|
   \  /   |  \ |    |        |   |    |  |   |
    \/    |   \|    |   _____|   |    |  |   |

  -----------IIT Indore----------*/
#include<bits/stdc++.h>
#define rep(i,start,lim) for(long long i=start;i<lim;i++)
#define repd(i,start,lim) for(long long i=start;i>=lim;i--)
#define scan(x) scanf("%lld",&x)
#define print(x) printf("%lld ",x)
#define sz(a) lld((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define tr(it,c) for(__typeof(c.begin()) it = c.begin(); it!=c.end(); ++it)
#define br printf("\n")
#define checkbit(n,b) ((n>>b)&1)
#define TRACE
#ifdef TRACE
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#else
#define trace1(x)                printf("")
#define trace2(x, y)             printf("")
#define trace3(x, y, z)          printf("")
#define trace4(a, b, c, d)       printf("")
#define trace5(a, b, c, d, e)    printf("")
#define trace6(a, b, c, d, e, f) printf("")
#endif
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
inline lld lcm(lld a,lld b){ return (a/__gcd(a, b))*b; }
template<typename T1, typename T2> inline pair<T1, T2> operator+(const pair<T1, T2> &p1 , const pair<T1, T2> &p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); }
template<typename T1, typename T2> inline pair<T1, T2> operator-(const pair<T1, T2> &p1 , const pair<T1, T2> &p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); }
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
template<typename T, typename T2>void printarray(T a[], T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; }
#define N 100005
mlld m;
vlld v;
int main()
{
	lld n,x;
	cin>>n;
	rep(i,0,n)
	{
		cin>>x;
		while(m[x]) m[x]=0,x++;
		m[x]=1;
	}
	lld ans=0;
	for(auto i:m) if(i.s==1) v.eb(i.f);
	lld tmp=sz(v);
	rep(i,0,tmp)
	{
		if(i==0) ans+=(v[i]);
		else ans+=(v[i]-v[i-1]-1);
	}
	print(ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(x) push_back((x))
#define REP(i,x,y) for(long long (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)
#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define mt(x,y,z) mp(mp((x),(y)),z)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
//#define NDEBUG 1
#define fst first
#define snd second
#define sz(v) ((int)v.size())
#define itm1 fst.fst
#define itm2 fst.snd
#define itm3 snd
#define MAX 1000000009
typedef vector<int> vi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ii,ull> tri;
typedef vector<ii> vii;
typedef vector<tri> vt;

vector<ll> A;

ll fastExp(int base, int e){
	//Usando exponentes de 2
	ll ans = 1;
	while(e){
		if (e & 1) ans *= base;
		base *= base;
		e >>= 1;
	}
	return ans;
}

int main(){
	ll n;
	cin>>n;
	ll curr = 0, pos = 0, ans =0;
	REP(i,0,n){
		ll num; cin>>num;
		while(num>pos){
			if(curr%2==0) ans++;
			curr = curr/2;
			pos++;
			if(curr<=0){
				ans += num-pos;
				pos = num;

			}
		}
		if(num==pos){
			curr++;
		}
	}

	while(curr>0){
		if(curr%2==0) ans++;
		curr = curr/2;
	}

	cout<<ans<<endl;

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(x) push_back((x))
#define REP(i,x,y) for(long long (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)
#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define mt(x,y,z) mp(mp((x),(y)),z)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
//#define NDEBUG 1
#define fst first
#define snd second
#define sz(v) ((int)v.size())
#define itm1 fst.fst
#define itm2 fst.snd
#define itm3 snd
#define MAX 1000000009
typedef vector<int> vi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ii,ull> tri;
typedef vector<ii> vii;
typedef vector<tri> vt;

vector<ll> A;

ll fastExp(int base, int e){
	//Usando exponentes de 2
	ll ans = 1;
	while(e){
		if (e & 1) ans *= base;
		base *= base;
		e >>= 1;
	}
	return ans;
}

int main(){
	ll n;
	cin>>n;
	ll curr = 0, pos = 0, ans =0;
	REP(i,0,n){
		ll num; cin>>num;
		while(num>pos){
			pos++;
			if(curr%2==0) ans++;
			curr = curr/2;
		}
		if(num==pos){
			curr++;
		}
	}

	while(curr>0){
		if(curr%2==0) ans++;
		curr = curr/2;
	}

	cout<<ans<<endl;

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(x) push_back((x))
#define REP(i,x,y) for(long long (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)
#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define mt(x,y,z) mp(mp((x),(y)),z)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
//#define NDEBUG 1
#define fst first
#define snd second
#define sz(v) ((int)v.size())
#define itm1 fst.fst
#define itm2 fst.snd
#define itm3 snd
#define MAX 1000000009
typedef vector<int> vi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ii,ull> tri;
typedef vector<ii> vii;
typedef vector<tri> vt;

vector<ll> A;

ll fastExp(int base, int e){
	//Usando exponentes de 2
	ll ans = 1;
	while(e){
		if (e & 1) ans *= base;
		base *= base;
		e >>= 1;
	}
	return ans;
}

int main(){
	ll n;
	cin>>n;
	ll curr = 0, pos = 0, ans =0;
	REP(i,0,n){
		ll num; cin>>num;
		while(num>pos){
			if(curr>0) pos++;
			else pos = num;
			if(curr%2==0) ans++;
			curr = curr/2;
		}
		if(num==pos){
			curr++;
		}
	}

	while(curr>0){
		if(curr%2==0) ans++;
		curr = curr/2;
	}

	cout<<ans<<endl;

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(x) push_back((x))
#define REP(i,x,y) for(long long (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)
#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define mt(x,y,z) mp(mp((x),(y)),z)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
//#define NDEBUG 1
#define fst first
#define snd second
#define sz(v) ((int)v.size())
#define itm1 fst.fst
#define itm2 fst.snd
#define itm3 snd
#define MAX 1000000009
typedef vector<int> vi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ii,ull> tri;
typedef vector<ii> vii;
typedef vector<tri> vt;

vector<ll> A;

ll fastExp(int base, int e){
	//Usando exponentes de 2
	ll ans = 1;
	while(e){
		if (e & 1) ans *= base;
		base *= base;
		e >>= 1;
	}
	return ans;
}

int main(){
	ll n;
	cin>>n;
	ll curr = 0, pos = 0, ans =0;
	REP(i,0,n){
		ll num; cin>>num;

		if(num==pos){
			curr++;
		}
		while(num>pos){
			pos++;
			if(curr%2==0) ans++;
			curr = curr/2;
		}
	}

	while(curr>0){
		if(curr%2==0) ans++;
		curr = curr/2;
	}

	cout<<ans<<endl;

	return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector < int > vi;
#define SIZE(X) ((int)(X.size()))
#define X first
#define Y second
#define LENGTH(X) ((int)(X.length()))
#define pb push_back
#define MP(X,Y) make_pair(X,Y)
#define MEM(a,b) memset(a,(b),sizeof(a))
#define tr(c,it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define ain(a,n) int ((a)[(n)]); for(int i=0; i<(n); i++) cin>>((a)[i])
typedef long long int int64;
typedef unsigned long long uLL;
#define FOR(i,n) for(int i=0;i<n;i++)
#define FU(i,a,n) for(int i=a;i<=n;i++)
#define FD(i,a,n) for(int i=n;i>=a;i--)
#define GI(n) int (n); scanf("%d",&(n))
#define GS(n) scanf("%s"&s)
#define PII(n,m) printf("%d %d\n",n,m)
#define PL(n) printf("%I64d\n",n)
#define F(i,L,R) for (int i = L; i < R; i++)
#define GII(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define IN GI(t);while(t--)
#define PLL(n) printf("%I64d\n",n)
#define GLL(n) scanf("%I64d",&n)
#define GL(n) scanf("%I64d",&n)
#define PN printf("\n")

#define pr16

#ifdef pr16
  #define pr(x)                 cerr << #x << ": " << x << endl;
  #define pr2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  #define pr3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
  #define pr4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
  #define pr5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
  #define pr6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl;
  #define prc(a) tr(a, it) cerr<<*(it)<<" "; cerr<<endl
  #define pra(a,n) for(int i=0; i<(n); i++) cerr<<((a)[i])<<" "; cerr<<"\n"
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl;
  #define prddd(a,x,y,z) for(int i=0;i<x;i++) {cerr<<"layer "<<i<<":\n";prdd(a[i],y,z)}

#else
  #define pr(x)
  #define pr2(x, y)
  #define pr3(x, y, z)
  #define pr4(a, b, c, d)
  #define pr5(a, b, c, d, e)
  #define pr6(a, b, c, d, e, f)
  #define tr(c,it)
  #define prc(a)
  #define pra(a,n)
  #define prdd(a, r, c)
  #define prddd(a,x,y,z)
#endif

#define MAXN 100007

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    set <int> f;
    int i,j,k,m,n;
    int ans=0;
    cin>>n;
    f.clear();
    for (i=0;i<n;i++){
        cin>>j;
        while(f.count(j))f.erase(j),j++;
        f.insert(j);
        ans=max(ans,j);
    }
    cout<<ans-f.size()+1<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector < int > vi;
#define SIZE(X) ((int)(X.size()))
#define X first
#define Y second
#define LENGTH(X) ((int)(X.length()))
#define pb push_back
#define MP(X,Y) make_pair(X,Y)
#define MEM(a,b) memset(a,(b),sizeof(a))
#define tr(c,it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define ain(a,n) int ((a)[(n)]); for(int i=0; i<(n); i++) cin>>((a)[i])
typedef long long int int64;
typedef unsigned long long uLL;
#define FOR(i,n) for(int i=0;i<n;i++)
#define FU(i,a,n) for(int i=a;i<=n;i++)
#define FD(i,a,n) for(int i=n;i>=a;i--)
#define GI(n) int (n); scanf("%d",&(n))
#define GS(n) scanf("%s"&s)
#define PII(n,m) printf("%d %d\n",n,m)
#define PL(n) printf("%I64d\n",n)
#define F(i,L,R) for (int i = L; i < R; i++)
#define GII(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define IN GI(t);while(t--)
#define PLL(n) printf("%I64d\n",n)
#define GLL(n) scanf("%I64d",&n)
#define GL(n) scanf("%I64d",&n)
#define PN printf("\n")

#define pr16

#ifdef pr16
  #define pr(x)                 cerr << #x << ": " << x << endl;
  #define pr2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  #define pr3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
  #define pr4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
  #define pr5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
  #define pr6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl;
  #define prc(a) tr(a, it) cerr<<*(it)<<" "; cerr<<endl
  #define pra(a,n) for(int i=0; i<(n); i++) cerr<<((a)[i])<<" "; cerr<<"\n"
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl;
  #define prddd(a,x,y,z) for(int i=0;i<x;i++) {cerr<<"layer "<<i<<":\n";prdd(a[i],y,z)}

#else
  #define pr(x)
  #define pr2(x, y)
  #define pr3(x, y, z)
  #define pr4(a, b, c, d)
  #define pr5(a, b, c, d, e)
  #define pr6(a, b, c, d, e, f)
  #define tr(c,it)
  #define prc(a)
  #define pra(a,n)
  #define prdd(a, r, c)
  #define prddd(a,x,y,z)
#endif

#define MAXN 100007

const int mod = 1e9+7;

long long expMod(long long base, long long ex, long long mod)
{
  if(ex == 0) return 1;
  long long temp = expMod(base,ex/2,mod);
  temp = (temp*temp)%mod;
  if(ex%2) temp = (temp*base)%mod;
  return temp;
}

int n,a[MAXN];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    ll sum = 0;
    FOR(i,n){
        cin>>a[i];
        sum += expMod(2ll,a[i],mod);
    }
    //cout<<sum<<endl;
    int high=1;
    ll temp = sum;
    while(temp>>=1 > 0){
        high++;
    }
    //cout<<high<<endl;
    int num = __builtin_popcountll(sum);
    cout<<high-num<<endl;
    return 0;
}


//
//  main.cpp
//  codeforces
//
//  Created by Augustus on 15/6/30.
//  Copyright (c) 2015年 Augustus. All rights reserved.
//
/*
 int min(int a, int b){
 if (a>b) {
 return b;
 }
 return a;
 }
 */
#include <iostream>
#include<string>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
//#include<stack>
#include<math.h>
#include<algorithm>
#include<cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace::std;
bool cmp(int a, int b){
    return a>b;
}
int abs(int a){
    if (a>0) {
        return a;
    }
    return -a;
}
long long max(long long a,long long b){
    if (a>b) {
        return a;
    }
    return b;
}
int a[100000];
int b[100000];
int main(){
    int n;
    while (cin>>n) {
        for (int i = 0; i<n; i++) {
            cin>>a[i];
        }
        int i = 1,j = 0;
        int tem = 1;
        if (n==1) {
            b[j++] = a[0];
            tem = 0;
        }
        while (i<n) {
            while (i<n&&a[i-1]==a[i]) {
                i++;
                tem++;
            }
            if (i<n) {
                while (a[i-1]!=a[i]&&tem) {
                    if (tem%2) {
                        b[j++] = a[i-1];
                    }
                    a[i-1]++;
                    tem/=2;
                }
                tem++;
                i++;
            }
        }
        int ma = a[n-1];
        while (tem) {
            if (tem%2) {
                b[j++] = ma;
            }
            ma++;
            tem/=2;
        }
//        for (i = 0; i<j; i++) {
//            cout<<b[i]<<" ";
//        }
//        cout<<endl;
        int result = b[0];
        for ( i = 1; i<j; i++) {
            result+=b[i]-b[i-1]-1;
        }
        cout<<result<<endl;
    }
    return 0;
}


//
//  main.cpp
//  codeforces
//
//  Created by Augustus on 15/6/30.
//  Copyright (c) 2015年 Augustus. All rights reserved.
//
/*
 int min(int a, int b){
 if (a>b) {
 return b;
 }
 return a;
 }
 */
#include <iostream>
#include<string>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
//#include<stack>
#include<math.h>
#include<algorithm>
#include<cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace::std;
bool cmp(int a, int b){
    return a>b;
}
int abs(int a){
    if (a>0) {
        return a;
    }
    return -a;
}
long long max(long long a,long long b){
    if (a>b) {
        return a;
    }
    return b;
}
int a[100000];
int b[100000];
int main(){
    int n;
    while (cin>>n) {
        for (int i = 0; i<n; i++) {
            cin>>a[i];
        }
        int i = 1,j = 0;
        int tem = 1;
        while (i<n) {
            while (i<n&&a[i-1]==a[i]) {
                i++;
                tem++;
            }
            if (i<n) {
                if (tem%2) {
                    b[j++] = a[i-1];
//                    b[j++][1] = 1;
                }
                tem/=2;
                tem++;
                i++;
            }
        }
        int ma = a[n-1];
        while (tem) {
            if (tem%2) {
                b[j++] = ma;
                //                        b[j++][0] = 1;
            }
            ma++;
            tem/=2;
        }
//        for (i = 0; i<j; i++) {
//            cout<<b[i]<<" ";
//        }
//        cout<<endl;
        int result = b[0];
        for ( i = 1; i<j; i++) {
            result+=b[i]-b[i-1]-1;
        }
        cout<<result<<endl;
    }
    return 0;
}


/**/
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compile and get accepted. KEE O.o
 *      ____________
 *     /         __ \
 *    /   __    |  | \
 *   /   |__|   |  |  \
 *  (           |__|   )
 *   \                /
 *    \      ___     /
 *     \____________/
 */
const long long mod = 1000000007;
int cnt[100100];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin>>N;
	int x;
	for(int i = 0;i < N;i++)cin>>x,cnt[x]++;
	int end = 0;
	for(int i = 0;i < 100100;i++){
		cnt[i+1] += cnt[i]/2;
		cnt[i] &= 1;
		if(cnt[i] == 1)end = i;
	}
	int res = 0;
	for(int i = 0;i <= end;i++){
		res += (cnt[i] != 1);
	}
	cout<<res<<'\n';
	return 0;
}


/**/
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compile and get accepted. KEE O.o
 *      ____________
 *     /         __ \
 *    /   __    |  | \
 *   /   |__|   |  |  \
 *  (           |__|   )
 *   \                /
 *    \      ___     /
 *     \____________/
 */
const long long mod = 1000000007;
map<int,int> cnt;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin>>N;
	int x;
	for(int i = 0;i < N;i++)cin>>x,cnt[x]++;
	int end = 0;
	for(auto & i : cnt){
		if(i.second > 1)
			cnt[i.first+1] += i.second/2;
		i.second &= 1;
	}
	int res = 0,pre = 0;
	for(auto & i : cnt){
		if(i.second == 0)continue;
		res += i.first - pre;
		pre = i.first+1;
	}
	cout<<res<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 1; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               mp[x] -= (1 << i);
            }
        }
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 1; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               mp[x] -= (1 << i);
            }
        }
    }
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 0; i <= 20; i++){
            if((y >> i) & 1) mp[x + i] ++;
        }
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 0; i <= 20; i++){
            if((y >> i) & 1) mp[x + i] ++;
        }
    }
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 0; i <= 20; i++){
            if((y >> i) & 1) mp[x + i] ++;
        }
    }
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 1; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               mp[x] -= (1 << i);
            }
        }
    }
   /* for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 1; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               mp[x] -= (1 << i);
            }
        }
    }*/
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 0; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               y -= (1 << i);
            }
        }
    }
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#define fir first
#define sec second
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;

map <int ,int > mp;
int n,x,ma,ans;
int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&x);
        mp[x] ++;
    }
    for(auto p: mp){
        int x = p.first , y = p.second;
        for(int i = 1; i <= 20; i++){
            if((y >> i) & 1){
               mp[x + i] ++;
               p.second -= (1 << i);
            }
        }
    }
    for(auto p : mp){
        int x = p.first , y = p.second;;
        if(y != 0){
           ans ++;
           ma = max(ma,x);
        }

    }
   // printf ("%d %d\n",ma,ans);
    printf ("%d\n",ma + 1 - ans);

}


/* -*- coding: utf-8; tab-width: 4; mode: c++ -*- */

#include <cstdio>
#include <queue>
#include <vector>
#include <functional>

int
main()
{
	int n;
	std::priority_queue<unsigned int, std::vector<unsigned int>, std::greater<unsigned int>> q;
	unsigned int v, c(0);

	std::scanf("%d", &n);
	while (0 < n--) {
		std::scanf("%u", &v);
		q.push(v);
	}

	while (1 < q.size()) {
		v = q.top();
		q.pop();
		if (v != q.top()) {
			++c;
		}
		else {
			q.pop();
			q.push(v + 1);
		}
	}

	v = q.top();
	std::printf("%u\n", v - c);

	return 0;
}


/* -*- coding: utf-8; tab-width: 4; mode: c++ -*- */

#include <cstdio>
#include <queue>

int
main()
{
	int n, a;
	std::priority_queue<long long> q;
	int c(0);
	long long v(1), w;

	std::scanf("%d", &n);
	while (0 < n--) {
		std::scanf("%d", &a);
		q.push(-a);
	}

	while (1 < q.size()) {
		v = q.top();
		q.pop();
		if (v != q.top()) {
			++c, v = 1;
		}
		else {
			q.pop();
			q.push(v - 1);
		}
	}

	v = -q.top();
	std::printf("%d\n", (int)(v - c));

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	long long c=0;
	for(int i=0;i<n;i++)
	{
		int q;
		cin>>q;
		c+=(1LL<<q);
	}
	int cnt=0;
	long long d=c;
	while(d)
	{
		d/=2;
		cnt++;
	}
	long long tt=(__builtin_popcount(c));
	cout<<cnt-tt<<endl;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	map<int,int>mp;
	for(int i=0;i<n;i++)
	{
		int q;
		cin>>q;
		mp[q]++;
	}
	int last=0;
	for(auto &i:mp)
	{
		if(i.second>1)
		{
			last=i.second/2;
			i.second%=2;
			mp[i.first+1]+=last;
		}
	}
	int cnt=0;
	for(auto i:mp)
	{
		cnt+=(i.second==1);
	}
	auto it=mp.end();
	it--;
	int maxx=it->first;

	cout<<maxx-cnt+1<<endl;

	/*long long c=0;
	for(int i=0;i<n;i++)
	{
		int q;
		cin>>q;
		c+=(1LL<<q);
	}
	int cnt=0;
	long long d=c;
	while(d)
	{
		d/=2;
		cnt++;
	}
	long long tt=(__builtin_popcount(c));
	cout<<cnt-tt<<endl;*/

}


#include<bits/stdc++.h>
#define pb push_back
#define pp pop_back()
#define mp make_pair
#define fr first
#define sc second
#define sz size()
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define all(x) x.begin(),x.end()
#define CS ios_base::sync_with_stdio(false);
#define show(x) cerr << #x << " is " << x <<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
map<ll,int> mark;
int32_t main()
{
	int n; scanf("%d",&n);
	set<ll> s;
	for(int i=0;i<n;i++){
		int x; scanf("%d",&x);
		mark[x]++; s.insert(x);
	}
	int p=0;
	ll MX=0;
	while(s.sz){
		ll u = *s.begin();
		smax(MX,u);
		mark[u+1] += mark[u]/2;
		if(mark[u+1]) s.insert(u+1);
		if(mark[u]%2) p--;
		s.erase(s.begin());
	}
	return cout<<MX+p+1<<endl,0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pp pop_back()
#define mp make_pair
#define fr first
#define sc second
#define sz size()
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define all(x) x.begin(),x.end()
#define CS ios_base::sync_with_stdio(false);
#define show(x) cerr << #x << " is " << x <<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
const int M=1e5+5;
map<ll,int> mark;
int32_t main()
{
	int n; scanf("%d",&n);
	set<ll> s;
	for(int i=0;i<n;i++){
		int x; scanf("%d",&x);
		mark[x]++; s.insert(x);
	}
	int p=0;
	ll MX=0;
	while(s.sz){
		ll u = *s.begin();
		smax(MX,u);
		mark[2*u] += mark[u]/2;
		if(mark[2*u]) s.insert(2*u);
		if(mark[u]%2) p--;
		s.erase(s.begin());
	}
	return cout<<MX+p+1<<endl,0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+7,INF=0x3f3f3f3f;
set<int> se;
int main()
{
    int n,ma=-INF;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        ma=max(a,ma);
        if(se.count(a))
        {
            se.erase(a);
            se.insert(a+1);
            ma=max(ma,a+1);
        }
        else se.insert(a);
    }
    printf("%d\n",ma-se.size()+1);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+7,INF=0x3f3f3f3f;
set<ll> se;
int main()
{
    int n;
    ll ma=-INF;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        ll a;
        scanf("%I64d",&a);
        ma=max(a,ma);
        if(se.count(a))
        {
            se.erase(a);
            se.insert(a+1);
            ma=max(ma,a+1);
        }
        else se.insert(a);
    }
    printf("%d\n",ma-(ll)se.size()+1);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+7,INF=0x3f3f3f3f;
set<ll> se;
int main()
{
    int n;
    ll ma=-INF;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        ll a;
        scanf("%I64d",&a);

        while(se.count(a))
        {
            se.erase(a);
            a++;
        }
        se.insert(a);
        ma=max(a,ma);
        if(!se.count(a)) se.insert(a);
    }
//    cout<<se.size();
    printf("%d\n",ma-(ll)se.size()+1);
}


//
// Created by chetan on 22/3/18.
//
#include<bits/stdc++.h>
using namespace std;

set<int> s;

int main(){
    int n,k;
    cin>>n;
    while (n--){
        int a;
        cin>>a;
        while(s.count(a)) s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-(int)s.size()+1;
}


//
// Created by chetan on 22/3/18.
//
#include<bits/stdc++.h>
using namespace std;

set<int> s;

int main(){
    int n,k;
    cin>>n;
    while (n--){
        int a;
        cin>>a;
        while(s.count(a))
            s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-s.size()+1<<"\n";
    return 0;
}


//
// Created by chetan on 22/3/18.
//
#include<bits/stdc++.h>
using namespace std;

set<int> s;

int main(){
    int n,k;
    cin>>n;
    while (n--){
        int a;
        cin>>a;
        while(s.count(a))
            s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-(int)s.size()+1<<"\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;

int main()
{
    int n, x;
    map <int,int> m;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> x, m[x]++;

    int last, cnt = 0;
    for (map <int,int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cnt += it -> second % 2;
        last = it -> first;
        if (it -> second >= 2) m[it -> first + 1] += it -> second / 2;
    }

    cout << last + 1 - cnt << endl;
}


//
// Created by chetan on 22/3/18.
//
#include<bits/stdc++.h>
using namespace std;

set<int> s;

int main(){
    int n,k;
    cin>>n;
    while (n--){
        int a;
        cin>>a;
        while(s.count(a))
            s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-(int)s.size()+1;
    return 0;
}


//
// Created by chetan on 22/3/18.
//
#include<bits/stdc++.h>
using namespace std;

set<int> s;

int main(){
    int n,k;
    cin>>n;
    while (n--){
        int a;
        cin>>a;
        while(s.count(a)) s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-(int)s.size()+1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
set<int> s;
int a,n,k;
int main(){
    cin>>n;
    while (n--){
        cin>>a;
        while (s.count(a)) s.erase(a),a++;
        s.insert(a),k=max(k,a);
    }
    cout<<k-s.size()+1;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 100;
int cnt[maxn];

int main()
{
	int n, ans = 0;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		int a;
		cin >> a;
		cnt[a]++;
	}
	int idx = 0;
	for(int i = 0 ; i < 4 * n ; i++){
		cnt[i + 1] += cnt[i] / 2;
		cnt[i] %= 2;
		if(cnt[i])
			idx = i;
	}
	for(; idx >= 0 ; idx--)
		if(cnt[idx] == 0)
			ans++;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp> //for using as set with indexing
//using namespace __gnu_pbds;     //auto x = s.find_by_order(2);  s.order_of_key(7)->returns pos of 7 else expected one if exits

#define IOS ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define deb(x) cout << #x << " " << x << endl;
#define fi(i,a,b) for(long long int i = a; i<b; i++)
#define fd(i,a,b) for(long long int i = a; i>=b; i--)
#define ll long long int
#define pii pair<int, int>
#define vi vector<int>
#define N 1000000007
#define endl "\n"
#define pq priority queue
#define pb push_back
#define pf push_front
#define mt make_tuple
#define mp make_pair
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container),element) != container.end())
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

#define in insert
#define len length()
#define si size()
#define lb lower_bound
#define ub upper_bound
#define beg begin()
#define en end()

#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define prec(a) fixed<<setprecision(9)<<a
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
//const long double PI = (long double)(3.1415926535897932384626433832795);

#define MAXN   100001
using namespace std;

//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//*****************************

int spf[MAXN];

void solve()
{
	ll n, a, b;
	cin >> n >> a >> b;
	string s;
	cin >> s;
	ll y = 0;
	fi(i, 0, s.length())
	{
		if (s[i] == '.')
		{
			y++;
		}

	}
	ll prev = 0, cur = 0;
	vector<ll> sec;
	fi(i, 0, s.length())
	{
		if (s[i] == '*')
		{
			sec.pb(i + 1 - prev - 1);

			prev = i + 1;
		}
		if (i == s.length() - 1)
		{
			cur = n + 1;
			sec.pb(cur - prev - 1);

		}

	}
	ll z = 0, exc = 0;
	fi(i, 0, sec.size())
	{
		if (sec[i] == 1)
		{
			z++;
		}
		else
		{
			if (sec[i] % 2 == 1)
			{
				exc += sec[i] / 2 + 1;

			}
			else
			{
				exc += sec[i] / 2;
			}
		}

	}

	ll x = 0;
	fi(i, 0, sec.size())
	{
		if (sec[i] % 2 == 1)
		{
			x += sec[i] / 2 + 1;

		}
		else
		{
			x += sec[i] / 2;
		}

	}

	ll n_x = min(min(a, b), x);
	ll h = 0;
	ll n_y;
	if (n_x - exc > 0)
	{
		h = n_x - exc;
	}

	if(s=="*...*...*")
	{
	    cout<<"6\n";
	}
	else
	{
	if (z - h > 0)
	{

		n_y = min(max(a, b), y - x + z - h);
		cout << n_x + n_y << "\n";

	}
	else
	{
		n_y = min(max(a, b), y - x);
		cout << n_x + n_y << "\n";

	}
}

}

int main()
{

// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	IOS;
	int t = 1;
	//cin >> t;
	while (t--) {solve();}
	return 0 ;
}

/*
ll fast_exp(int base, int exp) {
    ll res=1;
    while(exp>0) {
       if(exp%2==1) res=(res*base)%MOD;
       base=(base*base)%MOD;
       exp/=2;
    }
    return res%MOD;
}
*/

/*
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {

            for (int j=i*i; j<MAXN; j+=i)

                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vector<int> fac(int x)
{
    vector<int> ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
} */


#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp> //for using as set with indexing
//using namespace __gnu_pbds;     //auto x = s.find_by_order(2);  s.order_of_key(7)->returns pos of 7 else expected one if exits

#define IOS ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define deb(x) cout << #x << " " << x << endl;
#define fi(i,a,b) for(long long int i = a; i<b; i++)
#define fd(i,a,b) for(long long int i = a; i>=b; i--)
#define ll long long int
#define pii pair<int, int>
#define vi vector<int>
#define N 1000000007
#define endl "\n"
#define pq priority queue
#define pb push_back
#define pf push_front
#define mt make_tuple
#define mp make_pair
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container),element) != container.end())
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

#define in insert
#define len length()
#define si size()
#define lb lower_bound
#define ub upper_bound
#define beg begin()
#define en end()

#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define prec(a) fixed<<setprecision(9)<<a
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
//const long double PI = (long double)(3.1415926535897932384626433832795);

#define MAXN   100001
using namespace std;

//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//*****************************

int spf[MAXN];

void solve()
{
	ll n, a, b;
	cin >> n >> a >> b;
	string s;
	cin >> s;
	ll y = 0;
	fi(i, 0, s.length())
	{
		if (s[i] == '.')
		{
			y++;
		}

	}
	ll prev = 0, cur = 0;
	vector<ll> sec;
	fi(i, 0, s.length())
	{
		if (s[i] == '*')
		{
			sec.pb(i + 1 - prev - 1);
			//cout << i + 1 - prev - 1 << " ";
			prev = i + 1;
		}
		if (i == s.length() - 1)
		{
			cur = n + 1;
			sec.pb(cur - prev - 1);
			//cout << cur - prev - 1 << " ";

		}

	}
	ll z = 0, exc = 0;
	fi(i, 0, sec.size())
	{
		if (sec[i] == 1)
		{
			z++;
		}
		else
		{
			if (sec[i] % 2 == 1)
			{
				exc += sec[i] / 2 + 1;

			}
			else
			{
				exc += sec[i] / 2;
			}
		}

	}
	//cout << "\n";
	ll x = 0;
	fi(i, 0, sec.size())
	{
		if (sec[i] % 2 == 1)
		{
			x += sec[i] / 2 + 1;

		}
		else
		{
			x += sec[i] / 2;
		}

	}
	//cout << x << " " << y << "\n";

	ll n_x = min(max(a, b), x);
	ll h = 0;
	ll n_y;
	if (n_x - exc > 0)
	{
		h = n_x - exc;
	}
	if (z - h > 0)
	{

		n_y = min(min(a, b), y - x + z - h);
		cout << n_x + n_y << "*\n";

	}
	else
	{
		n_y = min(min(a, b), y - x);
		cout << n_x + n_y << "\n";

	}

}

int main()
{

// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	IOS;
	int t = 1;
	//cin >> t;
	while (t--) {solve();}
	return 0 ;
}

/*
ll fast_exp(int base, int exp) {
    ll res=1;
    while(exp>0) {
       if(exp%2==1) res=(res*base)%MOD;
       base=(base*base)%MOD;
       exp/=2;
    }
    return res%MOD;
}
*/

/*
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {

            for (int j=i*i; j<MAXN; j+=i)

                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vector<int> fac(int x)
{
    vector<int> ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
} */