//pB
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 3e5+10;
int n;
LL ans=0;
LL x[maxn], y[maxn];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=0; i<n; i++) cin>>y[i]; // 房間
for(int i=0; i<n; i++) cin>>x[i]; // 人
stack<int> st;
for(int i=0; i<n; i++){
while(!st.empty() and x[i]){
int t=st.top();
LL mi = min(y[t], x[i]);
ans += mi;
x[i] -= mi;
y[t] -= mi;
if(y[t] == 0) st.pop();
}
st.push(i);
}
for(int i=n-1; i>=0; i--){
if(x[i]==0) continue;
if(y[i]){
LL mi = min(y[i], x[i]);
x[i] -= mi;
y[i] -= mi;
}
ans-=x[i];
}
cout<<ans;
return 0;
}