#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid, int) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    while(sz < n) sz <<= 1;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  void propagate(int k, int len) {
    if(lazy[k] != OM0) {
      if(k < sz) {
        lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      }
      data[k] = g(data[k], lazy[k], len);
      lazy[k] = OM0;
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
    propagate(k, r - l);
    if(r <= a || b <= l) {
      return data[k];
    } else if(a <= l && r <= b) {
      lazy[k] = h(lazy[k], x);
      propagate(k, r - l);
      return data[k];
    } else {
      return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                         update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x) {
    return update(a, b, x, 1, 0, sz);
  }


  Monoid query(int a, int b, int k, int l, int r) {
    propagate(k, r - l);
    if(r <= a || b <= l) {
      return M1;
    } else if(a <= l && r <= b) {
      return data[k];
    } else {
      return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
               query(a, b, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid query(int a, int b) {
    return query(a, b, 1, 0, sz);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }
};


int main() {
  int N;
  int64 A[100000], B[100000];
  cin >> N;
  for(int i = 0; i < N; i++) cin >> A[i];
  for(int i = 0; i < N; i++) cin >> B[i];


  for(int i = 1; i < N; i++) A[i] += A[i - 1];
  for(int i = 1; i < N; i++) B[i] += B[i - 1];

  int64 ret = 0;
  for(int i = 0; i < N; i++) ret += llabs(B[i] - A[i]);

  int64 sa = A[N - 1] - B[N - 1];
  ret -= sa;

  using Pi = pair< int64, int >;
  auto f = [](Pi x, Pi y) { return min(x, y); };
  auto g = [](Pi x, int64 y, int len) { return Pi(x.first + y, x.second); };
  auto h = [](int64 x, int64 y) { return x + y; };
  const int64 INF = 1LL << 60;

  LazySegmentTree< Pi, int64 > rest(N - 1, f, g, h, {INF, -1}, 0);
  for(int i = 0; i < N - 1; i++) rest.set(i, {A[i] - B[i], i});
  rest.build();
  LazySegmentTree< Pi, int64 > cost(N - 1, f, g, h, {INF, -1}, 0);
  int64 sum = 0;
  for(int i = N - 2; i >= 0; i--) {
    --sum;
    cost.set(i, {sum, i});
  }
  cost.build();


  while(sa > 0) {
    Pi it;
    while((it = rest.query(0, N - 1)).first <= 0) {
      cost.update(0, it.second + 1, 2);
      rest.update(it.second, it.second + 1, INF - it.first);
    }
    auto best = cost.query(0, N - 1);
    if(best.first >= 0) break;
    int64 po = min(sa, rest.query(best.second, N - 1).first);
    rest.update(best.second, N - 1, -po);
    sa -= po;
    ret += best.first * po;
  }
  cout << ret << endl;
}



Measured memory_bytes: 14080