#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 3e5 + 5;
int x[maxn], y[maxn];
void solve() {
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> x[i];
for(int i = 1; i <= n; i++) cin >> y[i];
int ans = 0;
set<int> idleft;
idleft.insert(1);
for(int i = 2; i <= n; i++){
while(idleft.size() && y[i]){
int pos = *idleft.rbegin();
if(x[pos] > y[i]){
x[pos] -= y[i];
ans += y[i];
y[i] = 0;
}
else{
idleft.erase(idleft.find(pos));
y[i] -= x[pos];
ans += x[pos];
x[pos] = 0;
}
}
idleft.insert(i);
}
for(int i = 1; i <= n; i++){
int k = min(x[i], y[i]);
x[i] -= k;
y[i] -= k;
ans -= y[i];
}
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
}
# | 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... |