program folia
  implicit none
  integer :: n, i
  integer(8), dimension(0:100001) :: a = 0, b = 0, s = 0
  read(*,*) n
  read(*,*) a(0:n)
  do i = n, 0, -1
    s(i) = s(i + 1) + a(i)
  end do
  b(0) = 1 - a(0)
  do i = 1, n
    b(i) = min(b(i - 1) * 2 - a(i), s(i + 1))
  end do
  if (any(b(0:n) < 0)) then
    write(*,'(i0)') -1
    stop
  end if
  write(*,'(i0)') s(0) + sum(b(0:n))
end program folia