program folia
  implicit none
  integer :: n, i
  integer(8) :: a(0:100001) = 0, p(0:60) = 0, r, m = 0
  read(*,*) n
  read(*,*) a(0:n)
  p = (/(ibset(0_8, i), i = 0, 60)/)
  do i = min(60, n), 0, -1
    r = (a(i + 1) + 1) / 2 + a(i)
    if (r > p(i)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  r = 0
  do i = n, 61, -1
    m = m + a(i)
    r = r + m
  end do
  do i = min(n, 60), 1, -1
    m = min(m + a(i), (p(i - 1) - a(i - 1)) * 2)
    r = r + m
  end do
  write(*,'(i0)') r + 1
end program folia