#include <bits/stdc++.h>
#define pb push_back
#define SS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define int long long
#define all(v) v.begin(),v.end()
using namespace std;
const int N = 1e4, inf = 1e9 + 1, mod = 998244353;
void solve(){
int n;
cin >> n;
int x[n + 1], y[n + 1];
for(int i = 1; i <= n; i++){
cin >> x[i];
}
for(int i = 1; i <= n; i++){
cin >> y[i];
}
int free = 0, sat = 0, hap = 0, bad = 0;
for(int i = 1; i <= n; i++){
hap += min(free, y[i]);
if(free > y[i]){
free -= y[i];
y[i] = 0;
}
else{
y[i] -= free;
free = 0;
}
if(y[i] > 0){
hap += min(sat, y[i]);
bad += min(sat, y[i]);
if(sat > y[i]){
sat -= y[i];
y[i] = 0;
}
else{
y[i] -= sat;
sat = 0;
}
}
if(y[i] > 0){
sat += max(x[i], y[i]);
if(y[i] > x[i]){
y[i] -= x[i];
x[i] = 0;
}
else{
x[i] -= y[i];
y[i] = 0;
}
}
bad += y[i];
free += x[i];
}
cout << hap - bad;
}
signed main(){
SS
// freopen("trains.in", "r", stdin);
// freopen("trains.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
| # | 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... |