#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("0"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
using namespace std;
typedef long long int ll;
typedef pair<ll,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

// Segment tree (RMQ type)
struct seg {
  typedef ll TT;
  vector<TT> d; vp e; int x2;
  seg(){}
  seg(int mx) {
    x2 = 1; while(x2 < mx) x2 <<= 1;
    d = vl(x2<<1);
    e = vp(x2<<1);
    rep(i,x2) e[x2+i].se = i;
    drep(i,x2) {
      e[i].se = e[i<<1].se;
    }
  }
  void f(int i) {
    int l = i<<1, r = l|1;
    d[l] += d[i]; e[l].fi += d[i];
    d[r] += d[i]; e[r].fi += d[i];
    d[i] = 0;
  }
  void add(int a, int b, ll x, int i=1, int l=0, int r=-1){
    if (r == -1) r = x2;
    if(a <= l && r <= b) {
      d[i] += x;
      e[i].fi += x;
      return;
    }
    f(i);
    int c = (l+r)>>1;
    if(a < c) add(a,b,x,i<<1,l,c);
    if(c < b) add(a,b,x,i<<1|1,c,r);
    e[i] = min(e[i<<1],e[i<<1|1]);
  }
  P get(int a, int b, int i=1, int l=0, int r=-1){
    if (r == -1) r = x2;
    if(a <= l && r <= b) return e[i];
    f(i);
    int c = (l+r)>>1;
    P res(LINF,0);
    if(a < c) mins(res,get(a,b,i<<1,l,c));
    if(c < b) mins(res,get(a,b,i<<1|1,c,r));
    return res;
  }
};
//

ll a[MX], b[MX];

int main() {
  int n;
  scanf("%d",&n);
  rep(i,n) scanf("%lld",&a[i]);
  rep(i,n) {
    ll x;
    scanf("%lld",&x);
    a[i] -= x;
  }
  rep(i,n) b[i] = a[i];
  rep(i,n-1) a[i+1] += a[i];
  ll sum = a[n-1];
  ll ans = 0;
  rep(i,n) ans += abs(a[i]);
  seg s(n+5), t(n+5);
  rep(i,n) {
    s.add(0,i+1,a[i]>0?-1:1);
    if (b[i] <= 0) s.add(i,i+1,LINF);
    t.add(i,i+1,a[i]>0?a[i]:LINF);
  }
  while (sum) {
    P p = s.get(0,n);
    int i = p.se;
    P q = t.get(i,n);
    ll x = min(b[i],q.fi);
    // cerr<<p<<" "<<q<<" "<<x<<endl;
    ans += p.fi*x;
    sum -= x;
    t.add(i,n,-x);
    while (1) {
      P r = t.get(i,n);
      if (r.fi > 0) break;
      s.add(0,r.se+1,2);
      t.add(r.se,r.se+1,LINF);
    }
    b[i] -= x;
    if (b[i] <= 0) {
      s.add(i,i+1,LINF);
    }
  }
  cout<<ans<<endl;
  return 0;
}







Measured memory_bytes: 14080