#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define ertunt return
#define vodka void
#define sleepearly ertunt
using namespace std;
ll dist(ll a, ll b, ll n) {
ll res = max(a, b) - min(a, b);
res = min(res, 2 * n - res);
sleepearly res;
}
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
set<pair<ll, ll>> s;
ll j = 0;
for (ll i = 0; i < n; i++) {
ll b;
cin >> b;
if(b > 0)s.insert({i, b});
j+=b;
}
ll ans = 0;
for (ll i = 0; i < n; i++) {
if(s.empty())break;
auto it = s.upper_bound({i, 0});
if (it == s.begin()) continue;
it--;
pair<ll, ll> p = *it;
s.erase(it);
if (i < p.ff) break;
if (p.ss >= a[i]) {
ans += a[i];
p.ss -= a[i];
a[i] = 0;
if (p.ss > 0) s.insert(p);
} else {
ans += p.ss;
a[i] -= p.ss;
i--;
}
}
cout << ans*2-j;
}
# | 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... |