#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>
ll n;
vl x, y;
void f() {
cin >> n;
x.resize(n);
y.resize(n);
for(auto& u : x) cin >> u;
for(auto& u : y) cin >> u;
ll sve=0, ans=0, give, rem=0;
vl v(n, 0);
for(auto& u : y) rem+=u;
rep(i, 0, n){
give=min(sve, y[i]);
ans+=give;
sve-=give;
y[i]-=give;
rem-=give;
if(i) v[i-1]+=give;
sve+=x[i];
}
rrep(i, n-1, 0){
give=min(v[i], x[i]);
x[i]-=give;
v[i]-=give;
if(i) v[i-1]+=v[i];
give=min(y[i], x[i]);
x[i]-=give;
y[i]-=give;
rem-=give;
}
cout << ans-rem;
}
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... |