program NOMURA2020C
implicit none
  integer(8)::N,i
  integer(8),allocatable::A(:),k(:),l(:)
  read(*,*)N
  allocate(A(0:N),k(0:N),l(0:N))
  read(*,*)A
  if(A(0)==0_8) then
     k(0)=1_8
  else
     k(0)=0_8
  end if
  do i=1,N
     k(i)=k(i-1_8)*2_8-A(i)
     if((k(i)<=0_8 .and. i<N) .or. (k(i)<0_8 .and. i==N)) then
        write(*,*)'-1'
        stop
     end if
  end do
  l(N)=A(N)
  if(l(N)>k(N)+A(N)) then
     write(*,*)'-1'
     stop
     end if
  do i=N-1_8,0_8,-1_8
     l(i)=l(i+1)+A(i)
     if(l(i)>k(i)+A(i)) l(i)=k(i)+A(i)
  end do
  write(*,*) sum(l)
end program NOMURA2020C