#include <bits/stdc++.h>
#ifdef LOCAL
#include "Debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
while (cin >> n)
{
map<int, int> a, b;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
if (x)
a[i] = x;
}
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
if (x)
b[i] = x;
}
int ans = 0;
for (int i = n - 1; i >= 0; i--)
if (b.count(i))
{
while (1)
{
auto u = a.lower_bound(i);
if (u == begin(a))
break;
u--;
auto v = b.upper_bound(u->first);
if (v == end(b))
break;
int use = min(u->second, v->second);
ans += use;
a[u->first] -= use;
if (a[u->first] == 0)
a.erase(u->first);
b[v->first] -= use;
if (b[v->first] == 0)
b.erase(v->first);
}
}
for (int i = 0; i < n; i++)
ans -= max(0, b[i] - a[i]);
cout << ans << 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... |