#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n;
vector<int> x, y;
int main()
{
cin >> n;
x.resize(n), y.resize(n);
for(int i = 0; i < n; i ++)
cin >> x[i];
for(int i = 0; i < n; i ++)
cin >> y[i];
int sm = 0;
for(int i = 0; i < n; i ++)
sm += y[i];
for(int i = 0; i < n; i ++)
{
x[i] = min(sm, x[i]);
sm -= x[i];
}
int ans = 0;
set<int> st;
for(int i = 0; i < n; i ++)
{
while(y[i] > 0 && st.size())
{
int j = *st.begin();
int mi = min(x[j], y[i]);
x[j] -= mi;
y[i] -= mi;
ans += mi;
if(x[j] == 0) st.erase(j);
}
if(x[i] > 0) st.insert(i);
}
for(int i = n - 1; i >= 0; i--)
{
while(y[i] > 0)
{
int j = *st.begin();
int mi = min(x[j], y[i]);
x[j] -= mi;
y[i] -= mi;
if(j > i) ans -= mi;
if(x[j] == 0) st.erase(j);
}
}
cout << ans << endl;
return 0;
}
# | 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... |