program folia
  implicit none
  integer(8), parameter :: maxa = 100000000
  integer :: n, i, k = 1e9
  integer(8) :: a(0:100001) = 0, p(0:100001) = 0, x = 0, m = 0
  read(*,*) n
  read(*,*) a(0:n)
  p(0) = 1
  do i = 1, n
    p(i) = (p(i - 1) - a(i - 1)) * 2
    if (a(i) > p(i)) then
      write(*,'(i0)') -1
      stop
    end if
    if (p(i) > maxa) then
      k = i
      exit
    end if
  end do
  do i = n, k, -1
    m = m + a(i)
    x = x + m
  end do
  do i = min(n, k - 1), 0, -1
    m = min(m + a(i), p(i))
    x = x + m
  end do
  write(*,'(i0)') x
end program folia