#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
map<ll, ll>mp;
map<ll, ll>maxtime;
ll arr[N];
ll t[N];
ll r[N];
int main()
{
    int n;
    sci(n);
    ll maxx = 0;
    for (int i = 1; i <= n; ++i) {
        scll(arr[i]);
        maxx = max(maxx, arr[i]);
        ++mp[arr[i]];
    }
    for (int i = 1; i <= n; ++i) {
        scll(t[i]);
        maxtime[arr[i]] = max(maxtime[arr[i]], t[i]);
    }
    vector <pair<ll, ll>>vec;
    for (int i = 1; i <= n; ++i) {
        vec.push_back({ arr[i], t[i]});
    }
    sort(vec.begin(), vec.end());
    stack<pair<ll, ll>>st;
    for (int i = n - 1; i >= 0; --i)
    {
        while (!st.empty() && st.top().first >= vec[i].second)st.pop();
        if (st.empty())r[i] = -1;
        else r[i] = st.top().second;
        st.push({maxtime[vec[i].first], vec[i].first});
    }
    ll ans = 0;
    for (int i = 0; i < vec.size(); ++i) {
        if (mp[vec[i].first] > 1) {
            if (r[i] == -1) {
                ++maxx;
                ans += 1LL * (maxx - vec[i].first) * vec[i].second;
                ++mp[maxx];
            }
            else {
                ans += 1LL * (r[i] - vec[i].first) * vec[i].second;
                ++mp[r[i]];
            }
            --mp[vec[i].first];
        }
    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
map<ll, ll>mp;
map<ll, ll>maxtime;
ll arr[N];
ll t[N];
ll r[N];
int main()
{
    int n;
    sci(n);
    ll maxx = 0;
    for (int i = 1; i <= n; ++i) {
        scll(arr[i]);
        maxx = max(maxx, arr[i]);
        ++mp[arr[i]];
    }
    for (int i = 1; i <= n; ++i) {
        scll(t[i]);
        maxtime[arr[i]] = max(maxtime[arr[i]], t[i]);
    }
    vector <pair<ll, pair<ll, int>>>vec;
    vector < pair<ll, pair<ll, int>>>vec2;
    for (int i = 1; i <= n; ++i) {
        vec.push_back({ arr[i], {t[i], i} });
        vec2.push_back({ t[i], {arr[i], i} });
    }
    sort(vec.begin(), vec.end());
    sort(vec2.begin(), vec2.end());
    stack<pair<ll, ll>>st;
    for (int i = n - 1; i >= 0; --i)
    {
        while (!st.empty() && st.top().first >= vec[i].second.first)st.pop();
        if (st.empty())r[vec[i].second.second] = -1;
        else r[vec[i].second.second] = st.top().second;
        st.push({maxtime[vec[i].first], vec[i].first});
    }
    ll ans = 0;
    vector<pair<ll, ll>>temp;
    for (int i = 0; i < n; ++i) {
        if (mp[vec[i].first] > 1) {
            if (r[vec[i].second.second] == -1) {
                temp.push_back({ vec[i].second.first, vec[i].first });
                --mp[vec[i].first];
            }
            else {
                ans += 1LL * (r[vec[i].second.second] - vec[i].first) * vec[i].second.first;
                ++mp[r[vec[i].second.second]];
            }
            --mp[vec[i].first];
        }
    }
    sort(temp.rbegin(), temp.rend());
    for (int i = 0; i < temp.size(); ++i) {
        ++maxx;
        ans += (maxx - temp[i].second) * temp[i].first;
    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
pair<int, int>arr[N];
multiset<int>seet;
int main()
{
    int n;
    sci(n);
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].first);
    }
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].second);
    }
    sort(arr + 1, arr + n + 1);
    int cur = arr[1].first;
    ll ans = 0;
    ll sum = 0;
    for (int i = 1; i <= n || !seet.empty(); ) {
        while (i <= n && arr[i].first == cur) {
            sum += arr[i].second;
            seet.insert(arr[i].second);
            ++i;
        }
        if (seet.size() == 1) {
            sum = 0;
            seet.erase(seet.begin());
            cur = arr[i].first;
        }
        else {
            sum -= *(--seet.end());
            seet.erase(--seet.end());
            ++cur;
            ans += sum;
        }

    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
map<int, int>mp;
map<int, int>maxtime;
int arr[N];
int t[N];
int r[N];
int main()
{
    int n;
    sci(n);
    int maxx = 0;
    for (int i = 1; i <= n; ++i) {
        sci(arr[i]);
        maxx = max(maxx, arr[i]);
        ++mp[arr[i]];
    }
    for (int i = 1; i <= n; ++i) {
        sci(t[i]);
        maxtime[arr[i]] = max(maxtime[arr[i]], t[i]);
    }
    vector <pair<int, int>>vec;
    for (int i = 1; i <= n; ++i) {
        vec.push_back({ arr[i], t[i]});
    }
    sort(vec.begin(), vec.end());
    stack<ii>st;
    for (int i = n - 1; i >= 0; --i)
    {
        while (!st.empty() && st.top().first >= vec[i].second)st.pop();
        if (st.empty())r[i] = -1;
        else r[i] = st.top().second;
        st.push({maxtime[vec[i].first], vec[i].first});
    }
    ll ans = 0;
    for (int i = 0; i < vec.size(); ++i) {
        if (mp[vec[i].first] > 1) {
            if (r[i] == -1) {
                ++maxx;
                ans += 1LL * (maxx - vec[i].first) * vec[i].second;
                ++mp[maxx];
            }
            else {
                ans += 1LL * (r[i] - vec[i].first) * vec[i].second;
                ++mp[r[i]];
            }
            --mp[vec[i].first];
        }
    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
pair<int, int>arr[N];
multiset<int>seet;
int main()
{
    int n;
    sci(n);
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].first);
    }
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].second);
    }
    sort(arr + 1, arr + n + 1);
    int cur = arr[1].first;
    ll ans = 0;
    int sum = 0;
    for (int i = 1; i <= n; ) {
        while (i <= n && arr[i].first == cur) {
            sum += arr[i].second;
            seet.insert(arr[i].second);
            ++i;
        }
        if (seet.size() == 1) {
            sum = 0;
            seet.erase(seet.begin());
            cur = arr[i].first;
        }
        else {
            sum -= *(--seet.end());
            seet.erase(--seet.end());
            ++cur;
        }
        ans += sum;
    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
pair<int, int>arr[N];
multiset<int>seet;
int main()
{
    int n;
    sci(n);
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].first);
    }
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].second);
    }
    sort(arr + 1, arr + n + 1);
    int cur = arr[1].first;
    ll ans = 0;
    int sum = 0;
    for (int i = 1; i <= n; ) {
        while (i <= n && arr[i].first == cur) {
            sum += arr[i].second;
            seet.insert(arr[i].second);
            ++i;
        }
        if (seet.size() == 1) {
            sum = 0;
            seet.erase(seet.begin());
            cur = arr[i].first;
        }
        else {
            sum -= *(--seet.end());
            seet.erase(--seet.end());
            ++cur;
            ans += sum;
        }

    }
    pll(ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100005];
int b[100005];
pair<int, int>pr[100005];
priority_queue<pair<int,int>>q;
signed main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		pr[i].first = a[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
		pr[i].second = b[i];
	}
	sort(pr + 1, pr + n + 1);
	int cur = 0;
	int sum = 0;
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		while (!q.empty() && cur < pr[i].first)
		{
			sum -= q.top().second;
			ans += sum;
			q.pop();
			cur++;
		}
		sum += pr[i].second;
		q.push(pr[i]);
		cur = pr[i].first;
	}

	while (!q.empty() && cur < 10000000000000)
	{
		sum -= q.top().second;
		ans += sum;
		q.pop();
		cur++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[200005];
int b[200005];
pair<int, int>pr[200005];
priority_queue<int>q;
signed main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		pr[i].first = a[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
		pr[i].second = b[i];
	}
	sort(pr + 1, pr + n + 1);
	int cur = 0;
	int sum = 0;
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		while (!q.empty() && cur < pr[i].first)
		{
			sum -= q.top();
			ans += sum;
			q.pop();
			cur++;
		}
		sum += pr[i].second;
		q.push(pr[i].second);
		cur = pr[i].first;
	}

	while (!q.empty() && cur < 10000000000000)
	{
		sum -= q.top();
		ans += sum;
		q.pop();
		cur++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[200005];
int b[200005];
pair<int, int>pr[200005];
priority_queue<pair<int,int>>q;
signed main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		pr[i].first = a[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
		pr[i].second = b[i];
	}
	sort(pr + 1, pr + n + 1);
	int cur = 0;
	int sum = 0;
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		while (!q.empty() && cur < pr[i].first)
		{
			sum -= q.top().second;
			ans += sum;
			q.pop();
			cur++;
		}
		sum += pr[i].second;
		q.push(pr[i]);
		cur = pr[i].first;
	}

	while (!q.empty() && cur < 10000000000000)
	{
		sum -= q.top().second;
		ans += sum;
		q.pop();
		cur++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int, int>f;
struct node
{
	int a;
	int b;
}; node nd[200005];
int fd(int x)
{
	if (x == f[x])return x;
	return f[x] = fd(f[x]);
}
void merge(int x, int y)
{
	x = fd(x);
	y = fd(y);
	if (x == y)return;
	if (x > y)f[y] = x;
	else f[x] = y;
}
bool cmp(node x,node y)
{
	return x.b > y.b;
}
signed main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].a;
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].b;
	}
	sort(nd + 1, nd + 1 + n, cmp);
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		if (f[nd[i].a] == 0)
		{
			f[nd[i].a] = nd[i].a;
			if (nd[i].a - 1 > 0 && f[nd[i].a - 1] != 0)merge(nd[i].a, nd[i].a - 1);
			if (f[nd[i].a + 1] != 0)merge(nd[i].a, nd[i].a + 1);
		}
		else
		{
			if (nd[i].a - 1 > 0 && f[nd[i].a - 1] != 0)merge(nd[i].a, nd[i].a - 1);
			if (f[nd[i].a + 1] != 0)merge(nd[i].a, nd[i].a + 1);
			int x = fd(nd[i].a);
			x++;
			f[x] = x;
			ans += (x - nd[i].a)*nd[i].b;
			if (f[x + 1] != 0)merge(x + 1, x);
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int, int>f;
struct node
{
	int a;
	int b;
}; node nd[200005];
int fd(int x)
{
	if (x == f[x])return x;
	return f[x] = fd(f[x]);
}
void merge(int x, int y)
{
	x = fd(x);
	y = fd(y);
	if (x == y)return;
	if (x > y)f[y] = x;
	else f[x] = y;
}
bool cmp(node x,node y)
{
	return x.b > y.b;
}
signed main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].a;
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].b;
	}
	sort(nd + 1, nd + 1 + n, cmp);
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		if (f[nd[i].a] == 0)f[nd[i].a] = nd[i].a;
		else
		{
			int x = fd(nd[i].a);
			x++;
			f[x] = x;
			ans += (x - nd[i].a)*nd[i].b;
			if (f[x + 1] != 0)merge(x + 1, x);
		}
		if (nd[i].a - 1 > 0 && f[nd[i].a - 1] != 0)merge(nd[i].a, nd[i].a - 1);
		if (f[nd[i].a + 1] != 0)merge(nd[i].a, nd[i].a + 1);
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100005];
int b[100005];
pair<int, int>pr[100005];
priority_queue<int>q;
signed main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		pr[i].first = a[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> b[i];
		pr[i].second = b[i];
	}
	sort(pr + 1, pr + n + 1);
	int cur = 0;
	int sum = 0;
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		while (!q.empty() && cur < pr[i].first)
		{
			sum -= q.top();
			ans += sum;
			q.pop();
			cur++;
		}
		sum += pr[i].second;
		q.push(pr[i].second);
		cur = pr[i].first;
	}

	while (!q.empty() && cur < 10000000000000)
	{
		sum -= q.top();
		ans += sum;
		q.pop();
		cur++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int, int>f;
struct node
{
	int a;
	int b;
}; node nd[200005];
int fd(int x)
{
	if (x == f[x])return x;
	return f[x] = fd(f[x]);
}
void merge(int x, int y)
{
	x = fd(x);
	y = fd(y);
	if (x == y)return;
	if (x > y)f[y] = x;
	else f[x] = y;
}
bool cmp(node x,node y)
{
	return x.b > y.b;
}
signed main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].a;
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> nd[i].b;
	}
	sort(nd + 1, nd + 1 + n, cmp);
	int ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		if (f[nd[i].a] == 0)f[nd[i].a] = nd[i].a;
		else
		{
			int x = fd(nd[i].a);
			x++;
			while (f[x] != 0)
			{
				merge(nd[i].a, x);
				x = fd(x);
				x++;
			}
			f[x] = x;
			ans += (x - nd[i].a)*nd[i].b;
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    vector<vector<int>> v;
    for(int i=0;i<n;i++)
        cin>>b[i].ff;
    for(int i=0;i<n;i++)
        cin>>b[i].ss;
    sort(b,b+n);
    for(int i=1;i<=n;i++){
        if(i==(n)||b[i].ff!=b[i-1].ff){
            vector<int> v1;
            v1.pb(b[i-1].ff);
            ll j = i-1;
            while(b[j].ff==b[i-1].ff&&j>=0)v1.pb(b[j].ss),j--;
            v.pb(v1);
        }
    }
    ll ans=0;
    ll n1 = v.size();
    for(int i = 0;i<n1;i++){
        sort(++(v[i].begin()),v[i].end());
        ll l=v[i].size();
       // cout<<l<<endl;
        ll x =1000000007;
        ll y =*(v[i].begin());
        if(i!=n1-1) x=*(v[i+1].begin());
        for(int j=l-2;j>0;j--){
            if(i==(n1-1)||(x-y)>(l-1-j)){
                ans+=(l-1-j)*v[i][j];
            }
            else{
                ans+=(x-y)*v[i][j];
                v[i+1].pb(v[i][j]);
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    for(int i=0;i<n;i++)
        cin>>b[i].ff;
    for(int i=0;i<n;i++)
        cin>>b[i].ss;
    sort(b,b+n);
    ll c1=1,ans=0;
    g[b[0].ss]++;
    for(int i=1;i<=n;i++){
        if(i==(n)||b[i].ff!=b[i-1].ff){
           map<ll,ll> g1;
           ll y=1;
           auto t1=--g.end(),t2=g.end();
           t1->ss--;
           ll x =(i==(n)?(2*M):b[i].ff);
           while(t2!=g.begin()){
               if(t1->ss==0)t1--,t2--;
               else{
                  if(y<x-b[i-1].ff) ans+=y*(t1->ff),y++;
                  else{
                      ans+=(x-b[i-1].ff)*t1->ff;
                      g1[t1->ff]++;
                  }
                  t1->ss--;
               }
           }
           g.clear();
           g=g1,c1=1;
           if(i!=n)g[b[i].ss]++;
        }
        else{
            c1++;
            g[b[i].ss]++;
        }
    }

    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n=0,ans=0,s=0;
    cin>>n;
    pair<ll,ll> b[n];
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll,greater<ll>> ms;
    ms.insert(b[0].ss);
    s=b[0].ss;
    rp(i,1,n-1){
        if(b[i].ff==b[i-1].ff){
            s+=b[i].ss;
            ms.insert(b[i].ss);
        }
        else{
            ll i1=0;
            for(auto it=ms.begin();it!=ms.end()&&(i1<b[i].ff-b[i-1].ff);){
                ans+=i1*(*it);
                auto it1=it;
                it++;
                if(b[i-1].ff+i1<b[i].ff){
                    s-=(*it1);
                    ms.erase(it1);
                    i1++;
                }
            }
            ans+=s*i1;
            s+=b[i].ss;
            ms.insert(b[i].ss);
        }
    }
    //cout<<ans<<endl;
    int i1=0;
    for(auto j:ms){
        ans+=i1*j;
        i1++;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    vector<vi> v;
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll> ms;
    ll x=M;
    ll c[n];
    c[n-1]=x;
    repr(i,n-1){
        if(b[i].ff!=b[i+1].ff){
            x=b[i+1].ff;
        }
        c[i]=x;
    }
    ll l=1,s=0,ans=0;
    for(int i=1;i<n;i++){
        ans+=s;
        ms.insert(b[i].ss);
        s+=b[i].ss;
        if(b[i].ff!=b[i-1].ff){
            s-=*(--ms.end());
            ms.erase(--ms.end());
            l=1;
        }
        else{
            ans+=b[i].ss*l;
            if(c[i]-b[i].ff>l){
                s-=*(--ms.end());
                ms.erase(--ms.end());
                l++;
            }
        }
    }
    while(ms.end()!=ms.begin()){
        ans+=s;
        s-=*(--ms.end());
        ms.erase(--ms.end());
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n=0,ans=0;
    cin>>n;
    pair<ll,ll> b[n];
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll,greater<ll>> ms;
    ms.insert(b[0].ss);
    rp(i,1,n-1){
        if(b[i].ff==b[i-1].ff)
            ms.insert(b[i].ss);
        else{
            ll i1=0;
            for(auto it=ms.begin();it!=ms.end();){
                ans+=i1*(*it);
                auto it1=it;
                it++;
                if(b[i-1].ff+i1<b[i].ff){
                    ms.erase(it1);
                    i1++;
                }
            }
            ms.insert(b[i].ss);
        }
    }
    //cout<<ans<<endl;
    int i1=0;
    for(auto j:ms){
        ans+=i1*j;
        i1++;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    for(int i=0;i<n;i++)
        cin>>b[i].ff;
    for(int i=0;i<n;i++)
        cin>>b[i].ss;
    sort(b,b+n);
    ll c1=1,ans=0;
    g[b[0].ss]++;
    for(int i=1;i<=n;i++){
        if(i==(n)||b[i].ff!=b[i-1].ff){
           map<ll,ll> g1;
           ll y=1,t=0;;
           auto t1=--g.end(),t2=g.end();
           t1->ss--;
           ll x =(i==(n)?(2*M):b[i].ff);
           while(t2!=g.begin()){
               if(t1->ss==0)t1--,t2--;
               else if(t>0){
                   ans+=(t1->ss)*(x-b[i-1].ff)*(t1->ff);
                   t1--,t2--;
               }
               else{
                  if(y<x-b[i-1].ff) ans+=y*(t1->ff),y++,t1->ss--;
                  else{
                      ans+=(t1->ss)*(x-b[i-1].ff)*t1->ff;
                      t1--,t2--;
                      t++;
                  }
               }
           }
           vector<map<ll,ll>::iterator> v;
           for(auto j=g.begin();j!=g.end();j++)if(j->ss==0)v.pb(j);
           for(auto j=v.begin();j!=v.end();j++)g.erase(*j);
           c1=1;
           if(i!=n)g[b[i].ss]++;
        }
        else{
            c1++;
            g[b[i].ss]++;
        }
    }
    cout<<ans<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.141592653
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define M 1000000007
#define ub upper_bound
#define lb lower_bound
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    int t1=0;
    for(int i=0;i<n;i++){
        cin>>b[i].ff;
        g[b[i].ff]++;
        if(g[b[i].ff]>1)t1++;
    }
    for(int i=0;i<n;i++)cin>>b[i].ss;
    if(t1==0)cout<<0<<endl;
    else{
        sort(b,b+n);
        ll x1 =b[0].ff+1;
        vector<ll> v;
        vector<pair<ll,ll>> v1;
        v.pb(b[0].ss);
        int c=1;
        int ans =0;
        for(int i=1;i<n;i++){
            if(b[i].ff!=b[i-1].ff){
                if(c>1);
                {
                    sort(v.begin(),v.end());
                    //x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
                    while(c!=1){
                        v1.pb(mp(v[c-2],b[i-1].ff));
                        c--;
                    }
                }
                v.clear();
                v.pb(b[i].ss);
            }
            else {
                c++;
                //v.clear();
                v.pb(b[i].ss);
            }
        }
        sort(v1.begin(),v1.end());
        while(v1.end()!=v1.begin()){
            auto t =(*(--v1.end()));
            ll x=t.ss+1;
            while(g.find(x)!=g.end()){
                x++;
                ans+=t.ff;
            }
            ans+=t.ff;
            v1.pp();
            g[x]++;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    vector<vi> v;
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll> ms;
    ll x=M;
    ll c[n];
    c[n-1]=x;
    repr(i,n-1){
        if(b[i].ff!=b[i+1].ff){
            x=b[i+1].ff;
        }
        c[i]=x;
    }
    ll l=1,s=0,ans=0;
    for(int i=1;i<n;i++){
        ans+=s;
        ms.insert(b[i].ss);
        s+=b[i].ss;
        if(b[i].ff!=b[i-1].ff){
            s-=*(--ms.end());
            ms.erase(--ms.end());
            l=1;
        }
        else{
            if(c[i]-b[i].ff>l){
                s-=*(--ms.end());
                ms.erase(--ms.end());
            }
        }
    }
    while(ms.end()!=ms.begin()){
        ans+=s;
        s-=*(--ms.end());
        ms.erase(--ms.end());
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.141592653
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define M 1000000007
#define ub upper_bound
#define lb lower_bound
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    int t1=0;
    for(int i=0;i<n;i++){
        cin>>b[i].ff;
        g[b[i].ff]++;
        if(g[b[i].ff]>1)t1++;
    }
    for(int i=0;i<n;i++)cin>>b[i].ss;
    if(t1==0)cout<<0<<endl;
    else{
        sort(b,b+n);
        ll x1 =b[0].ff+1;
        vector<ll> v;
        vector<pair<ll,ll>> v1;
        v.pb(b[0].ss);
        int c=1;
        int ans =0;
        for(int i=1;i<n;i++){
            if(b[i].ff!=b[i-1].ff){
                if(c>1);
                {
                    sort(v.begin(),v.end());
                    //x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
                    while(c!=1){
                        v1.pb(mp(v[c-2],b[i-1].ff));
                        c--;
                    }
                }
                v.clear();
                v.pb(b[i].ss);
            }
            else {
                c++;
                //v.clear();
                v.pb(b[i].ss);
            }
        }
        sort(v1.begin(),v1.end());
        while(v1.end()!=v1.begin()){
            auto t =(*(--v1.end()));
            ll x=t.ss+1;
            while(g.find(x)!=g.end()){
                x++;
                ans+=t.ff;
            }
            ans+=t.ff;
            v1.pp();
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.141592653
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define M 1000000007
#define ub upper_bound
#define lb lower_bound
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    int t1=0;
    for(int i=0;i<n;i++){
        cin>>b[i].ff;
        g[b[i].ff]++;
        if(g[b[i].ff]>1)t1++;
    }
    for(int i=0;i<n;i++)cin>>b[i].ss;
    if(t1==0)cout<<0<<endl;
    else{
        sort(b,b+n);
        ll x1 =b[0].ff+1;
        ll x2=b[0].ss;
        int c=1;
        int ans =0;
        for(int i=1;i<n;i++){
            if(b[i].ff!=b[i-1].ff){
                if(c>1);
                {
                    x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
                    while(c!=1){
                        if(c!=1)ans+=x2;
                        if(g.find(x1)==g.end()){
                            c--;
                            //ans+=b[i-1].ss;
                            g[x1]++;
                        }
                        x1++;
                    }
                }
                x2=b[i].ss;
            }
            else {
                c++;
                x2=x2<b[i].ss?x2:b[i].ss;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i = 0 ; i<n ; i++)
#define repr(i,n) for(ll i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n=0,ans=0,s=0,p=0;
    cin>>n;
    pair<ll,ll> b[n];
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll> ms;
    ll c[n];
    c[n-1]=2*M;
    repr(i,n-1)
        c[i]=(b[i].ff!=b[i+1].ff)?(b[i+1].ff):c[i+1];
    rp(i,1,n-1){
        if(b[i].ff!=b[i-1].ff){
            ans+=s;
            s+=b[i].ss;
            ms.insert(b[i].ss);
            s-=(*(--ms.end()));
            ms.erase(--ms.end());
            p=0;
        }
        else{
            if(b[i].ff+p<c[i]-1){
                ans+=s;
                s+=b[i].ss;
                ms.insert(b[i].ss);
                s-=(*(--ms.end()));
                ms.erase(--ms.end());
                p++;
            }
            else{
                s+=b[i].ss;
                ms.insert(b[i].ss);
            }
            ans+=p*b[i].ss;
        }
    }
    while(ms.begin()!=ms.end()){
        ans+=s;
        s-=(*(--ms.end()));
        ms.erase(--ms.end());
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n=0,ans=0;
    cin>>n;
    pair<ll,ll> b[n];
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll,greater<ll>> ms;
    rp(i,1,n-1){
        if(b[i].ff==b[i-1].ff)
            ms.insert(b[i].ss);
        else{
            int i1=0;
            for(auto it=ms.begin();it!=ms.end();){
                ans+=i1*(*it);
                auto it1=it;
                it++;
                if(b[i-1].ff+i1<b[i].ff){
                    ms.erase(it1);
                    i1++;
                }
            }
            ms.insert(b[i].ss);
        }
    }
    //cout<<ans<<endl;
    for(int i1=0;ms.end()!=ms.begin();i1++){
        ans+=i1*(*(ms.begin()));
        ms.erase(ms.begin());
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pi 3.141592653
#define exp 2.71828
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define rp(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
bool svp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    if(a.first!=b.first)
        return (a.first < b.first);
    return (a.second>b.second);
}
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n=0,ans=0,s=0,p=0;
    cin>>n;
    pair<ll,ll> b[n];
    rep(i,n)
        cin>>b[i].ff;
    rep(i,n)
        cin>>b[i].ss;
    sort(b,b+n,svp);
    multiset<ll> ms;
    ll c[n];
    c[n-1]=2*M;
    repr(i,n-1)
        c[i]=(b[i].ff!=b[i+1].ff)?(b[i+1].ff):c[i+1];
    rp(i,1,n-1){
        if(b[i].ff!=b[i-1].ff){
            ans+=s;
            s+=b[i].ss;
            ms.insert(b[i].ss);
            s-=(*(--ms.end()));
            ms.erase(--ms.end());
            p=0;
        }
        else{
            if(b[i].ff+p<c[i]-1){
                ans+=s;
                s+=b[i].ss;
                ms.insert(b[i].ss);
                s-=(*(--ms.end()));
                ms.erase(--ms.end());
                p++;
            }
            else{
                s+=b[i].ss;
                ms.insert(b[i].ss);
            }
            ans+=p*b[i].ss;
        }
    }
    //cout<<ans<<endl;
    while(ms.begin()!=ms.end()){
        ans+=s;
        s-=(*(--ms.end()));
        ms.erase(--ms.end());
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.141592653
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define M 1000000007
#define ub upper_bound
#define lb lower_bound
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    int t1=0;
    for(int i=0;i<n;i++){
        cin>>b[i].ff;
        g[b[i].ff]++;
        if(g[b[i].ff]>1)t1++;
    }
    for(int i=0;i<n;i++)cin>>b[i].ss;
    if(t1==0)cout<<0<<endl;
    else{
        sort(b,b+n);
        ll x1 =b[0].ff+1;
        vector<ll> v;
        vector<pair<ll,ll>> v1;
        v.pb(b[0].ss);
        int c=1;
        int ans =0;
        for(int i=1;i<n;i++){
            if(b[i].ff!=b[i-1].ff){
                if(c>1);
                {
                    sort(v.begin(),v.end());
                    //x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
                    while(c!=1){
                        v1.pb(mp(v[c-2],b[i-1].ff));
                        c--;
                    }
                }
                v.clear();
                v.pb(b[i].ss);
            }
            else {
                c++;
                //v.clear();
                v.pb(b[i].ss);
            }
        }
        if(c>1)
        {
            sort(v.begin(),v.end());
            //x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
            while(c!=1){
                v1.pb(mp(v[c-2],b[n-1].ff));
                c--;
            }
            v.clear();
                //v.pb(b[i].ss);
        }
        sort(v1.begin(),v1.end());
        while(v1.end()!=v1.begin()){
            auto t =(*(--v1.end()));
            ll x=t.ss+1;
            while(g.find(x)!=g.end()){
                x++;
                ans+=t.ff;
            }
            ans+=t.ff;
            v1.pp();
            g[x]++;
            //cout<<t.ff<<" "<<t.ss<<endl;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.141592653
#define pb push_back
#define pp pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define M 1000000007
#define ub upper_bound
#define lb lower_bound
#define rep(i,n) for(int i = 0 ; i<n ; i++)
#define repr(i,n) for(int i = n-1 ; i>=0 ; i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define SORT(v) sort(v.begin(),v.end())
void solve();
signed main()
{
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    ll n;
    cin>>n;
    pair<ll,ll> b[n];
    map<ll,ll> g;
    int t1=0;
    for(int i=0;i<n;i++){
        cin>>b[i].ff;
        g[b[i].ff]++;
        if(g[b[i].ff]>1)t1++;
    }
    for(int i=0;i<n;i++)cin>>b[i].ss;
    if(t1==0)cout<<0<<endl;
    else{
        sort(b,b+n);
        ll x1 =b[0].ff+1;
        vector<ll>v;
        v.pb(b[0].ss);
        int c=1;
        int ans =0;
        for(int i=1;i<n;i++){
            if(b[i].ff!=b[i-1].ff){
                if(c>1);
                {
                    sort(v.begin(),v.end());
                    x1=(x1)>(b[i-1].ff+1)?(x1):(b[i-1].ff+1);
                    while(c!=1){
                        if(c!=1)ans+=v[c-2];
                        if(g.find(x1)==g.end()){
                            c--;
                            //ans+=b[i-1].ss;
                            g[x1]++;
                        }
                        x1++;
                    }
                }
                v.clear();
                v.pb(b[i].ss);
            }
            else {
                c++;
                //v.clear();
                v.pb(b[i].ss);
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 2e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
//    auto comp = [&](const pair<int, int> &lhs, const pair<int, int> &rhs) -> bool {
//        return lhs.second < rhs.second;
//    };
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || !now.empty();) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j].second);
            sum_now += category[j].second;
            ++j;
        }
        sum_now -= *now.rbegin();
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 2e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
    auto comp = [&](const pair<int, int> &lhs, const pair<int, int> &rhs) -> bool {
        return lhs.second < rhs.second;
    };
    multiset<pair<int, int>, decltype(comp)> now(comp);
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || now.size() > 1;) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j]);
            sum_now += category[j].second;
            ++j;
        }
        sum_now -= now.rbegin()->second;
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 4e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> t[i];
    for (int i = 0; i < n; ++i)
        category[i] = {a[i], t[i]};
    sort(category, category + n);
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    int cnt_now = -1;
    for (int i = 0; i < n;) {
        int j = i;
        while (j < n && (now.empty() || cnt_now == category[j].first)) {
            now.insert(category[j].second);
            cnt_now = category[j].first;
            sum_now += category[j].second;
            ++j;
        }
        assert(!now.empty());
        sum_now -= *now.rbegin();
        now.erase(--now.end());
        res += sum_now;
        i = j;
        cnt_now++;
    }
    cout << res;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 4e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> t[i];
    for (int i = 0; i < n; ++i)
        category[i] = {a[i], t[i]};
    sort(category, category + n);
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    int cnt_now = -1;
    for (int i = 0; i < n || !now.empty();) {
        int j = i;
        while (j < n && (now.empty() || cnt_now == category[j].first)) {
            now.insert(category[j].second);
            cnt_now = category[j].first;
            sum_now += category[j].second;
            ++j;
        }
        assert(!now.empty());
        sum_now -= *now.rbegin();
        now.erase(--now.end());
        res += sum_now;
        i = j;
        cnt_now++;
    }
    cout << res;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 4e6 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || !now.empty();) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j].second);
            sum_now += category[j].second;
            ++j;
        }
        sum_now -= *now.rbegin();
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 2e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
    multiset<pair<int, int>> now;
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || now.size() > 1;) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j]);
            sum_now += category[j].second;
            ++j;
        }
        sum_now -= now.rbegin()->second;
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 4e6 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || !now.empty();) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j].second);
            sum_now += category[j].second;
            ++j;
        }
        assert(!now.empty());
        sum_now -= *now.rbegin();
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 4e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> t[i];
    for (int i = 0; i < n; ++i)
        category[i] = {a[i], t[i]};
    sort(category, category + n);
    multiset<int> now;
    int sum_now = 0;
    int res = 0;
    int cnt_now = 0;
    for (int i = 0; i < n; ++i) {
        int j = i;
        while (j < n && (now.empty() || cnt_now == category[j].first)) {
            now.insert(category[j].second);
            cnt_now = category[j].first;
            sum_now += category[j].second;
            ++j;
        }
        assert(!now.empty());
        sum_now -= *now.rbegin();
        now.erase(--now.end());
        res += sum_now;
        i = j;
        cnt_now++;
    }
    cout << res;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC optmize("sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1")
//#pragma GCC optmize("fast-math")
//#pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define trace(x) cout << #x "=" << x << endl
#define int128 __int128

#define int long long

const int mod = 1e9 + 7;

const int maxn = 2e5 + 10;

int a[maxn];
int t[maxn];
pair<int, int> category[maxn];

signed main() {
    cin.tie();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        category[i] = {a[i], t[i]};
    }
    sort(category, category + n);
    auto comp = [&](const pair<int, int> &lhs, const pair<int, int> &rhs) -> bool {
        return lhs.second < rhs.second;
    };
    multiset<pair<int, int>, decltype(comp)> now(comp);
    int sum_now = 0;
    int res = 0;
    for (int i = 0; i < n || now.size() > 0;) {
        int j = i;
        while (j < n && category[i].first == category[j].first) {
            now.insert(category[j]);
            sum_now += category[j].second;
            ++j;
        }
        sum_now -= now.rbegin()->second;
        now.erase(now.find(*now.rbegin()));
        res += sum_now;
        i = j;
    }
    cout << res;
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.nd != y.nd) return x.nd > y.nd;
            return x.st < y.st;
         });
    if(a[0].st == a[n - 1].st){
        sort(a, a + n);
        ll sum = 0;
        for(int i = 0; i < n; i++){
            sum += (n - i - 1) * a[i].nd;
        }
        cout << sum << "\n";
        return;
    }
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        if(mp2.count(a[i].st)) x = mp2[a[i].st] + 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
            ll t = x;
            if(mp2.count(x)) {
                x = mp2[x] + 1;
                res += y * (x - t);
            }
        }
        mp[x] = y;
        mp2[a[i].st] = x;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    unordered_map<ll, ll> mp;
    unordered_map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.nd != y.nd) return x.nd > y.nd;
            return x.st < y.st;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        if(mp2.count(a[i].st)) x = mp2[a[i].st] + 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
        mp2[a[i].st] = x;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.nd != y.nd) return x.nd > y.nd;
            return x.st < y.st;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        if(mp2.count(a[i].st)) x = mp2[a[i].st] + 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
        mp2[a[i].st] = x;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
        mp2[a[i].st]++;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.st != y.st) return x.st < y.st;
            return x.nd < y.nd;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        x += mp2[a[i].st] - 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
        mp2[a[i].st]--;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
//ll a[N], b[N];

void Solve(){
    int n;
    cin >> n;
    map<int, int> mp;
    vector<pair<int, int>> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
        mp[a[i].st]++;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    int mx = 0;
    sort(all(a));
    ll res = 0;
    for(int i = 0; i < n; i++){
        int x = a[i].st;
        mx = max(mx, x);
        if(mp[x] > 1){
            while(mp[mx]) mx++, res += a[i].nd;
            mp[mx] = 1;
            mp[x]--;
        }
    }
    cout << res << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.nd != y.nd) return x.nd > y.nd;
            return x.st < y.st;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        if(mp2.count(a[i].st)) x = mp2[a[i].st] + 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
            ll t = x;
            if(mp2.count(x)) {
                x = mp2[x] + 1;
                res += y * (x - t);
            }
        }
        mp[x] = y;
        mp2[a[i].st] = x;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    unordered_map<ll, ll> mp;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.nd != y.nd) return x.nd > y.nd;
            return x.st < y.st;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = max(mx, a[i].st);
        y = a[i].nd;
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    sort(a, a + n, [](pair<ll, ll> x, pair<ll, ll> y){
            if(x.st != y.st) return x.st < y.st;
            return x.nd > y.nd;
         });
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        if(mp2.count(a[i].st)) x = mp2[a[i].st] + 1;
        res += y * (x - a[i].st);
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
        mp2[a[i].st] = x;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define lwb             lower_bound
#define upb             upper_bound
#define all(a)          (a).begin(), (a).end()
#define Reset(s, n)     memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define cntbit(n)       __builtin_popcountll(ll(n))

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int MOD = 1e9 + 7;
const ll INF = 1e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-12;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, ll p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline void onbit(ll &a, int i){a |= 1LL << i;}
inline void offbit(ll &a, int i){onbit(a, i), a -= (1LL << i);}

const int N = 3e5 + 5;

ll n, m;
int a[N];
int t[N];

void solve(){
   cin >> n;
   map<ll, vector<ll>> mp;
   for(int i = 0; i < n; i++){
      cin >> a[i];
   }
   for(int i = 0; i < n; i++){
      cin >> t[i];
      mp[a[i]].pb(t[i]);
   }
   priority_queue<pair<ll, ll>> prique;
   ll res = 0;
   for(auto it: mp){
      ll publication = it.st;
      ll start = it.st;
      for(ll x: it.nd) prique.push({x, start});
      while(!prique.empty()){
         res += prique.top().st * (publication - prique.top().nd);
         publication++;
         prique.pop();
         if(mp.count(publication)){
            for(ll x: mp[publication]){
               prique.push({x, publication});
            }
            mp.erase(publication);
            start = publication;
         }
      }
   }
   cout << res << "\n";

}

int main(){
   ios_base::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);
   cout << fixed << setprecision(10);
   //    freopen("input.txt", "r", stdin);
   //    freopen("output.txt", "w", stdout);
   int query = 1;
   //   cin >> query;
   int start = 1000 * clock() / CLOCKS_PER_SEC;
   while(query--) solve();
   cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}


#include <bits/stdc++.h>

#define st              first
#define nd              second
#define pb              push_back
#define pf              push_front
#define all(a)          (a).begin(), (a).end()
#define lwb             lower_bound
#define upb             upper_bound
#define vvll            vector<vector<ll> >
#define Reset(s, n)      memset(s, n, sizeof(s))
#define Sz(a)           int((a).size())
#define Max(a, b, c)    max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c)    min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a)       (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i)       (((n) >> (i)) & 1)
#define onbit(n, i)     ((n) |= (1LL << (i)))
#define offbit(n, i)    onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n)     __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline bool isPrime (ll x){ if(x <= 1) return 0; if(x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for(ll i = 5; i <= s; i += 6){if (!(x % i) || !(x % (i + 2))) return 0;}return 1;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int N = 2e5 + 5;

ll n, m, k;
pair<ll, ll> a[N];

void Solve(){
    cin >> n;
    unordered_map<ll, ll> mp;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
    for(ll i = 0, x, y; i < n; i++){
        x = a[i].st;
        y = a[i].nd;
        while(mp[x]) {
            if(mp[x] < y){
                swap(mp[x], y);
            }
            x++;
            res += y;
        }
        mp[x] = y;
    }
    cout << res << "\n";
}
/*
5
3 3 4 4 3
5 9 5 7 5
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int query = 1;
//    cin >> query;
    int start = 1000 * clock() / CLOCKS_PER_SEC;
    while(query--) Solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC  - start << "ms\n";
}