program abc135c
   implicit none
   integer(8) :: n, i, sum_attacker
   integer(8), allocatable :: a(:), b(:)

   read(*,*) n
   allocate(a(n+1), b(n))

   read(*,*) a(1:n+1)
   read(*,*) b(1:n) ! Brave

   sum_attacker = sum(a)

   do i = 1, n
      if (a(i)>=b(i)) then
         a(i) = a(i) - b(i)
         b(i) = 0
      else if (a(i) < b(i)) then
         b(i) = b(i) - a(i)
         a(i) = 0
         a(i+1) = max(a(i+1)-b(i), 0)
      end if
!      write(*,*) i, 'remain attacker:', a(:)
   end do

!   write(*,*) 'remain attacker:', a(:)

   write(*,*) sum_attacker - sum(a)
end program abc135c
