program sample
    implicit none
  
    integer(8) :: i,j,m,n,a,b,c
    
    integer(8),allocatable :: x(:),y(:)
  
    read(*,*) n
    allocate(x(n))
    allocate(y(n))
    read(*,*)x
    read(*,*)y
    a=0 !aは上の道の差
    c=X(1)
    do i=1,n-1
    
    if (x(i+1)+a >y(i) )then
        a=0
        b=i+1
        c=c+a+x(i+1)
    else
        a=x(i+1)-y(i) +a
        C=C+y(i)
    end if
    c=c+y(n)
    end do

    write(*,*) c
  
    stop
end program sample
  

