#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  auto cmp =
    [&](int a, int b) -> bool {
      if (cost[a] != cost[b]) {
        return cost[a] > cost[b];
      }
      if (cnt[a] != cnt[b]) {
        return cnt[a] < cnt[b];
      }
      return a < b;
    };

  vector<int> st(n);
  iota(st.begin(), st.end(), 0);
  sort(st.begin(), st.end(), cmp);

    set<int> avai;
    int lo = *min_element(cnt.begin(), cnt.end());
    int hi = *max_element(cnt.begin(), cnt.end()) + n;
    for (int i = lo; i <= hi; ++i) avai.insert(i);

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      int cur = st[i];
      int l = *avai.lower_bound(cnt[cur]);
      avai.erase(l);
      ans += cost[cur] * (l - cnt[cur]);
    }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  auto cmp1 =
    [&] (int a, int b) -> bool {
      if (cnt[a] != cnt[b]) return cnt[a] < cnt[b];
      if (cost[a] != cost[b]) return cost[a] > cost[b];
      return a < b;
    };

  auto cmp2 =
    [&](int a, int b) -> bool {
      if (cost[a] != cost[b]) return cost[a] > cost[b];
      if (cnt[a] != cnt[b]) return cnt[a] < cnt[b];
      return a < b;
    };

  vector<int> all(n);
  iota(all.begin(), all.end(), 0);
  sort(all.begin(), all.end(), cmp1);

  set<int, decltype(cmp2)> in(cmp2);
  int lo = 0;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = cnt[all[i]];
    in.insert(all[i]);
    while (in.size() and lo <= cur) {
      int t = *in.begin();
      lo = max(lo, cnt[t]);
      ans += cost[t] * (lo - cnt[t]);
      in.erase(t);
      ++lo;
    }
  }

  while (in.size()) {
    int t = *in.begin();
    lo = max(lo, cnt[t]);
    ans += cost[t] * (lo - cnt[t]);
    in.erase(t);
    ++lo;
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    ++mp[cnt[i]];
  }

  set<int> all;
  int cur = 0;
  for (auto& p : mp) {
    int r = p.second;
    int from = max(cur, p.first);
    while (r--) {
      all.insert(from++);
    }
    cur = from;
  }

  vector<int> ind(n);
  iota(ind.begin(), ind.end(), 0);
  sort(ind.begin(), ind.end(),
       [&](int a, int b) -> bool {
         if (cost[a] != cost[b]) return cost[a] > cost[b];
         if (cnt[a] != cnt[b]) return cnt[a] < cnt[b];
         return a < b;
       });

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int t = ind[i];
    auto p = all.lower_bound(cnt[t]);
    ans += cost[t] * (*p - cnt[t]);
    all.erase(p);
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  auto cmp =
    [&](int a, int b) -> bool {
      if (cost[a] != cost[b]) {
        return cost[a] > cost[b];
      }
      if (cnt[a] != cnt[b]) {
        return cnt[a] < cnt[b];
      }
      return a < b;
    };

  vector<int> st(n);
  iota(st.begin(), st.end(), 0);
  sort(st.begin(), st.end(), cmp);

  set<int> taken;
  map<int, int> from;
  taken.insert(0);
  from[0] = 0;

  auto nx =
  [&](int c) -> int {
    auto p = taken.lower_bound(c);
    int ans = -1;
    if (p == taken.end()) {
      ans = c;
      if (c != *taken.rbegin()+1) {
        taken.insert(c);
        from[c] = c;
      } else {
        taken.erase(c-1);
        taken.insert(c);
        from[c] = from[c-1];
      }
    } else {
      if (from[*p] <= c) {
        ans = *p+1;
        c = *p+1;
        taken.insert(c);
        from[c] = from[*p];

        if (next(p) != taken.end() and from[*next(p)] <= c+1) {
          taken.erase(c);
          from[*next(p)] = from[c];
        }
        taken.erase(*p);
      } else {
        ans = c;
        taken.insert(c);
        from[c] = c;
        if (p != taken.begin() and *prev(p) == c-1) {
          taken.erase(c-1);
          from[c] = from[c-1];
        }

        if (from[*p] <= c+1) {
          taken.erase(c);
          from[*p] = from[c];
        }
      }

    }
    return ans;
  };

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = st[i];
    int f = nx(cnt[cur]);
    ans += cost[cur] * (f - cnt[cur]);
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  auto cmp =
    [&](int a, int b) -> bool {
      if (cost[a] != cost[b]) {
        return cost[a] > cost[b];
      }
      if (cnt[a] != cnt[b]) {
        return cnt[a] < cnt[b];
      }
      return a < b;
    };

  set<int, decltype(cmp)> st(cmp);

  for (int i = 0; i < n; ++i) st.insert(i);
    set<int> avai;
    int lo = *min_element(cnt.begin(), cnt.end());
    int hi = *max_element(cnt.begin(), cnt.end()) + n;
    for (int i = lo; i <= hi; ++i) avai.insert(i);

    ll ans = 0;
    while (st.size()) {
      auto cur = *st.begin(); st.erase(st.begin());
      int l = *avai.lower_bound(cnt[cur]);
      avai.erase(l);
      ans += cost[cur] * (l - cnt[cur]);
    }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define endl '\n'

using ull = unsigned long int;
using ll = long long;

template <class Key, class Val>
using hmap = gp_hash_table<Key, Val>;

template <class Key>
using hset = gp_hash_table<Key, null_type>;

void solve(void);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const int precision = 13;
    cout.precision(precision);
    cout.setf(ios_base::fixed);

    solve();

    return 0;
}

void solve(void) {

  int n; cin >> n;
  vector<int> cnt(n);
  vector<ll> cost(n);

  for (auto& it : cnt) cin >> it;
  for (auto& it : cost) cin >> it;

  auto cmp =
    [&](int a, int b) -> bool {
      if (cost[a] != cost[b]) {
        return cost[a] > cost[b];
      }
      if (cnt[a] != cnt[b]) {
        return cnt[a] < cnt[b];
      }
      return a < b;
    };

  vector<int> st(n);
  iota(st.begin(), st.end(), 0);
  sort(st.begin(), st.end(), cmp);

  set<int> taken;
  map<int, int> from;
  taken.insert(0);
  from[0] = 0;

  auto nx =
  [&](int c) -> int {
    auto p = taken.lower_bound(c);
    int ans = -1;
    if (p == taken.end()) {
      ans = c;
      if (c != *taken.rbegin()+1) {
        taken.insert(c);
        from[c] = c;
      } else {
        taken.erase(c-1);
        taken.insert(c);
        from[c] = from[c-1];
      }
    } else {
      if (from[*p] <= c) {
        ans = *p+1;
        c = *p+1;
        taken.erase(c);
        taken.insert(c+1);
        from[c+1] = from[c];
      } else {
        ans = c;
        taken.insert(c);
        from[c] = c;
        if (p != taken.begin() and *prev(p) == c-1) {
          taken.erase(c-1);
          from[c] = from[c-1];
        }
      }

      if (next(p) != taken.end() and from[*next(p)] <= c+1) {
        taken.erase(c+1);
        from[*next(p)] = from[c];
      }
    }
    return ans;
  };

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = st[i];
    int f = nx(cnt[cur]);
    ans += cost[cur] * (f - cnt[cur]);
  }

  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200010];
int main()
{
	int n;
	cin>>n;
	long long tt=1000000000;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].x;
		if(tt>a[i].x)
		tt=a[i].x;
	}
	for(int i=1;i<=n;i++)
	cin>>a[i].y;
	set<long long> qq;
	map<long long,long long> c,d;
	for(long long i=1;i<=n;i++)
	{
		c[a[i].x]++;
		if(c[a[i].x]==1)
			d[a[i].x]=a[i].y;
		else
			d[a[i].x]=min(d[a[i].x],a[i].y);
			qq.insert(a[i].x);
	}
	long long sum=0;
	for(long long i=*qq.begin();!qq.empty();)
	{
		if(c[i]>1)
		{
			qq.erase(qq.begin());
			sum+=d[i]*(c[i]-1);
			if(c[i+1]!=0)
			{
				qq.insert(i+1);
				c[i+1]=c[i+1]+c[i]-1;
				c[i]=1;
				d[i+1]=min(d[i+1],d[i]);
			}
			else
			{
				qq.insert(i+1);
				c[i+1]=c[i]-1;
				d[i+1]=d[i];
				c[i]=1;
			}
		}
		else
			if(c[i]==1)
			{
				qq.erase(qq.begin());
			}
			i=*qq.begin();
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200020];
int main()
{
	long long n;
	cin>>n;
	map<long long,long long> tp,lt;
	map<long long, multiset<long long> > cnt;
	for(int i=1;i<=n;i++)
		cin>>a[i].x;
	for(int i=1;i<=n;i++)
		cin>>a[i].y;
	set<long long> c;
	for(int i=1;i<=n;i++)
	{
		tp[a[i].x]++;
		if(tp[a[i].x]==1)
			lt[a[i].x]=a[i].y;
		else
			lt[a[i].x]+=a[i].y;
		cnt[a[i].x].insert(a[i].y);
		c.insert(a[i].x);
	}
	int i;
	long long sum=0;
	for(i=*c.begin();!c.empty();)
	{
		if(tp[i]>1)
		{
			multiset<long long>::iterator it;
			it=cnt[i].end();
			it--;
			long long ss=lt[i]-*it;
			sum=sum+ss;
			cnt[i].erase(it);
			cnt[i+1].insert(cnt[i].begin(),cnt[i].end());
			tp[i+1]=tp[i]+tp[i+1]-1;
			lt[i+1]+=ss;
			c.erase(c.begin());
			c.insert(i+1);
		}
		else
		{
			c.erase(c.begin());
		}
		i=*c.begin();
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200010];
int main()
{
	int n;
	cin>>n;
	long long tt=1000000000;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].x;
		if(tt>a[i].x)
		tt=a[i].x;
	}
	for(int i=1;i<=n;i++)
	cin>>a[i].y;
	map<long long,long long> c,d;
	for(long long i=1;i<=n;i++)
	{
		c[a[i].x]++;
		if(c[a[i].x]==1)
			d[a[i].x]=a[i].y;
		else
			d[a[i].x]=min(d[a[i].x],a[i].y);
	}
	long long sum=0;
	long long step=n;
	for(long long i=tt;i<=2000000000;i++)
	{
		if(step==0) break;
		if(c[i]>1)
		{
			step--;
			sum+=d[i]*(c[i]-1);
			if(c[i+1]!=0)
			{
				c[i+1]=c[i+1]+c[i]-1;
				c[i]=1;
				d[i+1]=min(d[i+1],d[i]);
			}
			else
			{
				c[i+1]=c[i]-1;
				d[i+1]=d[i];
				c[i]=1;
			}
		}
		else
			if(c[i]==1)
			step--;
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200020];
int main()
{
	long long n;
	cin>>n;
	map<long long,long long> tp,lt;
	map<long long, multiset<long long> > cnt;
	for(int i=1;i<=n;i++)
		cin>>a[i].x;
	for(int i=1;i<=n;i++)
		cin>>a[i].y;
	set<long long> c;
	for(int i=1;i<=n;i++)
	{
		tp[a[i].x]++;
		if(tp[a[i].x]==1)
			lt[a[i].x]=a[i].y;
		else
			lt[a[i].x]+=a[i].y;
		cnt[a[i].x].insert(a[i].y);
		c.insert(a[i].x);
	}
	int i;
	long long sum=0;
	multiset<long long>::iterator it;
	multiset<long long> kk;
	for(i=*c.begin();!c.empty();)
	{
		if(tp[i]>1)
		{
			if(!cnt[i].empty())
			kk.insert(cnt[i].begin(),cnt[i].end());
			it=kk.end();
			it--;
			long long ss=lt[i]-*it;
			sum=sum+ss;
			kk.erase(it);
			tp[i+1]=tp[i]+tp[i+1]-1;
			lt[i+1]+=ss;
			c.erase(c.begin());
			c.insert(i+1);
		}
		else
		{
			c.erase(c.begin());
		}
		i=*c.begin();
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200020];
int main()
{
	long long n;
	cin>>n;
	map<long long,long long> tp,lt;
	map<long long, multiset<long long> > cnt;
	for(int i=1;i<=n;i++)
		cin>>a[i].x;
	for(int i=1;i<=n;i++)
		cin>>a[i].y;
	set<long long> c;
	for(int i=1;i<=n;i++)
	{
		tp[a[i].x]++;
		if(tp[a[i].x]==1)
			lt[a[i].x]=a[i].y;
		else
			lt[a[i].x]+=a[i].y;
		cnt[a[i].x].insert(a[i].y);
		c.insert(a[i].x);
	}
	int i;
	long long sum=0;multiset<long long>::iterator it;
	for(i=*c.begin();!c.empty();)
	{
		if(tp[i]>1)
		{
			it=cnt[i].end();
			it--;
			long long ss=lt[i]-*it;
			sum=sum+ss;
			cnt[i].erase(it);
			cnt[i+1].insert(cnt[i].begin(),cnt[i].end());
			tp[i+1]=tp[i]+tp[i+1]-1;
			lt[i+1]+=ss;
			c.erase(c.begin());
			c.insert(i+1);
		}
		else
		{
			c.erase(c.begin());
		}
		i=*c.begin();
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200020];
map<long long,long long> lt;
map<long long, multiset<long long> > cnt;
int main()
{
	long long n;
	cin>>n;

	for(int i=1;i<=n;i++)
		cin>>a[i].x;
	for(int i=1;i<=n;i++)
		cin>>a[i].y;
	set<long long> c;
	for(int i=1;i<=n;i++)
	{
		lt[a[i].x]+=a[i].y;
		cnt[a[i].x].insert(a[i].y);
		c.insert(a[i].x);
	}
	int i;
	long long sum=0;
	multiset<long long>::iterator it;
	multiset<long long> kk;
	for(i=*c.begin();!c.empty();)
	{
			if(!cnt[i].empty())
			kk.insert(cnt[i].begin(),cnt[i].end());
			if(kk.empty())
			{
				c.erase(c.begin());
				i=*c.begin();
				continue;
			}
			it=kk.end();
			it--;
			long long ss=lt[i]-*it;
			sum=sum+ss;
			kk.erase(it);
			lt[i+1]+=ss;
			c.erase(c.begin());
			c.insert(i+1);
			i=*c.begin();
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}a[200020];
int main()
{
	long long n;
	cin>>n;
	map<long long,long long> tp,lt;
	map<long long, multiset<long long> > cnt;
	for(int i=1;i<=n;i++)
		cin>>a[i].x;
	for(int i=1;i<=n;i++)
		cin>>a[i].y;
	set<long long> c;
	for(int i=1;i<=n;i++)
	{
		tp[a[i].x]++;
		if(tp[a[i].x]==1)
			lt[a[i].x]=a[i].y;
		else
			lt[a[i].x]+=a[i].y;
		cnt[a[i].x].insert(a[i].y);
		c.insert(a[i].x);
	}
	int i;
	long long sum=0;
	for(i=*c.begin();!c.empty();)
	{
		if(tp[i]>1)
		{
			long long ss=lt[i]-*cnt[i].end();
			sum=sum+ss;
			cnt[i].erase(*cnt[i].end());
			cnt[i+1].insert(cnt[i].begin(),cnt[i].end());
			tp[i+1]=tp[i]+tp[i+1]-1;
			lt[i+1]+=ss;
			c.erase(c.begin());
			c.insert(i+1);
		}
		else
		{
			c.erase(c.begin());
		}
		i=*c.begin();
	}
	cout<<sum<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<int,int>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const int Inf = 1e9;

struct Entry{
  int x;
  int cost;
  Entry(int x,int cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  int l;
  int r;
  int cost;
  int sz;
  Interval(int l,int r):l(l),r(r){}
  Interval(int l,int cost,int sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

int t;

int main(){
  int n; scanf("%d",&n);
  set<int> Tm;
  vector<Entry> A;
  map<int,int> Map;
  vector<int> B;
  map<int,vector<int> > costs;
  for(int i = 0 ; i < n ; ++i){
    int x; scanf("%d",&x);
    Tm.insert(x);
    B.push_back(x);
  }
  for(int i = 0 ; i < n ; ++i){
    int x; scanf("%d",&x);
    costs[B[i]].push_back(x);
    Map[B[i]] += x;
  }
  for(auto el : Tm){
    A.push_back(Entry(el,Map[el]));
  }
  set<Interval> I;
  I.insert(Interval(A.back().x + 1,Inf));
  set<Interval> Blk;
  for(int i = A.size() - 1 ; i > 0 ; ){
    if(costs[A[i].x].size() > 1){
      i--; continue;
    }
    int j = i - 1;
    int collect = A[i].cost;
    while(j >= 0 && A[j].x == A[j + 1].x - 1 && costs[A[j].x].size() <= 1){
      j--;
      collect += A[j].cost;
    }
    Blk.insert(Interval(A[j + 1].x + 1,collect,j - i));
    i = j;
  }

  for(int i = A.size() - 1 ; i > 0 ; --i){
    if(A[i].x - A[i - 1].x > 1)
      I.insert(Interval(A[i - 1].x + 1,A[i].x - 1));
  }

  ll Ans = 0;
  for(auto El : Tm){
    if(costs[El].size() < 2) continue;
    sort(all(costs[El]));
    int sz = costs[El].size();
    for(int i = 0 ; i < sz - 1; ++i){
      auto em = I.upper_bound({El,0});
      if(em == I.end()) em--;
      auto bl = Blk.upper_bound({El,0});
      if(bl == Blk.end()) bl--;
      Interval Empty =  *em;
      Interval Bulk = *bl;

      int jump = costs[El][i] * (Empty.l - El);
      int shft = Bulk.cost + costs[El][i] ;

      if(jump < shft){
        Ans += jump;
        I.erase(Empty);
        Empty.l++;
        if(Empty.l <= Empty.r){
          I.insert(Empty);
        }
      }else{
        Ans += shft;
        Blk.erase(Bulk);
        Bulk.sz++;
        Bulk.cost += costs[El][i];
        Blk.insert(Bulk);
      }
    }
  }
  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

map<int,int> Full;
ll t;

int main(){
  ll n; scanf("%lld",&n);
  set<ll> Tm;
  map<ll,vector<ll> > costs;
  vector<ll> A;
  vector<ll> B;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    Tm.insert(x);
    B.push_back(x);
    Full[x] = 1;
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    costs[B[i]].push_back(x);
    Full[B[i]] = x;
  }

  for(auto el : Tm){
    if(costs[el].size() < 2) continue;
    A.push_back(el);
  }
  ll Ans = 0;
  for(int i = 0 ; i < A.size() ; i++){
    if(costs[A[i]].size() > 1){
      sort(all(costs[A[i]]));
      int k = A[i] + 1;
      for(int j = 0 ; j < costs[A[i]].size() - 1; ++j){
        ll cost1 = costs[A[i]][j];
        while(Full[k] > 0){
          cost1 += Full[k];
          k++;
        }
        ll sum = min(cost1,(k - A[i]) * costs[A[i]][j]);
        Full[k] = sum;
        Ans += sum;
      }
    }
  }

  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const int Inf = 1e9;

struct T {
  ll x;
  ll t;
  T(ll x,ll t):x(x),t(t){}
  bool operator<(const T& Ti) const {
    if(t == Ti.t) return x < Ti.x;
    return t > Ti.t;
  }
};

set<ll> Timer;
vector<T> A;
map<ll,ll> Is;
int n;

int main(){
  scanf("%d",&n);
  for(int i = 0 ; i < n ;++i){
    ll x; scanf("%lld",&x);
    A.push_back(T(x,0));
    Timer.insert(x);
  }
  for(int i = 0 ; i < n ; ++i){
    ll t; scanf("%lld",&t);
    A[i].t = t;
  }
  sort(all(A));
  ll Ans = 0;
  for(int i = 0 ; i < n ; ++i){
    int P = *Timer.lower_bound(A[i].x);
    Ans += (P - A[i].x) * A[i].t;
    Is[P] = 1;
    Timer.erase(P);
    if(Is[P + 1] == 0) Timer.insert(P + 1);
  }
  cout << Ans << endl;
}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

map<int,int> Full;
ll t;

int main(){
  ll n; scanf("%lld",&n);
  set<ll> Tm;
  map<ll,vector<ll> > costs;
  vector<ll> A;
  vector<ll> B;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    Tm.insert(x);
    B.push_back(x);
    Full[x] = 1;
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    costs[B[i]].push_back(x);
    Full[B[i]] = x;
  }

  for(auto el : Tm){
    if(costs[el].size() < 2) continue;
    A.push_back(el);
  }
  ll Ans = 0;
  for(int i = 0 ; i < A.size() ; i++){
    if(costs[A[i]].size() > 1){
      sort(all(costs[A[i]]));
      int k = A[i] + 1;
      for(int j = 0 ; j < costs[A[i]].size() - 1; ++j){
        ll cost1 = costs[A[i]][j];
        while(Full[k] > 0){
          cost1 = Full[k];
          k++;
        }
        ll sum = min(cost1 + costs[A[i]][j],(k - A[i]) * costs[A[i]][j]);
        Full[k] = sum;
        Ans += sum;
      }
    }
  }

  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

map<int,int> Full;
ll t;

int main(){
  ll n; scanf("%lld",&n);
  set<ll> Tm;
  map<ll,vector<ll> > costs;
  vector<ll> A;
  vector<ll> B;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    Tm.insert(x);
    B.push_back(x);
    Full[x] = 1;
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    costs[B[i]].push_back(x);
    Full[B[i]] = x;
  }
  for(auto &el : Full){
    el.nd += Full[el.st - 1];
  }

  for(auto el : Tm){
    if(costs[el].size() < 2) continue;
    A.push_back(el);
  }
  ll Ans = 0;
  for(int i = 0 ; i < A.size() ; i++){
    if(costs[A[i]].size() > 1){
      sort(all(costs[A[i]]));
      int k = A[i] + 1;
      for(int j = 0 ; j < costs[A[i]].size() - 1; ++j){
        ll cost1 = costs[A[i]][j];
        while(Full[k] > 0){
          cost1 = Full[k];
          k++;
        }
        ll sum = min(cost1 + costs[A[i]][j],(k - A[i]) * costs[A[i]][j]);
        Full[k] = sum;
        Ans += sum;
      }
    }
  }

  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

ll t;

int main(){
  ll n; scanf("%lld",&n);
  set<ll> Tm;
  vector<Entry> A;
  map<ll,ll> Map;
  vector<ll> B;
  map<ll,vector<ll> > costs;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    Tm.insert(x);
    B.push_back(x);
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    costs[B[i]].push_back(x);
    Map[B[i]] += x;
  }
  for(auto el : Tm){
    A.push_back(Entry(el,Map[el]));
  }
  set<Interval> I;
  I.insert(Interval(A.back().x + 1,Inf));
  set<Interval> Blk;
  for(ll i = A.size() - 1 ; i > 0 ; ){
    if(costs[A[i].x].size() > 1){
      i--; continue;
    }
    ll j = i - 1;
    ll collect = A[i].cost;
    while(j >= 0 && A[j].x == A[j + 1].x - 1 && costs[A[j].x].size() <= 1){
      j--;
      collect += A[j].cost;
    }
    Blk.insert(Interval(A[j + 1].x + 1,collect,j - i));
    i = j;
  }

  for(ll i = A.size() - 1 ; i > 0 ; --i){
    if(A[i].x - A[i - 1].x > 1)
      I.insert(Interval(A[i - 1].x + 1,A[i].x - 1));
  }

  ll Ans = 0;
  for(auto El : Tm){
    if(costs[El].size() < 2) continue;
    sort(all(costs[El]));
    ll sz = costs[El].size();
    for(ll i = 0 ; i < sz - 1; ++i){
      auto em = I.upper_bound({El,0});
      if(em == I.end()) em--;
      auto bl = Blk.upper_bound({El,0});
      if(bl == Blk.end()) bl--;
      Interval Empty =  *em;
      Interval Bulk = *bl;

      ll jump = costs[El][i] * (Empty.l - El);
      ll shft = Bulk.cost + costs[El][i] ;

      if(jump < shft){
        Ans += jump;
        I.erase(Empty);
        Empty.l++;
        if(Empty.l <= Empty.r){
          I.insert(Empty);
        }
      }else{
        Ans += shft;
        Blk.erase(Bulk);
        Bulk.sz++;
        Bulk.cost += costs[El][i];
        Blk.insert(Bulk);
      }
    }
  }
  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

ll t;

int main(){
  ll n; scanf("%lld",&n);
  set<ll> Tm;
  vector<Entry> A;
  map<ll,ll> Map;
  vector<ll> B;
  map<ll,vector<ll> > costs;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    Tm.insert(x);
    B.push_back(x);
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%lld",&x);
    costs[B[i]].push_back(x);
    Map[B[i]] += x;
  }
  for(auto el : Tm){
    A.push_back(Entry(el,Map[el]));
  }
  set<Interval> I;
  I.insert(Interval(A.back().x + 1,Inf));
  set<Interval> Blk;
  for(ll i = A.size() - 1 ; i > 0 ; ){
    if(costs[A[i].x].size() > 1){
      i--; continue;
    }
    ll j = i - 1;
    ll collect = A[i].cost;
    while(j >= 0 && A[j].x == A[j + 1].x - 1 && costs[A[j].x].size() <= 1){
      j--;
      collect += A[j].cost;
    }
    Blk.insert(Interval(A[j + 1].x + 1,collect,j - i));
    i = j;
  }

  for(ll i = A.size() - 1 ; i > 0 ; --i){
    if(A[i].x - A[i - 1].x > 1)
      I.insert(Interval(A[i - 1].x + 1,A[i].x - 1));
  }
  ll Ans = 0;
  for(auto El : Tm){
    if(costs[El].size() < 2) continue;
    sort(all(costs[El]));
    ll sz = costs[El].size();
    for(ll i = 0 ; i < sz - 1; ++i){
      auto em = I.upper_bound({El,0});
      if(em == I.end()) em--;
      Interval Empty =  *em;
      if(Blk.empty()){
        ll jump = costs[El][i] * (Empty.l - El);
        Ans += jump;
        I.erase(Empty);
        Empty.l++;
        if(Empty.l <= Empty.r){
          I.insert(Empty);
        }
        continue;
      }
      auto bl = Blk.upper_bound({El,0});
      if(bl == Blk.end()) bl--;
      Interval Bulk = *bl;

      ll jump = costs[El][i] * (Empty.l - El);
      ll shft = Bulk.cost + costs[El][i] ;

      if(jump < shft){
        Ans += jump;
        I.erase(Empty);
        Empty.l++;
        if(Empty.l <= Empty.r){
          I.insert(Empty);
        }
      }else{
        Ans += shft;
        Blk.erase(Bulk);
        Bulk.sz++;
        Bulk.cost += costs[El][i];
        auto It = Blk.upper_bound({Bulk.l,0});
        if(It == Blk.end()){
          Blk.insert(Bulk);

        }else{
          Interval kk = *It;
          if(Bulk.sz + Bulk.l >= kk.l){
            Blk.erase(It);
            Bulk.sz += kk.sz;
            Bulk.cost += kk.cost;
            Blk.insert(Bulk);
          }

        }
      }
    }
  }
  cout << Ans << endl;

}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<ll,ll>
#define PL pair<ll,ll>
#define st first
#define nd second
#define all(x) x.begin(),x.end()

using namespace std;

const string Bulk = "Bulk";
const string Prob = "Prob";
const ll Inf = 1e9;

struct Entry{
  ll x;
  ll cost;
  Entry(ll x,ll cost):x(x),cost(cost){}
  bool operator<(const Entry& E) const {
    return x < E.x;
  }
};

struct Interval {
  ll l;
  ll r;
  ll cost;
  ll sz;
  Interval(ll l,ll r):l(l),r(r){}
  Interval(ll l,ll cost,ll sz):l(l),cost(cost),sz(sz){}
  bool operator<(const Interval& I) const {
    if(l == I.l) return r - l > I.r - I.l;
    return l < I.l;
  }
};

ll t;

ll main(){
  ll n; scanf("%d",&n);
  set<ll> Tm;
  vector<Entry> A;
  map<ll,ll> Map;
  vector<ll> B;
  map<ll,vector<ll> > costs;
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%d",&x);
    Tm.insert(x);
    B.push_back(x);
  }
  for(ll i = 0 ; i < n ; ++i){
    ll x; scanf("%d",&x);
    costs[B[i]].push_back(x);
    Map[B[i]] += x;
  }
  for(auto el : Tm){
    A.push_back(Entry(el,Map[el]));
  }
  set<Interval> I;
  I.insert(Interval(A.back().x + 1,Inf));
  set<Interval> Blk;
  for(ll i = A.size() - 1 ; i > 0 ; ){
    if(costs[A[i].x].size() > 1){
      i--; continue;
    }
    ll j = i - 1;
    ll collect = A[i].cost;
    while(j >= 0 && A[j].x == A[j + 1].x - 1 && costs[A[j].x].size() <= 1){
      j--;
      collect += A[j].cost;
    }
    Blk.insert(Interval(A[j + 1].x + 1,collect,j - i));
    i = j;
  }

  for(ll i = A.size() - 1 ; i > 0 ; --i){
    if(A[i].x - A[i - 1].x > 1)
      I.insert(Interval(A[i - 1].x + 1,A[i].x - 1));
  }

  ll Ans = 0;
  for(auto El : Tm){
    if(costs[El].size() < 2) continue;
    sort(all(costs[El]));
    ll sz = costs[El].size();
    for(ll i = 0 ; i < sz - 1; ++i){
      auto em = I.upper_bound({El,0});
      if(em == I.end()) em--;
      auto bl = Blk.upper_bound({El,0});
      if(bl == Blk.end()) bl--;
      Interval Empty =  *em;
      Interval Bulk = *bl;

      ll jump = costs[El][i] * (Empty.l - El);
      ll shft = Bulk.cost + costs[El][i] ;

      if(jump < shft){
        Ans += jump;
        I.erase(Empty);
        Empty.l++;
        if(Empty.l <= Empty.r){
          I.insert(Empty);
        }
      }else{
        Ans += shft;
        Blk.erase(Bulk);
        Bulk.sz++;
        Bulk.cost += costs[El][i];
        Blk.insert(Bulk);
      }
    }
  }
  cout << Ans << endl;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
   // cout << "-------------------" << "\n";

    int n;
    cin>>n;

    set<pii>s;
    multimap<int,int>ss;

    vpii arr(n);

    for(int i=0;i<n;i++)
     cin>>arr[i].ff;

    for(int i=0;i<n;i++)
    cin>>arr[i].ss;

    for(int i=0;i<n;i++)
    {
    s.insert(arr[i]);
    ss.insert({arr[i].ff,i});
    }

    int ans=0;

    int cnt=1;
    while(!s.empty())
    {
        auto x = s.begin();
        int ele = x->ff,ti = x->ss;
       // deb(ele,ti,ss.count(ele));

        if(ss.count(ele)==1)
        {

            ss.erase(ss.find(ele));
            s.erase(x);
        }
        else
        {
            s.erase(x);
            s.insert({ele+1,ti});
            ans+=ti;
            ss.erase(ss.find(ele));
            ss.insert({ele+1,cnt});
            cnt++;
        }

    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
    //cout << "-------------------" << "\n";

    int n;
    cin>>n;

    vpii arr(n);

    for(int i=0;i<n;i++) cin>>arr[i].ff;
    for(int i=0;i<n;i++) cin>>arr[i].ss;

   map<int,set<pii> > m;
   int cnt=0;

   for(int i=0;i<n;i++)
   {
       m[arr[i].ff].insert({arr[i].ss,++cnt});
   }

   int ans=0;

   for(auto &i:m)
   {
       auto &s =  i.ss;

       if(s.size()==1)
       continue;

       vpii a;

       for(auto j=s.begin();j!=s.end();j++)
       a.pb(*j);

       for(int j=0;j<a.size()-1;j++)
       {
           s.erase(a[j]);
           ans+=a[j].ff;
           m[i.ff+1].insert({a[j].ff,++cnt});
       }

   }

   cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
   // cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
   // cout << "-------------------" << "\n";

    int n;
    cin>>n;

   multimap<pii,int>s;
   unordered_map<int,int>ss;

    vpii arr(n);

    for(int i=0;i<n;i++)
     cin>>arr[i].ff;

    for(int i=0;i<n;i++)
    cin>>arr[i].ss;

    for(int i=0;i<n;i++)
    {
    s.insert({arr[i],i});
    ss[arr[i].ff]++;
    }

    int ans=0;

    int cnt=1;

    while(!s.empty())
    {
        auto x = s.begin();
        int ele = x->ff.ff,ti = x->ff.ss;

        if(ss[ele]==1)
        {

            ss.erase(ele);
            s.erase(s.find({ele,ti}));
        }
        else
        {
            s.erase(s.find({ele,ti}));
            s.insert({{ele+1,ti},cnt});
            ans+=ti;
            ss[ele]--;
            ss[ele+1]++;;
            cnt++;
        }

    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

bool cmp(pii p1,pii p2)
{
    return p1.ss > p2.ss;
}

void solve()
{
    //cout << "-------------------" << "\n";

    int n;
    cin>>n;

   vpii arr(n);

    for(int i=0;i<n;i++) cin>>arr[i].ff;
    for(int i=0;i<n;i++) cin>>arr[i].ss;

    int cnt=1;

    sort(all(arr));

    set<int>s;

    for(int i=0;i<n;i++)
    {
        if(arr[i].ff>cnt)
        {
            s.insert(arr[i].ff);
            cnt=arr[i].ff;
            cnt++;
        }
        else
        {
            s.insert(cnt);
            cnt++;
        }
    }

    int ans=0;

    sort(all(arr),cmp);

    for(int i=0;i<n;i++)
    {
        auto it = s.lower_bound(arr[i].ff);
        ans+=(*it-arr[i].ff)*arr[i].ss;
        s.erase(it);
    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
   // cout << "-------------------" << "\n";

    int n;
    cin>>n;

    multimap<pii,int>s;
    multimap<int,int>ss;

    vpii arr(n);

    for(int i=0;i<n;i++)
     cin>>arr[i].ff;

    for(int i=0;i<n;i++)
    cin>>arr[i].ss;

    for(int i=0;i<n;i++)
    {
    s.insert({arr[i],i});
    ss.insert({arr[i].ff,i});
    }

    int ans=0;

    int cnt=1;
    while(!s.empty())
    {
        auto x = s.begin();
        int ele = x->ff.ff,ti = x->ff.ss;
       // deb(ele,ti,ss.count(ele));

        if(ss.count(ele)==1)
        {

            ss.erase(ss.find(ele));
            s.erase(s.find({ele,ti}));
        }
        else
        {
            s.erase(s.find({ele,ti}));
            s.insert({{ele+1,ti},cnt});
            ans+=ti;
            ss.erase(ss.find(ele));
            ss.insert({ele+1,cnt});
            cnt++;
        }

    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
   // cout << "-------------------" << "\n";

    int n;
    cin>>n;

   multimap<pii,int>s;
   unordered_multimap<int,int>ss;

    vpii arr(n);

    for(int i=0;i<n;i++)
     cin>>arr[i].ff;

    for(int i=0;i<n;i++)
    cin>>arr[i].ss;

    for(int i=0;i<n;i++)
    {
    s.insert({arr[i],i});
    ss.insert({arr[i].ff,i});
    }

    int ans=0;

    int cnt=1;

    while(!s.empty())
    {
        auto x = s.begin();
        int ele = x->ff.ff,ti = x->ff.ss;
       // deb(ele,ti,ss.count(ele));

        if(ss.count(ele)==1)
        {

            ss.erase(ss.find(ele));
            s.erase(s.find({ele,ti}));
        }
        else
        {
            s.erase(s.find({ele,ti}));
            s.insert({{ele+1,ti},cnt});
            ans+=ti;
            ss.erase(ss.find(ele));
            ss.insert({ele+1,cnt});
            cnt++;
        }

    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
    //cout << "-------------------" << "\n";

    int n;
    cin>>n;

    vpii arr(n);

    for(int i=0;i<n;i++) cin>>arr[i].ff;
    for(int i=0;i<n;i++) cin>>arr[i].ss;

   unordered_map<int,set<pii> > m;
   int cnt=0;

   for(int i=0;i<n;i++)
   {
       m[arr[i].ff].insert({arr[i].ss,++cnt});
   }

   int ans=0;

   for(auto &i:m)
   {
       auto &s =  i.ss;

       if(s.size()==1)
       continue;

       vpii a;

       for(auto j=s.begin();j!=s.end();j++)
       a.pb(*j);

       for(int j=0;j<a.size()-1;j++)
       {
           s.erase(a[j]);
           ans+=a[j].ff;
           m[i.ff+1].insert({a[j].ff,++cnt});
       }

   }

   cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
   // cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"

const int N = 2e5 + 5;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

void solve()
{
   // cout << "-------------------" << "\n";

    int n;
    cin>>n;

   multimap<pii,int>s;
   unordered_multimap<int,int>ss;

    vpii arr(n);

    for(int i=0;i<n;i++)
     cin>>arr[i].ff;

    for(int i=0;i<n;i++)
    cin>>arr[i].ss;

    for(int i=0;i<n;i++)
    {
    s.insert({arr[i],i});
    ss.insert({arr[i].ff,i});
    }

    int ans=0;

    int cnt=1;

    while(!s.empty())
    {
        auto x = s.begin();
        int ele = x->ff.ff,ti = x->ff.ss;

        if(ss.count(ele)==1)
        {

            ss.erase(ss.find(ele));
            s.erase(s.find({ele,ti}));
        }
        else
        {
            s.erase(s.find({ele,ti}));
            s.insert({{ele+1,ti},cnt});
            ans+=ti;
            ss.erase(ss.find(ele));
            ss.insert({ele+1,cnt});
            cnt++;
        }

    }

    cout<<ans<<"\n";

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int tree[mx * 4];
int arr[mx];

void update( int node, int l, int r, int b, int x )
{
	if( l == r ) {
		arr[l] = x;
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	if( mid >= l ) update( 2*node, l, mid, b, x );
	else update( 2*node+1, mid+1, r, b, x );
	if( arr[tree[2*node]] > arr[tree[2*node+1]] ) tree[node] = tree[2*node];
	else tree[node] = tree[2*node+1];
}

int main()
{
	optimize();
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	nxt = 1;
	int lim = n+2;
	ll ans = 0, cur = num[0]-1, sum = 0;
	int cnt = 0;
	for( int i = 0; i < num.size(); ++i ) {
		while( cur < num[i] ) {
			if( cnt <= 1 ) break;
			if( arr[tree[1]] == 0 ) break;
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
		if( cur < num[i] ) {
			sum = 0;
			cur = num[i];
			cnt = 0;
			while(arr[tree[1]]) {
				update( 1, 1, lim, tree[1], 0 );
			}
		}
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			++cnt;
			update( 1, 1, lim, nxt++, vec[ul][j] );
			sum += vec[ul][j];
		}
		if(cnt > 1) {
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
	}
	while(1) {
		if( cnt <= 1 ) break;
		if( arr[tree[1]] == 0 ) break;
		sum -= arr[tree[1]];
		ans += sum;
		--cnt;
		update( 1, 1, lim, tree[1], 0 );
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int tree[mx * 4];
int arr[mx];

void init( int node, int l, int r )
{
	if( l == r ) {
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	init( 2*node, l, mid );
	init( 2*node+1, mid+1, r );
	tree[node] = tree[2*node];
}

void update( int node, int l, int r, int b, int x )
{
	if( l == r ) {
		arr[l] = x;
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	if( mid >= l ) update( 2*node, l, mid, b, x );
	else update( 2*node+1, mid+1, r, b, x );
	if( arr[tree[2*node]] > arr[tree[2*node+1]] ) tree[node] = tree[2*node];
	else tree[node] = tree[2*node+1];
}

int main()
{
	optimize();
	tree[1] = 1;
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	nxt = 1;
	int lim = n+2;
	init(1, 1, lim);
	ll ans = 0, cur = num[0]-1, sum = 0;
	int cnt = 0;
	for( int i = 0; i < num.size(); ++i ) {
		while( cur < num[i] ) {
			if( cnt <= 1 ) break;
			if( arr[tree[1]] == 0 ) break;
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
		if( cur < num[i] ) {
			sum = 0;
			cur = num[i];
			cnt = 0;
			while(arr[tree[1]]) {
				update( 1, 1, lim, tree[1], 0 );
			}
		}
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			++cnt;
			update( 1, 1, lim, nxt++, vec[ul][j] );
			sum += vec[ul][j];
		}
		if(cnt > 1) {
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
	}
	while(1) {
		if( cnt <= 1 ) break;
		if( arr[tree[1]] == 0 ) break;
		sum -= arr[tree[1]];
		ans += sum;
		--cnt;
		update( 1, 1, lim, tree[1], 0 );
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int main()
{
	optimize();
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	ll ans = 0, cur = num[0]-1, sum = 0;
	for( int i = 0; i < num.size(); ++i ) {
		while( cur < num[i] && !st2.empty() ) {
			++cur;
			sum -= *st2.rbegin();
			ans += sum;
			st2.erase( st2.find(*st2.rbegin()) );
		}
		cur = num[i];
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			st2.insert( vec[ul][j] );
			sum += vec[ul][j];
		}
		++cur;
		sum -= *st2.rbegin();
		ans += sum;
		st2.erase( st2.find(*st2.rbegin()) );
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int tree[mx * 4];
int arr[mx];

void update( int node, int l, int r, int b, int x )
{
	if( l == r ) {
		arr[l] = x;
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	if( mid >= l ) update( 2*node, l, mid, b, x );
	else update( 2*node+1, mid+1, r, b, x );
	if( arr[tree[2*node]] > arr[tree[2*node+1]] ) tree[node] = tree[2*node];
	else tree[node] = tree[2*node+1];
}

int main()
{
	optimize();
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	nxt = 1;
	int lim = n+2;
	ll ans = 0, cur = num[0]-1, sum = 0;
	for( int i = 0; i < num.size(); ++i ) {
		while( cur < num[i] ) {
			if( arr[tree[1]] == 0 ) break;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
		cur = num[i];
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			update( 1, 1, lim, nxt++, vec[ul][j] );
			sum += vec[ul][j];
		}
		++cur;
		sum -= arr[tree[1]];
		ans += sum;
		update( 1, 1, lim, tree[1], 0 );
	}
	while(1) {
		if( arr[tree[1]] == 0 ) break;
		sum -= arr[tree[1]];
		ans += sum;
		update( 1, 1, lim, tree[1], 0 );
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int tree[mx * 4];
int arr[mx];

void init( int node, int l, int r )
{
	if( l == r ) {
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	init( 2*node, l, mid );
	init( 2*node+1, mid+1, r );
	tree[node] = tree[2*node];
}

void update( int node, int l, int r, int b, int x )
{
	if( l == r ) {
		arr[l] = x;
		tree[node] = l;
		return;
	}
	int mid = (l + r)/2;
	if( mid >= b ) update( 2*node, l, mid, b, x );
	else update( 2*node+1, mid+1, r, b, x );
	if( arr[tree[2*node]] > arr[tree[2*node+1]] ) tree[node] = tree[2*node];
	else tree[node] = tree[2*node+1];
}

int main()
{
	optimize();
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	nxt = 1;
	int lim = n+2;
	init(1, 1, lim);
	ll ans = 0, cur = num[0]-1, sum = 0;
	int cnt = 0;
	for( int i = 0; i < num.size(); ++i ) {
		while( cur < num[i] ) {
			if( cnt <= 1 ) break;
			if( arr[tree[1]] == 0 ) break;
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
		if( cur < num[i] ) {
			sum = 0;
			cur = num[i];
			cnt = 0;
			while(arr[tree[1]]) {
				update( 1, 1, lim, tree[1], 0 );
			}
		}
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			//cerr << "OKKK" << endl;
			++cnt;
			update( 1, 1, lim, nxt++, vec[ul][j] );
			sum += vec[ul][j];
		}
		if(cnt > 1) {
			--cnt;
			++cur;
			sum -= arr[tree[1]];
			ans += sum;
			update( 1, 1, lim, tree[1], 0 );
		}
	}
	//cerr << cnt << endl;
	while(1) {
		if( cnt <= 1 ) break;
		if( arr[tree[1]] == 0 ) break;
		sum -= arr[tree[1]];
		ans += sum;
		--cnt;
		update( 1, 1, lim, tree[1], 0 );
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define PB push_back
//#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

const int mx = 2e5+5;
ll a[mx], t[mx];
int n;
set<ll> st;
map<ll,int> mp;
vector<ll> vec[mx];
int nxt = 1;
multiset<int> st2;
vector<ll> num;

int main()
{
	optimize();
	cin >> n;
	for( int i = 1; i <= n; ++i ) {
		cin >> a[i];
		st.insert( a[i] );
	}
	for( auto x : st ) {
		mp[x] = nxt++;
		num.PB(x);
	}
	for( int i = 1; i <= n; ++i ) cin >> t[i];
	for( int i = 1; i <= n; ++i ) {
		vec[mp[a[i]]].PB(t[i]);
	}
	for( int i = 1; i < nxt; ++i ) {
		sort( vec[i].begin(), vec[i].end() );
	}
	ll ans = 0, cur = num[0]-1, sum = 0;
	for( int i = 0; i < num.size(); ++i ) {
		if( cur+1 == num[i] ) {
			st2.clear();
			sum = 0;
		}
		int ul = mp[num[i]];
		for( int j = 0; j < vec[ul].size(); ++j ) {
			st2.insert( vec[ul][j] );
			sum += vec[ul][j];
		}
		sum -= *st2.rbegin();
		ans += sum;
		st2.erase( st2.find(*st2.rbegin()) );
	}
	cout << ans << endl;
}


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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


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

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

using namespace std;

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

const ll mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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