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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

const int INF=1001001001001001001ll;

int N;
int A[111111],B[111111];

signed main(){
	scanf("%lld",&N);
	rep(i,N)scanf("%lld",&A[i]);
	rep(i,N)scanf("%lld",&B[i]);


	priority_queue<int,vint,greater<int>>que;
	que.push(0);
	int b=0;
	int h=0;


	int num=0;
	rep(i,N){
		if(i){
			if(num>0){que.push(-b);que.push(-b);}
			if(que.size()>1)que.pop();
			
			h+=-(que.top()+b);
		}

		int s=A[i]-B[i];
		if(s>0){
			int tmp=que.top();
			if(que.size()>1)que.pop();
		
			que.push(tmp-s);
			b+=s;
		}
		else if(s<0){
			b-=(-s);
		}
		num+=s;
	}


	int cur=que.top()+b;
	int r=-1;
	que.push(-b);
	while(cur<0){
		int x=que.top();
		que.pop();
		h+=(x+b-cur)*r;
		r++;
		cur=x+b;
	}
	cout<<h<<endl;
	return 0;
}