#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
cin >> b[i];
}
ll ans = 0;
int x = n - 1;
for(int j = n - 1; j >= 1; j--) {
x = min(j, x);
while(x - 1 >= 0 && b[j] > 0) {
x--;
if(a[x] >= b[j]) {
ans += b[j];
a[x] -= b[j];
b[j] = 0;
} else {
ans += a[x];
b[j] -= a[x];
a[x] = 0;
}
}
}
x = 0;
for(int i = 0; i < n; i++) {
b[i] -= min(b[i], a[i]);
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... |