#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
 

#define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)

#include <vector>
#include <utility>


template<typename T, typename U> struct seg_tree_lazy {
    int S, H;
 
    T zero;
    vector<T> value;
 
    U noop;
    vector<bool> dirty;
    vector<U> prop;
 
    seg_tree_lazy<T, U>(int _S, T _zero = T(), U _noop = U()) {
        zero = _zero, noop = _noop;
        for (S = 1, H = 1; S < _S; ) S *= 2, H++;
 
        value.resize(2*S, zero);
        dirty.resize(2*S, false);
        prop.resize(2*S, noop);
    }
 
    void set_leaves(vector<T> &leaves) {
        copy(leaves.begin(), leaves.end(), value.begin() + S);
 
        for (int i = S - 1; i > 0; i--)
            value[i] = value[2 * i] + value[2 * i + 1];
    }
 
    void apply(int i, U &update) {
        value[i] = update(value[i]);
        if(i < S) {
            prop[i] = prop[i] + update;
            dirty[i] = true;
        }
    }
 
    void rebuild(int i) {
        for (int l = i/2; l; l /= 2) {
            T combined = value[2*l] + value[2*l+1];
            value[l] = prop[l](combined);
        }
    }
 
    void propagate(int i) {
        for (int h = H; h > 0; h--) {
            int l = i >> h;
 
            if (dirty[l]) {
                apply(2*l, prop[l]);
                apply(2*l+1, prop[l]);
 
                prop[l] = noop;
                dirty[l] = false;
            }
        }
    }
 
    void upd(int i, int j, U update) {
        i += S, j += S;
        propagate(i), propagate(j);
 
        for (int l = i, r = j; l <= r; l /= 2, r /= 2) {
            if((l&1) == 1) apply(l++, update);
            if((r&1) == 0) apply(r--, update);
        }
 
        rebuild(i), rebuild(j);
    }
 
    T query(int i, int j){
        i += S, j += S;
        propagate(i), propagate(j);
 
        T res_left = zero, res_right = zero;
        for(; i <= j; i /= 2, j /= 2){
            if((i&1) == 1) res_left = res_left + value[i++];
            if((j&1) == 0) res_right = value[j--] + res_right;
        }
        return res_left + res_right;
    }
};


#include <algorithm>


long long a[110000];
long long b[110000];
long long Sa[110000];
long long Sb[110000];
long long diff[110000];

struct node {
    long long big;
    int idx_big;

    long long min_next;
    int idx_min;

    long long global_min_next;
    int idx_global;
    
    node operator+(const node &n) {
    	node ret;

    	ret.global_min_next = min(global_min_next, n.global_min_next);
    	ret.idx_global = (ret.global_min_next == global_min_next ? idx_global : n.idx_global);

    	if (n.big > big) {
    		ret.big = n.big;
    		ret.idx_big = n.idx_big;
    		ret.min_next = n.min_next;
    		ret.idx_min = n.idx_min;
    	}
    	else {
    		ret.big = big;
    		ret.idx_big = idx_big;
    		ret.min_next = min(min_next, n.global_min_next);
    		ret.idx_min = (ret.min_next == min_next ? idx_min : n.idx_global);
    	}

        return ret;
    }    
};

struct update {
    long long value_b;
    long long value_n;

    node operator()(const node &n) {
        return {n.big + value_b, n.idx_big, n.min_next + value_n, n.idx_min, n.global_min_next + value_n, n.idx_global};
    }

    update operator+(const update &u) {
        return { value_b + u.value_b, value_n + u.value_n };
    }
};


void solve() {
	int n;
	scanf("%d", &n);

	long long ans = 0;

	REP(i,n) scanf("%lld", &a[i]);
	REP(i,n) scanf("%lld", &b[i]);

	REP(i,n) {
		Sa[i+1] = Sa[i] + a[i];
		Sb[i+1] = Sb[i] + b[i];
		
		diff[i+1] = Sa[i+1] - Sb[i+1];
		ans += (diff[i+1]<0 ? -diff[i+1] : diff[i+1]);
	}

	seg_tree_lazy<node, update> st(n, {-1000000000000000000LL,0,1000000000000000000LL,0,1000000000000000000LL,0}, {0, 0});
	vector<node> leaves(n);

	int bb = 0;
	for (int i = n-1; i >= 0; i--) {
		if (diff[i+1] > 0) bb++;
		else bb--;

		leaves[i].big = bb;
		leaves[i].idx_big = i;
		if (diff[i+1] > 0) leaves[i].min_next = diff[i+1];
		else leaves[i].min_next = 1000000000000000000LL;
		leaves[i].idx_min = leaves[i].idx_global = i;
		leaves[i].global_min_next = leaves[i].min_next;
	}
	st.set_leaves(leaves);

	long long need = diff[n];

	while (need > 0) {
		node qry = st.query(0, n-1);

		int where = qry.idx_big;
		long long how_many = min(need, qry.min_next);
		b[where] += how_many;

		//printf("at = %d hm = %lld (%d) big = %lld need = %lld\n", qry.idx_big, how_many, qry.idx_min, qry.big, need);
		ans -= qry.big * how_many;
		need -= how_many;

		vector<int> leaves;
		while (qry.min_next == how_many) {
			leaves.push_back(qry.idx_min);
			st.upd(qry.idx_min, qry.idx_min, {0, 1000000000000000000LL - how_many});
			qry = st.query(0, n-1);
		}

		for (int x : leaves) {
			st.upd(0, x, {-2, 0});
		}
		st.upd(where, n-1, {0, -how_many});
	}

	//REP(i,n) printf("%lld ", b[i]);
	//printf("\n");
	printf("%lld\n", ans);
}


int main() {
    solve();
}




Measured memory_bytes: 25216