#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n;
int a[N],b[N];
signed main(){
freopen("buy.in","r",stdin);
freopen("buy.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
int res=0;
priority_queue<int,vector<int>,greater<int>> q;
for(int i=1;i<=n;i++){
q.push(a[i]);
if(q.top()<b[i]){
res+=b[i]-q.top();
q.pop();
q.push(b[i]);
}
}
cout<<res;
return 0;
}