program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,j,asum,pmax,ans=0
    integer(int64), allocatable:: a(:)

    read*, n
    allocate(a(0:n))
    read*, a(:)
    asum = sum(a)
    pmax = 1
    i=0
    do while(pmax < asum)
        ans = ans + min(pmax, asum)
        if (a(i) > pmax) then
            print'(i0)', -1
            stop
        end if
        asum=asum-a(i)
        pmax=(pmax-a(i))*2
        i=i+1
    end do

    do j=i,n
        ans = ans + asum
        asum=asum-a(j)
    end do
    print'(i0)', ans
end program main