#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int x[N],y[N];
int main(){
int n; cin>>n;
for(int i=1;i<=n;i++) cin>>x[i];
for(int i=1;i<=n;i++) cin>>y[i];
int sc=0;
stack<pair<int,int>> st;
for(int i=1;i<=n;i++){
while(!st.empty() && y[i]!=0){
auto [id,val]=st.top();
st.pop();
int use=min(y[i],val);
y[i]-=use,val-=use;
sc+=use;
if(val>0) st.push({id,val});
}
if(x[i]!=0) st.push({i,x[i]}),x[i]=0;
}
while(!st.empty()) x[st.top().first]=st.top().second,st.pop();
for(int i=1;i<=n;i++){
y[i]-=min(x[i],y[i]);
sc-=y[i];
}
cout<<sc;
}