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