#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
signed main() {
int n;
cin >> n;
vector<int> x(n, 0), y(n, 0);
stack<ii> spots;
for (int i = 0; i < n; ++i) {
cin >> x[i];
spots.emplace(x[i], i);
}
for (int i = 0; i < n; ++i) {
cin >> y[i];
}
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
while (!spots.empty() && spots.top().second > i) {
spots.pop();
}
if (spots.empty()) {
break;
}
auto curr = spots.top();
spots.pop();
while (y[i] > 0 && !spots.empty()) {
if (spots.top().first <= y[i]) {
ans += spots.top().first;
y[i] -= spots.top().first;
spots.pop();
} else {
auto tmp = spots.top();
spots.pop();
tmp.first -= y[i];
ans += y[i];
y[i] = 0;
spots.push(tmp);
}
}
if (y[i] > 0) {
curr.first -= y[i];
y[i] = 0;
}
spots.push(curr);
}
for (int i = 0; i < n; ++i) {
ans -= y[i];
}
cout << ans << '\n';
return 0;
}