#include<bits/stdc++.h>
using namespace std ;
const int N = 3e5+5 ;
int n , arr[N] , brr[N] , ans ;
stack<int> stk ;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) ;
cin >> n ;
for(int i=1 ; i<=n ; i++) cin >> arr[i] ;
for(int i=1 ; i<=n ; i++) cin >> brr[i] ;
for(int i=1 ; i<=n ; i++){
// brr[i]
while(brr[i] && !stk.empty()){
int k = stk.top() ; stk.pop() ;
int temp = min(arr[k],brr[i]) ;
arr[k]-=temp ; brr[i]-=temp ; ans+=temp ;
if(arr[k]) stk.push(k) ;
}
if(arr[i]) stk.push(i) ;
}
for(int i=1 ; i<=n ; i++){
if(!brr[i]) continue ;
if(arr[i]) ans-=max(0,brr[i]-arr[i]) ;
else ans-=brr[i] ;
}
cout << ans << '\n' ;
return 0 ;
}