#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 6;
int st[4*maxn+5], lazy[4*maxn+5];
void build(int id, int l, int r, vector<int> &a){
lazy[id] = -1;
if (l == r){
st[id] = a[l];
return;
}
int mid = (l + r)/2;
build(id*2, l, mid, a);
build(id*2+1, mid+1, r, a);
st[id] = st[id*2] + st[id*2+1];
}
void down(int id, int l, int r){
int mid = (l + r)/2;
if (lazy[id] != -1){
lazy[id*2] = lazy[id];
lazy[id*2+1] = lazy[id];
// set all into lazy[id]
st[id*2] = lazy[id]*(mid - l + 1);
st[id*2+1] = lazy[id]*(r - mid);
lazy[id] = -1;
}
}
int get(int id, int l, int r, int ul, int ur){
if (l > ur || r < ul) return 0;
if (ul <= l && r <= ur) return st[id];
down(id, l, r);
int mid = (l + r)/2;
return get(id*2, l, mid, ul, ur) + get(id*2+1, mid+1, r, ul, ur);
}
void update(int id, int l, int r, int ul, int ur, int val){
if (l > ur || r < ul) return;
if (ul <= l && r <= ur){
st[id] = val * (r - l + 1);
lazy[id] = val;
return;
}
down(id, l, r);
int mid = (l + r)/2;
update(id*2, l, mid, ul, ur, val);
update(id*2+1, mid+1, r, ul, ur, val);
st[id] = st[id*2] + st[id*2+1];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n+1), y(n+1);
// ignore the 0 people
// go from 1 to n-1, if there exist space for them of at least 1 tier higher (smallest possible), put them there, else ignore
// go from 0 to n-1, since they were ignored, try to fit them in their own tier
// if impossible, then put them in the lowest tier possible (can ignore and then just -1)
for (int i = 1; i <= n; i++){
cin >> x[i];
}
for (int i = 1; i <= n; i++){
cin >> y[i];
}
build(1, 1, n, x);
// need to fix nlog^2n
int ans = 0;
for (int i = 2; i <= n; i++){
// put at next availible slot
int chck = get(1, 1, n, 1, i-1);
if (chck <= y[i]){
update(1, 1, n, 1, i-1, 0);
y[i] -= chck;
ans += chck;
}
else{
int l = 1, r = i-1;
while(l < r){
int mid = (l + r + 1)/2;
int tot = get(1, 1, n, mid, i-1);
if (tot >= y[i]){
l = mid;
}
else{
r = mid - 1;
}
}
// l is the one that is not 0
if (l != i-1){
int rem1 = y[i] - get(1, 1, n, l+1, i-1);
int rem = get(1, 1, n, l, l) - rem1;
update(1, 1, n, l+1, i-1, 0);
update(1, 1, n, l, l, rem);
}
else{
int rem = get(1, 1, n, i-1, i-1) - y[i];
update(1, 1, n, i-1, i-1, rem);
}
ans += y[i];
y[i] = 0;
}
}
for (int i = 1; i <= n; i++){
x[i] = get(1, 1, n, i, i);
int fit = min(x[i], y[i]);
x[i] -= fit;
y[i] -= fit;
ans -= y[i];
}
cout << ans;
}