#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++){
cin >> x[i];
}
for (int i = 0; i < n; i++){
cin >> y[i];
}
int sum = 0;
for (int i = 0; i < n; i++){
sum += y[i];
}
int ans = -INT_MAX;
// it will decrease, then increase.
auto f = [&](int bully) -> int{
int cur = 0;
// assign the bullied to the worst position
// assign the rest to the best position
int cbully = bully;
int rest = sum - bully;
vector<int> tx = x;
vector<int> ty = y;
vector<int> nty = ty;
int tk = bully;
for (int i = 0; i < n; i++){
int take = min(tk, nty[i]);
tk -= take;
nty[i] -= take;
}
int l = 0;
int tkn = 0;
int i = 0, j = 0;
// these people have tier i
// l/r is the thing they are given
// tx is the free spots
// ty is the people taking
while (i < n && j < n){
if (!rest){
if (!cbully){
break;
}
int take = min(tx[l], min(cbully, ty[j]));
//cout << i << "take bully " << take << '\n';
if (j < l){
cur -= take;
}
else if (j > l){
cur += take;
}
tx[l] -= take;
cbully -= take;
ty[j] -= take;
if (tx[l] == 0){
l++;
}
if (ty[j] == 0){
j++;
}
continue;
}
int take = min(tx[l], min(rest, nty[i]));
//cout << i << "take rest " << take << '\n';
if (i < l){
cur -= take;
}
else if (i > l){
cur += take;
}
tx[l] -= take;
rest -= take;
nty[i] -= take;
if (tx[l] == 0){
l++;
}
if (nty[i] == 0){
i++;
}
}
return cur;
};
int prev = -INT_MAX;
for (int i = 0;; i++){
int cur = f(i);
if (cur > prev){
cout << max(cur, f(sum)) << '\n';
return 0;
}
prev = cur;
}
for (int i = 0; i <= sum; i++){
cout << f(i) << '\n';
}
int lo = 0;
int hi = sum;
// find first point where f(x) > f(x + 1)
// FFFFFFTTTTT
while (lo < hi){
int mid = (lo + hi) >> 1;
if (f(mid) >= f(mid + 1)){
hi = mid;
}
else{
lo = mid + 1;
}
}
cout << max(f(lo), f(sum)) << '\n';
}