#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define int long long
signed main() {
int N;
cin>>N;
vector<int> slot(N+1, 0), person(N+1);
for(int i=0; i<N; i++) cin>>slot[i];
for(int i=0; i<N; i++) cin>>person[i];
int ans = 0;
queue<int> st;
for(int i=0; i<N; i++) {
while(!st.empty() && person[i]) {
int last_idx = st.front();
int take = min(slot[last_idx], person[i]);
slot[last_idx] -= take;
person[i] -= take;
ans += take;
if(slot[last_idx] == 0)
st.pop();
}
st.push(i);
}
for(int i=0; i<N; i++) {
int take = min(slot[i], person[i]);
slot[i] -= take;
person[i] -= take;
}
for(int i=0; i<N; i++) ans -= person[i];
cout<<ans;
}