#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int n, sy, x[MAXN], y[MAXN], tx[MAXN], ty[MAXN];
inline int cmp(const int& t, const int&s){
return (t < s) - (s < t);
}
int f(int d){
memcpy(tx, x, n*sizeof(int));
memcpy(ty, y, n*sizeof(int));
int* xp = tx + n - 1, *yp = ty;
int s = 0, ret = 0;
//cerr << d << '\n';
while(s < d){
int tk, cx = xp - tx, cy = yp - ty;
if(s + min(*xp, *yp) >= d){
tk = d-s;
*xp -= tk;
*yp -= tk;
}
else{
if(*xp < *yp){
tk = *xp;
*yp -= tk;
*xp = 0;
xp--;
}
else if(*yp < *xp){
tk = *yp;
*xp -= tk;
*yp = 0;
yp++;
}
else{
tk = *xp;
*xp = 0;
*yp = 0;
xp--; yp++;
}
}
s += tk;
//cerr << cx << ' ' << cy << '\n';
ret += tk * cmp(cx, cy);
}
/*for(int i=0; i<n; i++){
cerr << tx[i] << ' ';
}
cerr << '\n';
for(int i=0; i<n; i++){
cerr << ty[i] << ' ';
}
cerr << '\n';
cerr << xp - tx << ' ' << yp - ty << '\n';
cerr << ret << '\n';*/
xp = tx;
while(s < sy){
int tk, cx = xp - tx, cy = yp - ty;
if(s + min(*xp, *yp) >= sy){
tk = sy-s;
*xp -= tk;
*yp -= tk;
}
else{
if(*xp < *yp){
tk = *xp;
*yp -= tk;
*xp = 0;
xp++;
}
else if(*yp < *xp){
tk = *yp;
*xp -= tk;
*yp = 0;
yp++;
}
else{
tk = *xp;
*xp = 0;
*yp = 0;
xp++; yp++;
}
}
s += tk;
ret += tk * cmp(cx, cy);
}
/*for(int i=0; i<n; i++){
cerr << tx[i] << ' ';
}
cerr << '\n';
for(int i=0; i<n; i++){
cerr << ty[i] << ' ';
}
cerr << '\n';
cerr << xp - tx << ' ' << yp - ty << '\n';
cerr << ret << '\n';*/
return ret;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
int sx = 0;
for(int i=0; i<n; i++){
cin >> x[i];
}
for(int i=0; i<n; i++){
cin >> y[i];
sy += y[i];
}
for(int i=0; i<n; i++){
if(sy == sx){
x[i] = 0;
}
else if(sx + x[i] >= sy){
x[i] = sy - sx;
sx = sy;
}
else{
sx += x[i];
}
}
int l = 0, r = sy, m;
while(r - l >= 3){
int m1 = l + (r - l) / 3;
int m2 = r - (r - l) / 3;
int f1 = f(m1);
int f2 = f(m2);
if(f1 < f2){
l = m1;
}
else if(f1 > f2){
r = m2;
}
else{
l = m1;
r = m2;
}
}
int ans = INT_MIN;
for(int i=l; i<=r; i++){
ans = max(ans, f(i));
}
cout << ans;
return 0;
}