// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on

void main()
{
    long N = lread();
    auto A = aryread();

    alias T = Tuple!(long, long);

    T[] solve(long depth)
    {
        if (depth == N + 1)
            return [T(0, 0)];
        auto B = solve(depth + 1);
        dprint(depth, B);
        T[] ret;
        foreach (i; 0 .. B.length)
        {
            long b = B[i][0];
            long c = (b + 1) / 2;
            long a = A[depth];
            if (l(b + a) <= depth)
                ret ~= T(b + a, B[i][1] + b + a);
            if (l(c + a) <= depth)
                ret ~= T(c + a, B[i][1] + c + a);
        }
        return ret.sort().uniq().array();
    }

    auto ans = solve(0);

    if (ans.length == 0)
    {
        writeln(-1);
        return;
    }

    writeln(ans.map!"a[1]"().reduce!max);
}

long l(long x)
{
    long ret;
    while (1 < x)
        ret++, x = (x + 1) / 2;
    return ret;
}
