#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];
}
for (int i = 0; i < n; ++i) {
cin >> y[i];
}
int ans = 0;
spots.emplace(x[0], 0);
for (int i = 1; i < n; ++i) {
spots.emplace(x[i], i);
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);
}
}
spots.push(curr);
if (y[i] > 0 && spots.top().first > 0) {
int x = min(spots.top().first, y[i]);
spots.top().first -= x;
y[i] -= x;
}
}
if (!spots.empty()) {
while (!spots.empty() && spots.top().second > 0) {
spots.pop();
}
if (!spots.empty() && y[0] > 0 && spots.top().first > 0) {
int x = min(spots.top().first, y[0]);
y[0] = x;
}
}
for (int i = 0; i < n; ++i) {
ans -= y[i];
}
cout << ans << '\n';
return 0;
}