//pB
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 3e5+10;
int n, m;
LL ans=0;
LL x[maxn], y[maxn];
stack<pair<int, LL> > st;
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]; // 人
int yy=0, xx=0;
for(; yy<n and xx<n; yy++){
if(yy==xx){
st.push({xx, x[xx]});
xx++;
}
while(y[yy] and xx<n){
LL mi = min(y[yy], x[xx]);
ans += mi;
x[xx] -= mi;
y[yy] -= mi;
// cout<<xx<<" "<<yy<<" "<<mi<<"\n";
if(x[xx] == 0) xx++;
}
if(y[yy] and xx>=n) break;
}
auto[pos, num] = st.top();
if(yy==pos){
st.pop();
LL mi = min(y[yy], num);
y[yy] -= mi;
num -= mi;
if(num) st.push({pos, num});
}
while(!st.empty()){
auto[pos, num] = st.top(); st.pop();
ans-=num;
// cout<<pos<<" "<<num<<"\n";
}
/*
for(; yy<n and !st.empty();){
auto[pos, num] = st.top(); st.pop();
if(yy==pos){
LL mi = min(y[yy], num);
y[yy] -= mi;
num -= mi;
}
while(yy<n and num){
LL mi = min(y[yy], num);
ans -= mi;
num -= mi;
y[yy] -= mi;
// cout<<pos<<" "<<yy<<" -"<<mi<<"\n";
if(y[yy] == 0) yy++;
}
}*/
cout<<ans;
return 0;
}