program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d,ans
    integer(8),allocatable::x(:),y(:)
  
    read(*,*) n
    allocate(x(n+1),y(n))
    read(*,*)x
    read(*,*)y
    a=0
    ans=0
    do i=1,n
        if(y(i)+a<=x(i))then
            a=0
            ans=ans+y(i)+a
        elseif(a>=x(i))then
            ans=ans+x(i)
            a=y(i)
        else
            ans=ans+x(i)
            a=y(i)+a-x(i)
        end if
    end do
    if(x(n+1)>a)then
        ans=ans+a
    else
        ans=ans+x(n+1)
    end if
    write(*,*)ans
    stop
end program sample
  

