#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
signed main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n; cin>>n;
int a[n],b[n];
for(int i=0;i<n;i++)cin>>b[i];
for(int i=0;i<n;i++)cin>>a[i];
int ans=0;
stack<pii> s;
for(int i=0;i<n;i++) {
while(!s.empty() && a[i]>0) {
pii t=s.top();
int temp=min(t.second,a[i]);
a[i]-=temp;
b[t.first]-=temp;
ans+=temp;
s.pop();
if(b[t.first]!=0)s.push({t.first,b[t.first]});
}
s.push({i,b[i]});
}
/*cout<<ans<<"\n";
for(int i=0;i<n;i++)cout<<a[i]<<" ";
cout<<"\n";
for(int i=0;i<n;i++)cout<<b[i]<<" ";
cout<<"\n";*/
for(int i=0;i<n;i++) {
int temp=min(a[i],b[i]);
a[i]-=temp;
b[i]-=temp;
}
for(int i=0;i<n;i++) {
ans-=a[i];
}
cout<<ans;
}