#include<bits/stdc++.h>
using namespace std;
long long ans=0;
long long a[100010],b[100010],d=0,di=0,di1=-1;
int main(){
freopen("buy.in","r",stdin);
freopen("buy.out","w",stdout);
long long n;
scanf("%d",&n);
for(long long i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(long long i=0;i<n;i++){
scanf("%d",&b[i]);
}
while(di!=n-1){
d=0;
for(long long i=di1+1;i<n;i++){
if(b[i]>d){
d=b[i];
di=i;
}
}
for(long long i=di1+1;i<=di;i++){
b[i]=-1;
if(a[i]<d){
ans+=d-a[i];
}
}
di1=di;
}
cout<<ans;
return 0;
}