#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
const int mod = 1e9 + 9;
const int mx = 3e5 + 5;
int ans = 0;
signed main() {
	int n;	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<int> b(n);
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	set<int> st;
	for (int i = 0; i < n; i++) {
		st.insert(i);
	}
	for (int i = n - 1; i >= 0; i--) {
		auto x = st.upper_bound(i);
		while (x != st.end()) {
			int xx = *x;
			int mn = min(a[i], b[*x]);
			a[i] -= mn;
			b[*x] -= mn;
			ans += mn;
			x++;
			if (a[i] == 0) break;
		}
	}
	for (int i = 0; i < n; i++ ) {
		int t = min(a[i], b[i]);
		a[i] -= t;	b[i] -= t;
	}
	for (int i = 0; i < n; i++) {
		if (b[i] > 0) {
			ans -= b[i];
		}
	}
	cout << ans << "\n";
	return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |