program c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans
    integer(int64), allocatable:: a(:),node(:)
    integer(int64):: ma(0:50)
    read*, n
    allocate(a(0:n), node(0:n))
    read*, a(:)
    node(n) = a(n)
    ma(0) = 1

    do i=1,min(50_8,n)
        ma(i) = max(min(2**i-a(i), 2*ma(i-1)-a(i)),0_8)
    end do
    ! print*, ma(0:min(50,n))
    do i=n-1,0,-1
        if ( i > 50) then
            node(i) = node(i+1)+a(i)
        else
            if (ma(i) < node(i+1)/2+mod(node(i+1),2_8)) then
                print'(i0)', -1
                stop
            end if
            node(i) = min(ma(i),node(i+1))+a(i)
        end if
    end do
    if (node(0) > 1) then
        print'(i0)', -1
    else
        ans=0
        do i=0,n
            ans=ans+node(i)
        end do
        print'(i0)', ans
    end if
end program c