#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pb push_back
#define sz(a) (ll) a.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(ll i=a; i<b; i++)
#define rrep(i, a, b) for(ll i=a; i>=b; i--)
#define vl vector<ll>
#define vpll vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define pll pair<ll, ll>
void f() {
ll n; cin >> n;
vl x(n), y(n);
ll rem=0;
for(auto& u : x) cin >> u;
for(auto& u : y){ cin >> u; rem+=u; }
ll curr=0, up=y[0], wait=0, ans=0;
rep(i, 0, n){
wait+=curr;
curr=up;
if(i!=n-1) up=y[i+1];
else up=0;
ll give=min(up, x[i]);
ans+=give;
x[i]-=give;
up-=give;
rem-=give;
give=min(curr, x[i]);
x[i]-=give;
curr-=give;
rem-=give;
give=min(wait, x[i]);
x[i]-=give;
wait-=give;
rem-=give;
ans-=give;
ll j=i+2;
while(x[i] && rem){
give=min(y[j], x[i]);
y[j]-=give;
x[i]-=give;
rem-=give;
ans+=give;
j++;
}
}
cout << ans << endl;
}
int main() {
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; i++) {
// cout << '#' << i << endl;
f();
cout << endl;
}
}
# | 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... |