# pragma GCC optimize("Ofast")
# pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include<unordered_map>

#define for0(i,n) for(ll i = 0;i < (n);i++)
#define for1(i,n) for(ll i = 1;i<=(n);i++)
#define for2(i,n) for(ll i = 2;i<=(n);i++)
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
#define rep(i,st,en,cnt) for(ll i = st;i <= en;i+=cnt)
#define PB push_back
#define MP make_pair
#define fastio cin.tie(0);cout.tie(0);ios::sync_with_stdio(0)
#define F first
#define S second
#define EPS 1e-8
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f3f3f
#define NEGINF -INF
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define inp_arr(a,n) for0(i,n) {cin>>a[i];}
#define out_arr(a,n) for0(i,n) {cout<<a[i]<<" ";}

using namespace std;

typedef pair<int,int> ii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<ll> > vvl;
typedef vector<pair<long long,long long> > vll;
typedef pair<long long,pll> tll;

const ll mod = 1e9+7;

ll nop = 1e7+2;
vi primes(nop,1);

void sieve(){
    primes[1] = primes[0] = 0;
    for(ll i = 2;i * i <= nop;i++){
        if(primes[i]){
            for(ll j = i*i; j <= nop;j+=i){
                primes[j] = 0;
            }
        }
    }
}

bool isVowel(char ch) { ch = tolower(ch);return (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'); }

ll gcd(ll a,ll b){    //a larger, b smaller
    if(b>a) swap(b,a);
    if(b==0) return a;
    //cout<<a<<" "<<b<<" "<<cnt<<endl;
    return gcd(b,a%b);
}

void extgcd(ll a,ll b,ll& x,ll& y){  //a smaller b larger
    if(a==0) {
        x = a;y = b;
        return;
    }
    extgcd(b%a,a,x,y);
    ll y1 = y,x1 = x;
    x = y1 - (b/a)*x1;
    y = x1;
}

ll lcm(ll a,ll b){
    return (a*b)/gcd(a,b);
}

ll bexp(ll num,ll e){
    ll ans = 1;
    while(e>0){
        if(e&1){
            ans *= num;
        }
        num *= num;
        e>>= 1;
    }
    return ans;
}

ll mexp(ll num,ll e){
    ll ans = 1;
    while(e>0){
        if(e&1){
            ans = (ans *num)%mod;
        }
        num = (num*num)%mod;
        e>>=1;
    }
    return ans%mod;
}

ll modinv(ll a){
    return mexp(a,mod-2);
}

int main() {
    fastio;
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t = 1;
    //cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vll a(n);
        for0(i,n) cin>>a[i].F;
        for0(i,n) cin>>a[i].S;
        multiset<ll,greater<ll> > st;
        ll ans = 0;
        sort(all(a));
        ll i = 0,res = 0,tmp = -5;
        while(i < n) {
            ll j = i-1;
            if(tmp != a[i].F) {
                while(!st.empty()) {
                    res -= *(st.begin());
                    ans += res;
                    st.erase(st.begin());
                }
                tmp = a[i].F;
            }
            while(j+1 < n && a[j+1].F == a[i].F) {
                st.insert(a[j+1].S);
                res += a[j+1].S;
                j++;
            }
            if(st.size() > 0) {
                res -= *(st.begin());
                ans += res;
                tmp++;
                st.erase(st.begin());
                i = j+1;
            }
        }
        while(!st.empty()) {
            res -= *(st.begin());
            ans += res;
            st.erase(st.begin());
        }
        cout<<ans<<endl;
    }
    /* #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif */
    return 0;
}


# pragma GCC optimize("Ofast")
# pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include<unordered_map>

#define for0(i,n) for(ll i = 0;i < (n);i++)
#define for1(i,n) for(ll i = 1;i<=(n);i++)
#define for2(i,n) for(ll i = 2;i<=(n);i++)
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
#define rep(i,st,en,cnt) for(ll i = st;i <= en;i+=cnt)
#define PB push_back
#define MP make_pair
#define fastio cin.tie(0);cout.tie(0);ios::sync_with_stdio(0)
#define F first
#define S second
#define EPS 1e-8
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f3f3f
#define NEGINF -INF
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define inp_arr(a,n) for0(i,n) {cin>>a[i];}
#define out_arr(a,n) for0(i,n) {cout<<a[i]<<" ";}

using namespace std;

typedef pair<int,int> ii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<ll> > vvl;
typedef vector<pair<long long,long long> > vll;
typedef pair<long long,pll> tll;

const ll mod = 1e9+7;

ll nop = 1e7+2;
vi primes(nop,1);

void sieve(){
    primes[1] = primes[0] = 0;
    for(ll i = 2;i * i <= nop;i++){
        if(primes[i]){
            for(ll j = i*i; j <= nop;j+=i){
                primes[j] = 0;
            }
        }
    }
}

bool isVowel(char ch) { ch = tolower(ch);return (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'); }

ll gcd(ll a,ll b){    //a larger, b smaller
    if(b>a) swap(b,a);
    if(b==0) return a;
    //cout<<a<<" "<<b<<" "<<cnt<<endl;
    return gcd(b,a%b);
}

void extgcd(ll a,ll b,ll& x,ll& y){  //a smaller b larger
    if(a==0) {
        x = a;y = b;
        return;
    }
    extgcd(b%a,a,x,y);
    ll y1 = y,x1 = x;
    x = y1 - (b/a)*x1;
    y = x1;
}

ll lcm(ll a,ll b){
    return (a*b)/gcd(a,b);
}

ll bexp(ll num,ll e){
    ll ans = 1;
    while(e>0){
        if(e&1){
            ans *= num;
        }
        num *= num;
        e>>= 1;
    }
    return ans;
}

ll mexp(ll num,ll e){
    ll ans = 1;
    while(e>0){
        if(e&1){
            ans = (ans *num)%mod;
        }
        num = (num*num)%mod;
        e>>=1;
    }
    return ans%mod;
}

ll modinv(ll a){
    return mexp(a,mod-2);
}

bool sbythis(pll a,pll b) {
    if(a.F != b.F) return a.F < b.F;
    return a.S > b.S;
}

int main() {
    fastio;
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t = 1;
    //cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vll a(n);
        for0(i,n) cin>>a[i].F;
        for0(i,n) cin>>a[i].S;
        set<pll> st;
        ll ans = 0;
        sort(all(a),sbythis);
        st.insert(a[0]);
        ll mn = a[0].F;
        for1(i,n-1) {
            if(a[i].F < mn) a[i].F = mn;
            auto it = st.lower_bound({a[i].F,-1});
            if(it == st.end() || it->F != a[i].F) {
                st.insert(a[i]);
            }
            else if(it->S < a[i].S) {
                pll tmp = *it;
                st.erase(it);
                st.insert(a[i]);
                st.insert({tmp.F+1,tmp.S});
                mn++;
                ans += tmp.S;
            }
            else {
                st.insert({a[i].F+1,a[i].S});
                mn++;
                ans += a[i].S;
            }
        }
        cout<<ans<<endl;
    }
    /* #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif */
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int,vector<int>>st;
struct shan{
    int size;
    int time;
    bool operator<(const shan x) const {
            return size < x.size;
      }
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    // for(i=0;i<200002;i++)
    // {
    //     a[i].size=-1;a[i].time=-1;
    // }
    // priority_queue<int>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;a[n].time=10000000;

    sort(a,a+n);
    i=0;
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        while(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    // while(i<n||!st.empty())
    // {
    //     if(st.empty())curr=a[i].size;
    //     while(i<n&&curr==a[i].size)
    //     {
    //         st.push(-a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     sum+=st.top();
    //     ans+=sum;
    //     st.pop();
    //     curr++;
    // }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int>st;
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    for(i=0;i<200002;i++)
    {
        a[i].size=-1;a[i].time=-1;
    }
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;

    sort(a,a+n,compare);
    i=0;
    // for(i=0;i<n;i++)
    // {
    //     curr=a[i].size;
    //     while(curr==a[i+1].size)
    //     {
    //         st.push(a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     st.push(a[i].time);
    //     sum+=a[i].time;
    //     while(!st.empty())
    //     {
    //         if(curr==a[i+1].size)
    //         break;
    //         else
    //         {
    //             sum-=st.top();
    //             st.pop();
    //             curr++;
    //             ans+=sum;
    //         }
    //     }
    // }
    while(i<n||!st.empty())
    {
        if(st.empty())curr=a[i].size;
        while(i<n&&curr==a[i].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        sum-=st.top();
        ans+=sum;
        st.pop();
        curr++;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        while(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        // sum-=*st.begin();
        // st.erase(st.begin());
        // curr++;
        // ans+=sum;
        while(st.size()>0)
        {
            if(curr==a[i].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
        i--;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    scanf("%llu",&n);
    multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        // sum-=*st.begin();
        // st.erase(st.begin());
        // curr++;
        // ans+=sum;
        while(st.size()>0)
        {
            if(curr==a[i].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
        i--;
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    scanf("%llu",&n);
    multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i+1].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.insert(a[i].time);
        sum+=a[i].time;
        while(st.size()>0)
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
struct shan{
    int size;
    int time;
};
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    shan a[n+2];multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        // sum-=*st.begin();
        // st.erase(st.begin());
        // curr++;
        // ans+=sum;
        while(st.size()>0)
        {
            if(curr==a[i].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
        i--;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    scanf("%llu",&n);
    multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i+1].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.insert(a[i].time);
        sum+=a[i].time;curr=i+1;
        while(st.size()>0)
        {
            if(curr==a[i].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
struct shan
{
    int size;
    int time;
};
bool compare(shan i1,shan i2)
{
    return(i1.size<=i2.size);
}
signed main()
{
    int n,i,curr,ans=0;multiset<int,greater<int>>st;multiset<int,greater<int>>::iterator it;
    cin>>n;
    shan a[n+2];
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    sort(a,a+n,compare);
    a[n].size=10000000000;
    a[n].time=1000000;
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        while(a[i].size==curr)
        {
            st.insert(a[i].time);
            i++;
        }
        for(it=st.begin();it!=st.end();it++)
        {
            if(st.empty()||curr==a[i].size)
            break;
            else
            {
                ans+=(curr-*(it));
                curr++;
                st.erase(it);
            }
        }
        i--;
    }
    for(it=st.begin();it!=st.end();it++)
    {
        ans+=(curr-*(it));
        curr++;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int>st;
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    for(i=0;i<200002;i++)
    {
        a[i].size=-1;a[i].time=-1;
    }
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;

    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        while(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int,vector<int>, greater<int>>st;
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    // for(i=0;i<200002;i++)
    // {
    //     a[i].size=-1;a[i].time=-1;
    // }
    // priority_queue<int>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;a[n].time=10000000;

    sort(a,a+n,compare);
    i=0;
    // for(i=0;i<n;i++)
    // {
    //     curr=a[i].size;
    //     while(curr==a[i+1].size)
    //     {
    //         st.push(a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     st.push(a[i].time);
    //     sum+=a[i].time;
    //     while(!st.empty())
    //     {
    //         if(curr==a[i+1].size)
    //         break;
    //         else
    //         {
    //             sum-=st.top();
    //             st.pop();
    //             curr++;
    //             ans+=sum;
    //         }
    //     }
    // }
    while(i<n||!st.empty())
    {
        if(st.empty())curr=a[i].size;
        while(i<n&&curr==a[i].size)
        {
            st.push(-a[i].time);
            sum+=a[i].time;
            i++;
        }
        sum+=st.top();
        ans+=sum;
        st.pop();
        curr++;
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int,vector<int>, greater<int>>st;
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    // for(i=0;i<200002;i++)
    // {
    //     a[i].size=-1;a[i].time=-1;
    // }
    // priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;a[n].time=10000000;

    sort(a,a+n,compare);
    i=0;
    // for(i=0;i<n;i++)
    // {
    //     curr=a[i].size;
    //     while(curr==a[i+1].size)
    //     {
    //         st.push(a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     st.push(a[i].time);
    //     sum+=a[i].time;
    //     while(!st.empty())
    //     {
    //         if(curr==a[i+1].size)
    //         break;
    //         else
    //         {
    //             sum-=st.top();
    //             st.pop();
    //             curr++;
    //             ans+=sum;
    //         }
    //     }
    // }
    while(i<n||!st.empty())
    {
        if(st.empty())curr=a[i].size;
        while(i<n&&curr==a[i].size)
        {
            st.push(-a[i].time);
            sum+=a[i].time;
            i++;
        }
        sum+=st.top();
        ans+=sum;
        st.pop();
        curr++;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    scanf("%llu",&n);
    priority_queue<int>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    shan a[n+2];multiset<int,greater<int>>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i].size)
        {
            st.insert(a[i].time);
            sum+=a[i].time;
            i++;
        }
        // sum-=*st.begin();
        // st.erase(st.begin());
        // curr++;
        // ans+=sum;
        while(st.size()>0)
        {
            if(curr==a[i].size)
            break;
            else
            {
                sum-=*st.begin();
                st.erase(st.begin());
                curr++;
                ans+=sum;
            }
        }
        i--;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int,vector<int>, greater<int>>st;
struct shan{
    int size;
    int time;
    bool operator<(const shan x) const {
            return size < x.size;
      }
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    // for(i=0;i<200002;i++)
    // {
    //     a[i].size=-1;a[i].time=-1;
    // }
    // priority_queue<int>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;a[n].time=10000000;

    sort(a,a+n);
    i=0;
    // for(i=0;i<n;i++)
    // {
    //     curr=a[i].size;
    //     while(curr==a[i+1].size)
    //     {
    //         st.push(a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     st.push(a[i].time);
    //     sum+=a[i].time;
    //     while(!st.empty())
    //     {
    //         if(curr==a[i+1].size)
    //         break;
    //         else
    //         {
    //             sum-=st.top();
    //             st.pop();
    //             curr++;
    //             ans+=sum;
    //         }
    //     }
    // }
    while(i<n||!st.empty())
    {
        if(st.empty())curr=a[i].size;
        while(i<n&&curr==a[i].size)
        {
            st.push(-a[i].time);
            sum+=a[i].time;
            i++;
        }
        sum+=st.top();
        ans+=sum;
        st.pop();
        curr++;
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    for(i=0;i<200002;i++)
    {
        a[i].size=-1;a[i].time=-1;
    }
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;

    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        while(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
priority_queue<int,vector<int>, greater<int>>st;
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    // for(i=0;i<200002;i++)
    // {
    //     a[i].size=-1;a[i].time=-1;
    // }
    // priority_queue<int>st;
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].size);
    for(i=0;i<n;i++)
    scanf("%llu",&a[i].time);
    a[n].size=10000000000;a[n].time=10000000;

    sort(a,a+n,compare);
    i=0;
    // for(i=0;i<n;i++)
    // {
    //     curr=a[i].size;
    //     while(curr==a[i+1].size)
    //     {
    //         st.push(a[i].time);
    //         sum+=a[i].time;
    //         i++;
    //     }
    //     st.push(a[i].time);
    //     sum+=a[i].time;
    //     while(!st.empty())
    //     {
    //         if(curr==a[i+1].size)
    //         break;
    //         else
    //         {
    //             sum-=st.top();
    //             st.pop();
    //             curr++;
    //             ans+=sum;
    //         }
    //     }
    // }
    while(i<n||!st.empty())
    {
        if(st.empty())curr=a[i].size;
        while(i<n&&curr==a[i].size)
        {
            st.push(-a[i].time);
            sum+=a[i].time;
            i++;
        }
        sum+=st.top();
        ans+=sum;
        st.pop();
        curr++;
    }
    printf("%llu",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
struct shan{
    int size;
    int time;
};
shan a[200002];
bool compare(shan a,shan b)
{
    return(a.size<=b.size);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,i,sum=0,ans=0,curr=0;
    cin>>n;
    priority_queue<int>st;
    for(i=0;i<n;i++)
    cin>>a[i].size;
    for(i=0;i<n;i++)
    cin>>a[i].time;
    a[n].size=10000000000;
    a[n].time=1000000;
    sort(a,a+n,compare);
    for(i=0;i<n;i++)
    {
        curr=a[i].size;
        if(curr==a[i+1].size)
        {
            st.push(a[i].time);
            sum+=a[i].time;
            i++;
        }
        st.push(a[i].time);
        sum+=a[i].time;
        if(st.size()>1)
        {
        while(!st.empty())
        {
            if(curr==a[i+1].size)
            break;
            else
            {
                sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
            }
        }
        }
        else
        {
            sum-=st.top();
                st.pop();
                curr++;
                ans+=sum;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e6;

unordered_map<ll, vector<ll>> arr;

ll solve() {
	int n; cin >> n;
	vector<ll> vec(n);
	for (int i = 0; i < n; ++i) {
		cin >> vec[i];
	}
	for (int i = 0; i < n; ++i) {
		int x; cin >> x;
		arr[vec[i]].emplace_back(x);
	}
	ll res = 0;
	priority_queue<ll> tmp;
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	bool done = false;
	ll ans = 0;
	for (auto v : vec) {
		if (arr[v].size() > 1 or !tmp.empty()) {
			for (int i = 0; i < arr[v].size(); ++i) {
				tmp.push(arr[v][i]);
				ans += arr[v][i];
			}
			// sort(tmp.begin(), tmp.end());
			ans -= tmp.top(); tmp.pop();
			int curr = v;
			// cout << ":" << v << " : ";
			// for (auto t : tmp) cout << t << " "; cout << "\n";
			while (!tmp.empty()) {
				curr++;
				res += ans;
				if (arr[curr].size()) {
					break;
				} else {
					ans -= tmp.top(); tmp.pop();
				}
			}
		}
	}
	return res;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	#endif
	cout << solve() << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);

	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}

	auto it = mapa.begin();
	int ans = 0;

	while(it != mapa.end())
	{
		//cout << it->first << ' ' << it->second.size() << '\n';
		if(it->second.size() > 1)
		{
			//cout << "entrou " << it->first << '\n';

			auto it2 = next(it);
			int d = 0;

			if(it2 != mapa.end())
				d = it2->first - it->first;

			//cout << "before here " << d << '\n';

			if(it2 != mapa.end() and d > 0 and it->second.size() > 1)
			{
				sum[it->first] -= *prev(it->second.end());
				//cout << "somou " << sum[it->first] << '\n';
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				d--;
			}

			if(it2 == mapa.end())
			{
				sum[it->first] -= *prev(it->second.end());

				//cout << "SOMOU " << sum[it->first] << '\n';
				ans += sum[it->first];
				mapa[it->first + 1] = multiset<int>{};
				swap(mapa[it->first + 1], mapa[it->first]);
				break;
			}
			else
			{
				//cout << "here " << it->first << ' ' << it->second.size() << '\n';
				if(it2->second.size() >= it->second.size())
				{
					for(auto x : it->second)
						it2->second.insert(x);
					sum[it2->first] += sum[it->first];
				}
				else
				{
					//puts("xiii");
					for(auto x : it2->second)
						it->second.insert(x);
/*
cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';
*/
					sum[it2->first] += sum[it->first];
					swap(mapa[it2->first], mapa[it->first]);
/*

cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';

it++;

cout << it->first << '\n';

exit(0);
*/

				}
			}
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);

	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}

	auto it = mapa.begin();
	int ans = 0;

	while(it != mapa.end())
	{
		//cout << it->first << ' ' << it->second.size() << '\n';
		if(it->second.size() > 1)
		{
			auto it2 = next(it);
			int d = 0;

			if(it2 != mapa.end())
				d = it2->first - it->first;

			while(it2 != mapa.end() and d > 0 and it->second.size() > 1)
			{
				sum[it->first] -= *prev(it->second.end());
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				d--;
			}
			if(it2 == mapa.end())
			{
				sum[it->first] -= *prev(it->second.end());
				it->second.erase(prev(it->second.end()));
				ans += sum[it->first];
				mapa[it->first + 1] = multiset<int>{};
				swap(mapa[it->first + 1], mapa[it->first]);
				swap(sum[it->first + 1], sum[it->first]);
			}
			else
			{
				//cout << "here " << it->first << ' ' << it->second.size() << '\n';
				if(it2->second.size() >= it->second.size())
				{
					for(auto x : it->second)
					{
						it2->second.insert(x);
						sum[it2->first] += x;
					}
				}
				else
				{
					//puts("xiii");
					for(auto x : it2->second)
					{
						it->second.insert(x);
						sum[it->first] += x;
					}
/*
cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';
*/
					swap(sum[it2->first], sum[it->first]);
					swap(mapa[it2->first], mapa[it->first]);
/*

cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';

it++;

cout << it->first << '\n';

exit(0);
*/

				}
			}
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);

	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}

	auto it = mapa.begin();
	int ans = 0;

	while(it != mapa.end())
	{
		//cout << it->first << ' ' << it->second.size() << '\n';
		if(it->second.size() > 1)
		{
			auto it2 = next(it);
			int d = 0;

			if(it2 != mapa.end())
				d = it2->first - it->first;

			while(it2 != mapa.end() and d > 0 and it->second.size() > 1)
			{
				sum[it->first] -= *prev(it->second.end());
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				d--;
			}
			if(it2 == mapa.end())
			{
				sum[it->first] -= *prev(it->second.end());

				ans += sum[it->first];
				mapa[it->first + 1] = multiset<int>{};
				swap(mapa[it->first + 1], mapa[it->first]);
				break;
			}
			else
			{
				//cout << "here " << it->first << ' ' << it->second.size() << '\n';
				if(it2->second.size() >= it->second.size())
				{
					for(auto x : it->second)
					{
						it2->second.insert(x);
						sum[it2->first] += x;
					}
				}
				else
				{
					//puts("xiii");
					for(auto x : it2->second)
					{
						it->second.insert(x);
						sum[it->first] += x;
					}
/*
cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';
*/
					swap(sum[it2->first], sum[it->first]);
					swap(mapa[it2->first], mapa[it->first]);
/*

cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';

it++;

cout << it->first << '\n';

exit(0);
*/

				}
			}
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);
	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}
	auto it = mapa.begin();
	int ans = 0;
	while(it != mapa.end())
	{
		if(it->second.size() > 1)
		{
			int nxt = it->first + 1;
			sum[it->first] -= *prev(it->second.end());
			ans += sum[it->first];
			it->second.erase(prev(it->second.end()));
			while(!mapa.count(nxt) and it->second.size() >= 1)
			{
				sum[it->first] -= *prev(it->second.end());
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				nxt++;
			}
			if(mapa[nxt].size() < it->second.size())
			{
				swap(mapa[nxt], mapa[it->first]);
				swap(sum[nxt], sum[it->first]);
			}
			for(auto x : it->second)
				mapa[nxt].insert(x);
		 	sum[nxt] += sum[it->first];
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);

	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}

	auto it = mapa.begin();
	int ans = 0;

	while(it != mapa.end())
	{
		//cout << it->first << ' ' << it->second.size() << '\n';
		if(it->second.size() > 1)
		{
			auto it2 = next(it);
			int d = 0;

			if(it2 != mapa.end())
				d = it2->first - it->first;

			while(it2 != mapa.end() and d > 0 and it->second.size() >= 1)
			{
				sum[it->first] -= *prev(it->second.end());
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				d--;
			}
			if(it2 == mapa.end())
			{
				sum[it->first] -= *prev(it->second.end());

				ans += sum[it->first];
				mapa[it->first + 1] = multiset<int>{};
				swap(mapa[it->first + 1], mapa[it->first]);
				break;
			}
			else
			{
				//cout << "here " << it->first << ' ' << it->second.size() << '\n';
				if(it2->second.size() >= it->second.size())
				{
					for(auto x : it->second)
					{
						it2->second.insert(x);
						sum[it2->first] += x;
					}
				}
				else
				{
					//puts("xiii");
					for(auto x : it2->second)
					{
						it->second.insert(x);
						sum[it->first] += x;
					}
/*
cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';
*/
					swap(sum[it2->first], sum[it->first]);
					swap(mapa[it2->first], mapa[it->first]);
/*

cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';

it++;

cout << it->first << '\n';

exit(0);
*/

				}
			}
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second

int n;
map<int, int> sum;
map<int, multiset<int>> mapa;
int A[300000], T[300000];

int32_t main()
{
	scanf(" %lld", &n);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &A[i]);
	for(int i = 0; i < n; i++)
		scanf(" %lld", &T[i]);

	for(int i = 0; i < n; i++)
	{
		mapa[A[i]].insert(T[i]);
		sum[A[i]] += T[i];
	}

	auto it = mapa.begin();
	int ans = 0;

	while(it != mapa.end())
	{
		//cout << it->first << ' ' << it->second.size() << '\n';
		if(it->second.size() > 1)
		{
			//cout << "entrou " << it->first << '\n';

			auto it2 = next(it);
			int d = 0;

			if(it2 != mapa.end())
				d = it2->first - it->first;

			//cout << "before here " << d << '\n';

			while(it2 != mapa.end() and d > 0 and it->second.size() > 1)
			{
				sum[it->first] -= *prev(it->second.end());
				//cout << "somou " << sum[it->first] << '\n';
				ans += sum[it->first];
				it->second.erase(prev(it->second.end()));
				d--;
			}

			if(it2 == mapa.end())
			{
				sum[it->first] -= *prev(it->second.end());

				//cout << "SOMOU " << sum[it->first] << '\n';
				ans += sum[it->first];
				mapa[it->first + 1] = multiset<int>{};
				swap(mapa[it->first + 1], mapa[it->first]);
				break;
			}
			else
			{
				//cout << "here " << it->first << ' ' << it->second.size() << '\n';
				if(it2->second.size() >= it->second.size())
				{
					for(auto x : it->second)
						it2->second.insert(x);
					sum[it2->first] += sum[it->first];
				}
				else
				{
					//puts("xiii");
					for(auto x : it2->second)
						it->second.insert(x);
/*
cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';
*/
					sum[it2->first] += sum[it->first];
					swap(mapa[it2->first], mapa[it->first]);
/*

cout << it->first << ' ' << mapa[it->first].size() << ' ';
cout << it2->first << ' ' << mapa[it2->first].size() << '\n';

it++;

cout << it->first << '\n';

exit(0);
*/

				}
			}
		}
		it++;
	}
	cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target ("avx2,avx,fma")
#define ll long long int
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(i=l;i<r;i++)
#define bep(i,l,r) for(i=l;i>=r;i--)
#define all(v) v.begin(),v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
int main()
{
    io;
    int i,j,k,n;
    cin>>n;
        vector<pii>a(n);
        rep(j,0,n)
        {
        cin>>a[j].ff;
        }
        rep(j,0,n)
        {
        cin>>a[j].ss;
        a[j].ss=-a[j].ss;
        }
        sort(all(a));
        set<pair<ll,int> >s;
        s.insert(mp(a[0].ss,0));
        ll ans=0;
        rep(j,1,n)
        {
            rep(k,0,min((int)s.size(),a[j].ff-a[j-1].ff))
            {
                //cout<<(-(*s.begin()).ff)<<"\n";
                ans+=(-(*s.begin()).ff)*(a[((*s.begin()).ss)].ff-a[j-1].ff-k);
                s.erase(*s.begin());
            }
            s.insert(mp(a[j].ss,j));
           // cout<<a[j].ss<<" ";
           //cout<<ans<<" ";
        }
        k=0;
        while(!s.empty())
        {
            ans+=(-(*s.begin()).ff)*(a[j-1].ff+k-a[((*s.begin()).ss)].ff);
                s.erase(*s.begin());
                k++;
        }
        cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target ("avx2,avx,fma")
#define ll long long int
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(i=l;i<r;i++)
#define bep(i,l,r) for(i=l;i>=r;i--)
#define all(v) v.begin(),v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
int main()
{
    io;
    int i,j,k,n;
    cin>>n;
        vector<pii>a(n);
        rep(j,0,n)
        {
        cin>>a[j].ff;
        }
        rep(j,0,n)
        {
        cin>>a[j].ss;
        a[j].ss=-a[j].ss;
        }
        sort(all(a));
        set<pair<ll,int> >s;
        s.insert(mp(a[0].ss,0));
        ll ans=0;
        rep(j,1,n)
        {
            rep(k,0,min((int)s.size(),a[j].ff-a[j-1].ff))
            {
                //cout<<(-(*s.begin()).ff)<<"\n";
                ans+=(-(*s.begin()).ff)*(a[j-1].ff+k-a[((*s.begin()).ss)].ff);
                s.erase(*s.begin());
            }
            s.insert(mp(a[j].ss,j));
           // cout<<a[j].ss<<" ";
           if(n==200000)
           cout<<ans<<" "<<a[j].ff<<" "<<a[j].ss<<"\n";
        }
        k=0;
        while(!s.empty())
        {
            ans+=(-(*s.begin()).ff)*(a[j-1].ff+k-a[((*s.begin()).ss)].ff);
                s.erase(*s.begin());
                k++;
        }
        cout<<ans<<"\n";
    return 0;
}


using System;
using System.Linq;

namespace Бал_в_БерлГУ
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            int[] boys = Console.ReadLine().Split(' ')
                .Select(int.Parse).ToArray();

            Array.Sort(boys);

            int m = int.Parse(Console.ReadLine());

            int[] girls = Console.ReadLine().Split(' ')
                .Select(int.Parse).ToArray();

            Array.Sort(girls);

            if (n > m)
            {
                Console.WriteLine(Min_Max(girls, boys));
            }
            else
            {
                Console.WriteLine(Min_Max(boys, girls));
            }
        }

        static int Min_Max(int[] arrayMin, int[] arrayMax)
        {
            int count = 0;
            int k = 0;
            for (int i = 0; i < arrayMin.Length; i++)
            {
                for (int j = k; j < arrayMax.Length; j++)
                {
                    if (Math.Abs(arrayMin[i] - arrayMax[j]) <= 1)
                    {
                        count++;
                        k++;
                        break;
                    }
                }
            }
            return count;
        }
    }
}


using System;
using System.Linq;

namespace Бал_в_БерлГУ
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            int[] boys = Console.ReadLine().Split(' ')
                .Select(int.Parse).ToArray();

            Array.Sort(boys);

            int m = int.Parse(Console.ReadLine());

            int[] girls = Console.ReadLine().Split(' ')
                .Select(int.Parse).ToArray();

            Array.Sort(girls);

            if (n > m)
            {
                Console.WriteLine(Min_Max(girls, boys));
            }
            else
            {
                Console.WriteLine(Min_Max(boys, girls));
            }
        }

        static int Min_Max(int[] arrayMin, int[] arrayMax)
        {
            int count = 0;
            int k = 0;
            for (int i = 0; i < arrayMin.Length; i++)
            {
                for (int j = k; j < arrayMax.Length; j++)
                {
                    if (Math.Abs(arrayMin[i] - arrayMax[j]) <= 1)
                    {
                        arrayMax[j] = -1;
                        count++;
                        k++;
                        break;
                    }
                }
            }
            return count;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Round_277_5
{
    class Program
    {
        static void Main(string[] args)
        {
            //SwapSort ss = new SwapSort();
            //ss.Test();
            BerSU_Ball bb = new BerSU_Ball();
            bb.Test();
        }
    }
    class SwapSort
    {
        class MySwap
        {
            public int i=0;
            public int j=0;
        }
        List<MySwap> Sort (int[]a)
        {
            List<MySwap> l = new List<MySwap>();
            for(int i=0;i<a.Length;i++)
            {
                int min = i;
                for(int j=i;j<a.Length;j++)
                {
                    if (a[j] < a[min]) min = j;
                }
                if(i!=min)
                {
                    Swap(a, i, min);
                    MySwap m = new MySwap();
                    m.i = i;
                    m.j = min;
                    l.Add(m);
                }

            }
            return l;

        }
        void Print(int[]a)
        {
            for (int i = 0; i < a.Length; i++)
                Console.Write(a[i]);
            Console.WriteLine();
        }
        public void Test()
        {
            //int[] a = { 3, 2, 1 };
            int n = int.Parse(Console.ReadLine());
            int[]a=new int[n];
            string[] ss = Console.ReadLine().Split();
            for (int i = 0; i < n && i < ss.Length; i++)
                a[i] = int.Parse(ss[i]);

            List<MySwap> lms = Sort(a);
            Console.WriteLine(lms.Count());
            for (int i = 0; i < lms.Count; i++)
                Console.WriteLine(lms[i].i + " " + lms[i].j);

        }
        void Swap(int []a,int i,int j)
        {
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
    class BerSU_Ball
    {
        int Merge(int[]a,int[]b)
        {
            int i = 0;
            int j = 0;
            int cnt = 0;
            while(i<a.Length&&j<b.Length)
            {
                //while (i < a.Length && b[j] - a[i] > 1) i++;
                //if(Math.Abs(a[i]-b[j])<=1)
                //{
                //    cnt++;
                //    i++;
                //    j++;
                //}
                //if (i >= a.Length) break;
                //while (j < b.Length && a[i] - b[j] > 1) j++;
                //if (Math.Abs(a[i] - b[j]) <= 1)
                //{
                //    cnt++;
                //    i++;
                //    j++;
                //}
                while(i<a.Length&&j<b.Length&&Math.Abs(a[i]-b[j])<=1)
                {
                    i++;
                    j++;
                    cnt++;
                }
                if (i >= a.Length || j >= b.Length) break;
                while(i<a.Length&&a[i]<b[j]-1)
                {
                    i++;
                }
                if (i >= a.Length || j >= b.Length) break;
                while(j<b.Length&&b[j]<a[i]-1)
                {
                    j++;
                }
            }
            return cnt;
        }
        public void Test()
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[n];
            string[] ss = Console.ReadLine().Split();
            for (int i = 0; i < n && i < ss.Length; i++)
                a[i] = int.Parse(ss[i]);
            int m = int.Parse(Console.ReadLine());
            int[] b = new int[m];
            ss = Console.ReadLine().Split();
            for (int i = 0; i < m && i < ss.Length; i++)
                b[i] = int.Parse(ss[i]);
            Array.Sort(a);
            Array.Sort(b);
            Console.WriteLine(Merge(a, b));
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Round_277_5
{
    class Program
    {
        static void Main(string[] args)
        {
            //SwapSort ss = new SwapSort();
            //ss.Test();
            BerSU_Ball bb = new BerSU_Ball();
            bb.Test();
        }
    }
    class SwapSort
    {
        class MySwap
        {
            public int i=0;
            public int j=0;
        }
        List<MySwap> Sort (int[]a)
        {
            List<MySwap> l = new List<MySwap>();
            for(int i=0;i<a.Length;i++)
            {
                int min = i;
                for(int j=i;j<a.Length;j++)
                {
                    if (a[j] < a[min]) min = j;
                }
                if(i!=min)
                {
                    Swap(a, i, min);
                    MySwap m = new MySwap();
                    m.i = i;
                    m.j = min;
                    l.Add(m);
                }

            }
            return l;

        }
        void Print(int[]a)
        {
            for (int i = 0; i < a.Length; i++)
                Console.Write(a[i]);
            Console.WriteLine();
        }
        public void Test()
        {
            //int[] a = { 3, 2, 1 };
            int n = int.Parse(Console.ReadLine());
            int[]a=new int[n];
            string[] ss = Console.ReadLine().Split();
            for (int i = 0; i < n && i < ss.Length; i++)
                a[i] = int.Parse(ss[i]);

            List<MySwap> lms = Sort(a);
            Console.WriteLine(lms.Count());
            for (int i = 0; i < lms.Count; i++)
                Console.WriteLine(lms[i].i + " " + lms[i].j);

        }
        void Swap(int []a,int i,int j)
        {
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
    class BerSU_Ball
    {
        int Merge(int[]a,int[]b)
        {
            int i = 0;
            int j = 0;
            int cnt = 0;
            while(i<a.Length&&j<b.Length)
            {
                //while (i < a.Length && b[j] - a[i] > 1) i++;
                //if(Math.Abs(a[i]-b[j])<=1)
                //{
                //    cnt++;
                //    i++;
                //    j++;
                //}
                //if (i >= a.Length) break;
                //while (j < b.Length && a[i] - b[j] > 1) j++;
                //if (Math.Abs(a[i] - b[j]) <= 1)
                //{
                //    cnt++;
                //    i++;
                //    j++;
                //}
                while(i<a.Length&&j<b.Length&&Math.Abs(a[i]-b[j])<=1)
                {
                    i++;
                    j++;
                    cnt++;
                }
                if (i >= a.Length || j >= b.Length) break;
                while(i<a.Length&&a[i]<b[j]-1)
                {
                    i++;
                }
                if (i >= a.Length || j >= b.Length) break;
                while(j<b.Length&&b[j]<a[j]-1)
                {
                    j++;
                }
            }
            return cnt;
        }
        public void Test()
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[n];
            string[] ss = Console.ReadLine().Split();
            for (int i = 0; i < n && i < ss.Length; i++)
                a[i] = int.Parse(ss[i]);
            int m = int.Parse(Console.ReadLine());
            int[] b = new int[m];
            ss = Console.ReadLine().Split();
            for (int i = 0; i < m && i < ss.Length; i++)
                b[i] = int.Parse(ss[i]);
            Array.Sort(a);
            Array.Sort(b);
            Console.WriteLine(Merge(a, b));
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace ReadWriteTemplate
{
    public class Solver
    {
        public static void Solve()
        {
            int n = ReadInt();
            var a = ReadIntArray();
            int m = ReadInt();
            var b = ReadIntArray();

            Array.Sort(a);
            Array.Sort(b);

            var c = new int[1000];
            var d = new int[1000];
            for (int i = 0; i < n; i++)
            {
                c[a[i]]++;
            }
            for (int i = 0; i < m; i++)
            {
                d[b[i]]++;
            }

            int ans = 0;
            for (int i = 0; i < 110; i++)
            {
                int count = Math.Min(c[i], d[i]);
                ans += count;
                c[i] -= count;
                d[i] -= count;

                count = Math.Min(c[i], d[i + 1]);
                ans += count;
                c[i] -= count;
                d[i + 1] -= count;

                count = Math.Min(c[i + 1], d[i]);
                ans += count;
                c[i + 1] -= count;
                d[i] -= count;
            }

            Writer.WriteLine(ans);
        }

        public static void Main()
        {
            Reader = Console.In;
            Writer = Console.Out;
            //Reader = File.OpenText("input.txt");
            //Writer = File.CreateText("output.txt");

            Solve();

            Reader.Close();
            Writer.Close();
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    { static void Main(string[] args)
        {
            int p =Convert.ToInt32(Console.ReadLine());
			string[] num1 = Console.ReadLine().Split(' ');
            int k= Convert.ToInt32(Console.ReadLine());
			string[] num2 = Console.ReadLine().Split(' ');
            int [] first= new int[p];
            int [] second = new int[k];
            int i = 0;
            while (i<p)
            {
                first[i]= Convert.ToInt32(num1[i]);
                i++;
            }
            i = 1;
            int buf;
            while (i < p)
            {
                if (first[i] < first[i - 1])
                {
                    buf = first[i - 1];
                    first[i - 1] = first[i];
                    first[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            while (i < k)
            {
                second[i] = Convert.ToInt32(num2[i]);
                i++;
            }
            i = 1;
            while (i < k)
            {
                if (second[i] < second[i - 1])
                {
                    buf = second[i - 1];
                    second[i - 1] = second[i];
                    second[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            int j = 0;
            int count = 0;
            while(i<p)
            {
                while (j < k && i < p)
                {
                    if (Math.Abs(first[i] - second[j])<=1)
                    {
                        count++;
                        i++;
                    }
                    /*else*/ j++;
                }
                i++;
            }
			Console.WriteLine(Convert.ToString(count));
         }
    }
}


//хорошо реализовано, с умом и математикой
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int p =Convert.ToInt32(Console.ReadLine());
			string[] num1 = Console.ReadLine().Split(' ');
            int k= Convert.ToInt32(Console.ReadLine());
			string[] num2 = Console.ReadLine().Split(' ');
            int [] first= new int[p];
            int [] second = new int[k];
            int i = 0;
            while (i<p)
            {
                first[i]= Convert.ToInt32(num1[i]);
                i++;
            }
            i = 1;
            int buf;
            while (i < p)
            {
                if (first[i] < first[i - 1])
                {
                    buf = first[i - 1];
                    first[i - 1] = first[i];
                    first[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            while (i < k)
            {
                second[i] = Convert.ToInt32(num2[i]);
                i++;
            }
            i = 1;
            while (i < k)
            {
                if (second[i] < second[i - 1])
                {
                    buf = second[i - 1];
                    second[i - 1] = second[i];
                    second[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            int j = 0;
            int count = 0;
            while(i<p)
            {
                while(j<k)
                {
                    if (Math.Abs(first[i] - second[j])<=1)
                    {
                        count++;
                        i++;
                    }
                    else j++;
                }
                i++;
            }
			Console.WriteLine(Convert.ToString(count));
         }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    { static void Main(string[] args)
        {
            int p =Convert.ToInt32(Console.ReadLine());
			string[] num1 = Console.ReadLine().Split(' ');
            int k= Convert.ToInt32(Console.ReadLine());
			string[] num2 = Console.ReadLine().Split(' ');
            int [] first= new int[p];
            int [] second = new int[k];
            int i = 0;
            while (i<p)
            {
                first[i]= Convert.ToInt32(num1[i]);
                i++;
            }
            i = 1;
            int buf;
            while (i < p)
            {
                if (first[i] < first[i - 1])
                {
                    buf = first[i - 1];
                    first[i - 1] = first[i];
                    first[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            while (i < k)
            {
                second[i] = Convert.ToInt32(num2[i]);
                i++;
            }
            i = 1;
            while (i < k)
            {
                if (second[i] < second[i - 1])
                {
                    buf = second[i - 1];
                    second[i - 1] = second[i];
                    second[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            int j = 0;
            int count = 0;
            while(i<p)
            {
                while (j < k && i < p)
                {
                    if (Math.Abs(first[i] - second[j])<=1)
                    {
                        Console.WriteLine(first[i].ToString() + " " + second[j].ToString());
                        count++;
                        i++;
                    }
                    /*else*/ j++;
                }
                i++;
            }
			Console.WriteLine(Convert.ToString(count));
            Console.ReadLine();
         }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    { static void Main(string[] args)
        {
            int p =Convert.ToInt32(Console.ReadLine());
			string[] num1 = Console.ReadLine().Split(' ');
            int k= Convert.ToInt32(Console.ReadLine());
			string[] num2 = Console.ReadLine().Split(' ');
            int [] first= new int[p];
            int [] second = new int[k];
            int i = 0;
            while (i<p)
            {
                first[i]= Convert.ToInt32(num1[i]);
                i++;
            }
            i = 1;
            int buf;
            while (i < p)
            {
                if (first[i] < first[i - 1])
                {
                    buf = first[i - 1];
                    first[i - 1] = first[i];
                    first[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            while (i < k)
            {
                second[i] = Convert.ToInt32(num2[i]);
                i++;
            }
            i = 1;
            while (i < k)
            {
                if (second[i] < second[i - 1])
                {
                    buf = second[i - 1];
                    second[i - 1] = second[i];
                    second[i] = buf;
                    if (i > 1) i--;
                }
                else i++;
            }
            i = 0;
            int j = 0;
            int count = 0;
            while(i<p)
            {
                while (j < k && i < p)
                {
                    if (Math.Abs(first[i] - second[j])<=1)
                    {
                        count++;
                        i++;
                    }
                    else j++;
                }
                i++;
            }
			Console.WriteLine(Convert.ToString(count));
         }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task6
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a1;
            int[] a2;

            Read(1, out a1);
            Read(2, out a2);

            a1 = a1.OrderBy(i => i).ToArray();
            a2 = a2.OrderBy(i => i).ToArray();

            int answer = 0;
            int sIndex = 0;
            for (int i = 0; i < a1.Length; i++)
            {
                for (int j = sIndex; j < a2.Length; j++)
                {
                    if (Math.Abs(a1[i] - a2[j]) <= 1)
                    {
                        sIndex = j + 1;
                        answer++;
                        break;
                    }
                }
            }

            Console.WriteLine(answer);
        //    Console.ReadLine();
        }

        static void Read(int p, out int[] arr)
        {
            int n1;
            string str;
            do
            {
              //  Console.Write("Размер массива " + p + ": ");
                str = Console.ReadLine();
                if (string.IsNullOrEmpty(str) || !int.TryParse(str, out n1) || n1 < 1 || n1 > 100)
                    continue;
                break;
            } while (true);

            arr = new int[n1];
            do
            {
              //  Console.Write("Элементы массива " + p + ": ");
                str = Console.ReadLine();
                if (string.IsNullOrEmpty(str))
                    continue;
                var strArr = str.Split(' ');
                if (strArr.Length != n1)
                    continue;

                int i;
                for (i = 0; i < n1; i++)
                {
                    if (!int.TryParse(strArr[i], out arr[i]) || arr[i] < 1 || arr[i] > 100)
                        break;
                }

                if (i != n1)
                    continue;
                break;
            } while (true);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (Math.Abs(boys[i] - girls[j]) <= 1)
                    {
                        count++;
                        girls[i] = -1;
                        break;
                    }
                }
            }
            Console.WriteLine(count);
            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            if (boys.Length <= girls.Length)
            {
                to = boys.Length;
            }
            else
            {
                to = girls.Length;
            }
            for (int i = 0; i <to; i++)
            {
                if ((Math.Abs(boys[i] - girls[i]) == 1) || (boys[i] - girls[i] == 0))
                {
                    count++;
                }
            }
            Console.WriteLine(count);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            int toBig = 0;
            if (boys.Length <= girls.Length)
            {
                to = boys.Length;
                toBig = girls.Length;
            }
            else
            {
                to = girls.Length;
                toBig = boys.Length;
            }

            for (int i = 0; i <to; i++)
            {
                for (int j = 0; j < toBig; j++)
                {
                    if (((Math.Abs(boys[i]) - girls[j] == 1) || (boys[i] - girls[i] == 0)) && ((boys[i] != -1) && (girls[i] !=-1))  )
                    {
                        count++;
                        boys[i] = -1;
                        boys[j] = -1;
                    }
                }
            }
            Console.WriteLine(count);
            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (Math.Abs(boys[i] - girls[j]) <= 1)
                    {
                        count++;
                        girls[j] = -1;
                        break;
                    }
                }
            }
            Console.WriteLine(count);
            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            if (boys.Length <= girls.Length)
            {
                to = boys.Length;
            }
            else
            {
                to = girls.Length;
            }
            for (int i = 0; i <to; i++)
            {
                if ((Math.Abs(boys[i] - girls[i]) == 1) || (boys[i] - girls[i] == 0))
                {
                    count++;
                }
            }
            Console.WriteLine(count);
            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] boys = new int[n];
            string[] inputBoys = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                boys[i] = int.Parse(inputBoys[i]);
            }

            int m = int.Parse(Console.ReadLine());
            int[] girls = new int[m];
            string[] inputGirls = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                girls[i] = int.Parse(inputGirls[i]);
            }
            Array.Sort(boys);
            Array.Sort(girls);
            int to = 0;
            int count = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (Math.Abs(boys[i] - girls[i]) <= 1)
                    {
                        count++;
                        girls[i] = -1;
                        break;
                    }
                }
            }
            Console.WriteLine(count);
            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;

namespace prB {

    class Program {
#if ONLINE_JUDGE
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), System.Text.Encoding.ASCII, false, 1024 * 10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), System.Text.Encoding.ASCII, 1024 * 10);
#else
        private static readonly StreamWriter writer = new StreamWriter("output");
        private static readonly StreamReader reader = new StreamReader("input");
#endif
        static void Main(string[] args) {
            int n = Next();
            int[] a = new int[n];
            for(int i = 0; i < n; i++) {
                a[i] = Next();
            }
            int m = Next();
            int[] b = new int[m];
            for(int i = 0; i < m; i++) {
                b[i] = Next();
            }
            Array.Sort(a);
            Array.Sort(b);
            int j = 0;
            int ans = 0;
            for(int i = 0; i < n; i++) {
                while(j < m && b[j] < a[i] - 1) {
                    ++j;
                }
                if(j == m)
                    break;
                if(Math.Abs(a[i] - b[j]) <= 1) {
                    j++;
                    ans++;
                }
            }
            writer.Write(ans);

            writer.Flush();
#if !ONLINE_JUDGE
            writer.Close();
#endif
        }
        private static int Next() {
            int c;
            int res = 0;
            do {
                c = reader.Read();
                if(c == -1)
                    return res;
            } while(c != '-' && (c < '0' || c > '9'));
            int sign = 1;
            if(c == '-') {
                sign = -1;
                c = reader.Read();
            }
            res = c - '0';
            while(true) {
                c = reader.Read();
                if(c < '0' || c > '9')
                    return res * sign;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public object Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();
        int m = ReadInt();
        var b = ReadIntArray();

        Array.Sort(a);
        Array.Sort(b);

        int pa = 0, pb = 0;
        int ans = 0;
        while (pa < n && pb < m)
        {
            if (Math.Abs(a[pa] - b[pb]) <= 1)
            {
                ans++;
                pa++;
                pb++;
            }
            else if (a[pa] < b[pb] - 1)
                pa++;
            else
                pb++;
        }

        return ans;

        return null;
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("instruction.in");
        //writer = new StreamWriter("instruction.out");
#endif
        try
        {
            object result = new Solver().Solve();
            if (result != null)
                writer.WriteLine(result);
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write<T>(params T[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    #endregion
}


using System;

public class Program {
    private int[] boys;
    private int[] girls;

    public void Solve() {
        Console.ReadLine();
        boys = Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), x => int.Parse(x));
        Console.ReadLine();
        girls = Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), x => int.Parse(x));
        Console.WriteLine(findMaxPairs());
    }

    private int findMaxPairs() {
        Array.Sort(boys);
        Array.Sort(girls);
        bool[] used = new bool[girls.Length];
        int count = 0;
        foreach (int boy in boys) {
            for (int i = 0; i < girls.Length; ++i) {
                if (!used[i] && Math.Abs(boy - girls[i]) <= 1) {
                    used[i] = true;
                    ++count;
                    break;
                }
            }
        }
        return count;
    }

    public static void Main() {
        new Program().Solve();
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication249
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split();
            int[] m = new int[n];
            for (int i = 0; i < n; i++)
            {
                m[i] = int.Parse(s[i]);
            }
            int n1 = int.Parse(Console.ReadLine());
            string[] s1 = Console.ReadLine().Split();
            int[] m1 = new int[n1];
            for (int i = 0; i < n1; i++)
            {
                m1[i] = int.Parse(s1[i]);
            }
            int p = 0;
            Array.Sort(m);
            Array.Sort(m1);
            int k = 0;
            for (int i = 0; i < m.Length; i++)
            {
                int x = m[i];
                for (int e = p; e < m1.Length; e++)
                {
                    if (Math.Abs(x - m1[e]) <= 1)
                    {
                        k++;
                        p = e + 1;
                        break;
                    }
                }
            }
            Console.WriteLine(k);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication249
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split();
            int[] m = new int[n];
            for (int i = 0; i < n; i++)
            {
                m[i] = int.Parse(s[i]);
            }
            int n1 = int.Parse(Console.ReadLine());
            string[] s1 = Console.ReadLine().Split();
            int[] m1 = new int[n1];
            for (int i = 0; i < n1; i++)
            {
                m1[i] = int.Parse(s1[i]);
            }
            int max = -1;
            for (int i = 0; i < n; i++)
            {
                int k = 0;
                for (int e = 0; e < n1; e++)
                {
                    if (Math.Abs(m[i] - m1[e]) <= 1)
                    {
                        k++;
                    }
                }
                if (k > max)
                {
                    max = k;
                }
            }
            Console.WriteLine(max);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] array = new int[n];
            string[] s = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                array[i] = int.Parse(s[i]);
            }
            int m = int.Parse(Console.ReadLine());
            int[] array1 = new int[m];
            string[] s1 = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                array1[i] = int.Parse(s1[i]);
            }
            Array.Sort(array);
            Array.Sort(array1);
            int f = 0;
            for (int i = 0; i < n; i++)
            {
                int x = array[i];
                for (int e = i; e < m; e++)
                {
                    if ( (Math.Abs(x-array1[e]) <= 1) )
                    {
                        f++;
                        break;
                    }
                }
            }

            Console.WriteLine(f);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] array = new int[n];
            string[] s = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
            {
                array[i] = int.Parse(s[i]);
            }
            int m = int.Parse(Console.ReadLine());
            int[] array1 = new int[m];
            string[] s1 = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                array1[i] = int.Parse(s1[i]);
            }
            Array.Sort(array);
            Array.Sort(array1);
            int f = 0;
            for (int i = 0; i < n; i++)
            {
                int x = array[i];
                for (int e = i; e < n; e++)
                {
                    if ( (Math.Abs(x-array1[e]) <= 1) )
                    {
                        f++;
                        break;
                    }
                }
            }

            Console.WriteLine(f);
        }
    }
}


/***************************************************************************
* Title       : BerSU Ball
* URL         : http://codeforces.com/problemset/problem/489/B
* Occasion    : Codeforces Round #277.5 (Div. 2)
* Date        : Sept 20 2017
* Complexity  : O(1) 46ms
* Author      : Atiq Rahman
* Status      : Accepted
* Notes       : Simple angle calculation for hour and minute hands
* meta        : tag-geometry, tag-math
***************************************************************************/
using System;

public class Demo {
  public static void Main() {
    int  n = int.Parse(Console.ReadLine());   // discard n
    // dancing skill boys
    int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    int m = int.Parse(Console.ReadLine());   // discard m
    // dancing skill girls
    int[] b = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    Array.Sort(a);
    Array.Sort(b);

    bool[] occupied = new bool[b.Length];

    int match_count = 0;    // number of boys matched with girls
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (occupied[j] == false && Math.Abs(a[i] - b[j]) < 2) {
          match_count++;
          occupied[j] = true;
          break;
        }

    /* int girl_count = 0;    // number of girls matched with boys
    for (int j = 0; j < m; j++)
      for (int i = 0; i < n; i++)
        if (Math.Abs(a[i]-b[j]) < 2) {
          girl_count++;
          break;
        }*/

    Console.WriteLine(match_count);
  }
}

/* New strategy
 * sort first
 *
 * ----------------------------------
 Need to mark occuopied ones
 first come first server basis
 1 3
 5 2 6 0

 won't work

  say these are sorted

 we can keep a count who is got how many times..
 for each boy

  if frequency for a boy is 1. then assign the matching..

  occupied array set to true

   */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static int max(int a, int b)
        {
            if (a > b)
                return b;
            return a;
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string s = Console.ReadLine();
            int[] boys = new int[n];
            int x=0;
            int y=0;
            if (n == 1)
            {
                boys[0] = int.Parse(s.Substring(0));
            }
            else
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] == ' ')
                    {
                        boys[y] = int.Parse(s.Substring(x, i - x));
                        x = i + 1;
                        y++;
                    }
                }
                for (int i = s.Length - 1; i > -1; i--)
                {
                    if (s[i] == ' ')
                    {
                        boys[n - 1] = int.Parse(s.Substring(i + 1));
                        break;
                    }
                }
            }
            int m = int.Parse(Console.ReadLine());
            string s1 = Console.ReadLine();
            int[] girls = new int[m];
            x=0;
            y=0;
            if (m == 1)
            {
                girls[0] = int.Parse(s1.Substring(0));
            }
            else
            {
                for (int i = 0; i < s1.Length; i++)
                {
                    if (s1[i] == ' ')
                    {
                        girls[y] = int.Parse(s1.Substring(x, i - x));
                        x = i + 1;
                        y++;
                    }
                }
                for (int i = s1.Length - 1; i > -1; i--)
                {
                    if (s1[i] == ' ')
                    {
                        girls[m - 1] = int.Parse(s1.Substring(i + 1));
                        break;
                    }
                }
            }
            int[] numboys = new int[100];
            int[] numgirls = new int[100];
            int result = 0;
            for (int i = 0; i < n; i++)
            {
                numboys[boys[i] - 1]++;
            }
            for (int i = 0; i < m; i++)
            {
                numgirls[girls[i] - 1]++;
            }
            for (int i = 99; i > -1; i--)
            {
                if (i == 5)
                    i = 5;
                if (i < 99)
                {
                    int temp3 = max(numboys[i], numgirls[i + 1]);
                    result += temp3;
                    numboys[i] -= temp3;
                    numgirls[i+1] -= temp3;
                }
                int temp = max(numboys[i], numgirls[i]);
                result += temp;
                numboys[i] -= temp;
                numgirls[i] -= temp;
                if (i > 0)
                {
                    int temp2 = max(numboys[i], numgirls[i - 1]);
                    result += temp2;
                    numboys[i] -= temp2;
                    numgirls[i-1] -= temp2;
                }

            }
            Console.WriteLine(result);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());
            long[] boys = new long[n];
            string s = Console.ReadLine();
            int l = 0;
            int length = l + 1;
            for (long i = 0; i < boys.Length - 1; i++)
            {
                while (s[length] != ' ')
                    length++;
                boys[i] = long.Parse(s.Substring(l, length - l));
                l = length;
                length++;
            }
            boys[n - 1] = long.Parse(s.Substring(length));
            long m = long.Parse(Console.ReadLine());
            long[] girls = new long[m];
            s = Console.ReadLine();
            l = 0;
            length = l + 1;
            for (long i = 0; i < girls.Length - 1; i++)
            {
                while (s[length] != ' ')
                    length++;
                girls[i] = long.Parse(s.Substring(l, length - l));
                l = length;
                length++;
            }
            girls[m - 1] = long.Parse(s.Substring(length));
            Array.Sort(boys);
            Array.Sort(girls);
            if (boys[boys.Length - 1] < girls[0] || girls[girls.Length - 1] < boys[0])
            {
                Console.WriteLine("0");
                return;
            }
            long count = 0;
            for (long i = 0; i < boys.Length; i++)
            {
                for (long j = 0; j < girls.Length; j++)
                {
                    if (girls[j] != 0)
                    {
                        if (boys[i] - girls[j] < 2 && boys[i] - girls[j] > -2)
                        {
                            girls[j] = 0;
                            count++;
                            break;
                        }
                    }
                }
            }
            Console.WriteLine(count);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());
            long[] boys = new long[n];
            string s = Console.ReadLine();
            int l = 0;
            int length = l + 1;
            for (long i = 0; i < boys.Length - 1; i++)
            {
                while (s[length] != ' ')
                    length++;
                boys[i] = long.Parse(s.Substring(l, length - l));
                l = length;
                length++;
            }
            boys[n - 1] = long.Parse(s.Substring(length));
            long m = long.Parse(Console.ReadLine());
            long[] girls = new long[m];
            s = Console.ReadLine();
            l = 0;
            length = l + 1;
            for (long i = 0; i < girls.Length - 1; i++)
            {
                while (s[length] != ' ')
                    length++;
                girls[i] = long.Parse(s.Substring(l, length - l));
                l = length;
                length++;
            }
            girls[m - 1] = long.Parse(s.Substring(length));
            Array.Sort(boys);
            Array.Sort(girls);
            if (boys[boys.Length - 1] < girls[0] || girls[girls.Length - 1] < boys[0])
            {
                Console.WriteLine("0");
                return;
            }
            long count = 0;
            for (long i = 0; i < boys.Length; i++)
            {
                for (long j = 0; j < girls.Length; j++)
                {
                    if (girls[j] != 0)
                    {
                        if (boys[i] - girls[j] < 2 && boys[i] - girls[j] < -1)
                        {
                            girls[j] = 0;
                            count++;
                            break;
                        }
                    }
                }
            }
            Console.WriteLine(count);
        }
    }
}