#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <string>
#include <queue>
#include <unordered_set>
#include <deque>
#include <numeric>
#include <cmath>
#include <unordered_map>
#include <set>
#include <stack>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<ll> cap(n), bike(n);
for (int i = 0; i < n; ++i) {
cin >> cap[i];
}
for (int i = 0; i < n; ++i) {
cin >> bike[i];
}
ll ans = 0;
stack<ll> notfull;
notfull.push(0);
for (int i = 1; i < n; ++i) {
while (!notfull.empty() && bike[i] >= cap[notfull.top()]) {
ans += cap[notfull.top()];
bike[i] -= cap[notfull.top()];
cap[notfull.top()] = 0;
notfull.pop();
}
if (!notfull.empty() && bike[i] > 0) {
ans += bike[i];
cap[notfull.top()] -= bike[i];
bike[i] = 0;
}
notfull.push(i);
}
for (int i = 0; i < n; ++i) {
bike[i] -= min(bike[i], cap[i]);
ans -= bike[i];
}
cout << ans << '\n';
}
# | 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... |