#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

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

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

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

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

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

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

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

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 3e5 + 5;

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

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

}

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

void Solve(){
    cin >> n;
    map<ll, ll> mp;
    map<ll, ll> mp2;
    for(int i = 0; i < n; i++){
        cin >> a[i].st;
    }
    for(int i = 0; i < n; i++){
        cin >> a[i].nd;
    }
    ll mx = 0;
    ll res = 0;
        sort(a, a + n);
    if(a[0].st == a[n - 1].st){
        ll sum = 0;
        for(int i = 0; i < n; i++){
            sum += (n - i - 1) * a[i].nd;
        }
        cout << sum << "\n";
        return;
    }

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

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


#include <bits/stdc++.h>

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

using namespace std;

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

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

const int N = 2e5 + 5;

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

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

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


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
map<int,int> inv;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

set<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].insert((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	int now = -1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		while(ff[i].size()>1)
		{
			auto now = *prev(ff[i].end());ff[i].erase(prev(ff[i].end()));
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			//cerr<<kind<<" "<<cost<<endl;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;

			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].insert((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
		//cerr<<"---------\n";
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
map<int,int> inv;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

priority_queue<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].push((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	int now = -1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		while(!ff[i].empty())
		{
			auto now = ff[i].top();ff[i].pop();
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].push((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

set<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)) idx[x] = num++;
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]);
	_rep(i,1,n) sci(t[i]);
	_rep(i,1,n)
	{
		int id = ID(a[i]);
		ff[id].insert((matter){a[i],t[i],i});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		for(auto it = ff[i].rbegin();it!=ff[i].rend();it++)
		{
			auto now = *it;
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].insert((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
int inv[maxn];
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

LL sumff[maxn];
LL minff[maxn];
LL maxff[maxn];
int szff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;
vector<int> ffcost[maxn];

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		szff[id]++;
		if(szff[id]==1)
			maxff[id]=minff[id] = t[can[i-1].sc],sumff[id]+=t[can[i-1].sc];
		else
		{
			maxff[id] = max((LL)t[can[i-1].sc],maxff[id]);
			minff[id] = min((LL)t[can[i-1].sc],minff[id]);
		}
		ffcost[id].pb(t[can[i-1].sc]);

	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(szff[i]>=2)q.push(i),inq[i] = 1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = 0;
		sort(all(ffcost[i]));
		auto last = ffcost[i].back();
		while(szff[i]>=1)
		{
			auto now = ffcost[i].back();
			szff[i]--;
			ffcost[i].pop_back();
			int cost,kind,I;
			cost = now,kind = inv[i];
			kind+=x;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)x*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				szff[id]++;
				ffcost[id].pb(cost);
				if(szff[id]>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
			x++;
		}
		ffcost[i].pb(last);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

set<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)) idx[x] = num++;
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;
int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].insert((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		auto last = *ff[i].begin();
		for(auto it = ff[i].rbegin();it!=ff[i].rend();it++)
		{
			auto now = *it;
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].insert((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
		ff[i].clear();
		ff[i].insert(last);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
map<int,int> inv;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

priority_queue<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].push((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	int now = -1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		while(ff[i].size()>=1)
		{
			auto now = ff[i].top();ff[i].pop();
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].push((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
map<int,int> inv;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

priority_queue<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].push((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	int now = -1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		while(ff[i].size()>1)
		{
			auto now = ff[i].top();ff[i].pop();
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].push((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<cassert>
#include<cmath>
#include<unordered_map>
#include<algorithm>
#include<queue>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

const int maxn = 60+10;
const int INF = 1e9+7;

LL ans = 0;
pii info[maxn];
int n;

// OK..Down to 1611 because of my little but deadly mistake
// But the problem is a very beautiful implement of Heap
// I just want to say ji bu ru ren,gan bai xia feng!!!
// We can see that we will only left the maxcost undone for
// some "matter" which have the same a's value
// then,the problem in fact can be solved,we in fact want to
// maintian the sum of some elements's t's value,whose a's value is the same,
// and the max t's value of these element,in one process we delet the element
// which has the max t's value,and add the remaining element's sum of t's
// value,and also then make all the remaining element's a's value add one
// and continuing maintain the sum of the element which has the same a's
// unitl we have iterated all the element and the "maintaining block" is empty
// So,here comes heap,it can delete the max/min element and get the max/min
// element in O(log(n)),then the complexity of time is O(nlogn)!
// Beautiful! When can I be purple....

// by the way the beautiful is created by a red juju
// %%%%%

int main(void)
{
	sci(n);
	_for(i,0,n)sci(info[i].fr);
	_for(i,0,n)sci(info[i].sc);
	priority_queue<int,vector<int>,less<int> > q;
	int x,i;
	sort(info,info+n);
	i = 0;
	LL ans = 0;
	LL sum = 0;
	while(!q.empty()||i<n)
	{
		if(q.empty())
			x = info[i].fr;
		while(x==info[i].fr&&i<n)
		{
			sum+=info[i].sc;
			q.push(info[i++].sc);
		}
		sum-=q.top();q.pop();
		ans+=sum;
		x++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
map<int,int> inv;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

set<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].insert((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	int now = -1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		while(ff[i].size()>=1)
		{
			auto now = *prev(ff[i].end());ff[i].erase(prev(ff[i].end()));
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			//cerr<<kind<<" "<<cost<<endl;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;

			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].insert((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
		//cerr<<"---------\n";
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

set<matter> ff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)) idx[x] = num++;
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;
int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		ff[id].insert((matter){can[i-1].fr,t[can[i-1].sc],can[i-1].sc});
	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(ff[i].size()>=2)q.push(i),inq[i] = 1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = ff[i].size()-1;
		for(auto it = ff[i].rbegin();it!=ff[i].rend();it++)
		{
			auto now = *it;
			int cost,kind,I;
			cost = now.cost,kind  = now.kind,I = now.id;
			kind+=x;x--;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)(x+1)*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				ff[id].insert((matter){kind,cost,I});
				if(ff[id].size()>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cassert>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef unsigned long long uLL;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<vi> vii;
typedef vector<LL> vl;
typedef vector<vl> vll;
typedef vector<vll> vlll;
typedef vector<vii> viii;
typedef double DB;

const int maxn = 200000+10;
const int maxt = 3000000+10;
const int maxp = 5;
const int maxd = 7;
const LL INF = 1e18;
const int mod =100000007;
const int apsz = 26;
const double eps = 1e-8;

map<int,int> idx;
int inv[maxn];
int num;

struct matter
{
	int kind,cost,id;
	bool operator<(const matter& rhs) const
	{
		if(kind!=rhs.kind)return kind<rhs.kind;
		if(cost!=rhs.cost)return cost>rhs.cost;
		return id<rhs.id;
	}
};

LL sumff[maxn];
LL minff[maxn];
LL maxff[maxn];
int szff[maxn];

inline int ID(int x)
{
	if(!idx.count(x)){
		idx[x] = num++;
		inv[num-1] = x;
	}
	return idx[x];
}
int a[maxn];
int t[maxn];
int n;
int inq[maxn];
vector<pii> can;
vector<int> ffcost[maxn];

int main(void)
{
	sci(n);
	_rep(i,1,n) sci(a[i]),can.pb({a[i],i});
	_rep(i,1,n) sci(t[i]);
	sort(all(can));
	_rep(i,1,n)
	{
		int id = ID(can[i-1].fr);
		szff[id]++;
		if(szff[id]==1)
			maxff[id]=minff[id] = t[can[i-1].sc],sumff[id]+=t[can[i-1].sc];
		else
		{
			maxff[id] = max((LL)t[can[i-1].sc],maxff[id]);
			minff[id] = min((LL)t[can[i-1].sc],minff[id]);
		}
		ffcost[id].pb(t[can[i-1].sc]);

	}
	LL ans = 0;
	queue<int> q;
	_for(i,0,num) if(szff[i]>=2)q.push(i),inq[i] = 1;
	while(!q.empty())
	{
		int i = q.front();q.pop();
		inq[i] = 0;
		int x = 0;
		sort(all(ffcost[i]));
		auto last = ffcost[i].back();
		while(szff[i]>=1)
		{
			auto now = ffcost[i].back();
			szff[i]--;
			ffcost[i].pop_back();
			int cost,kind,I;
			cost = now,kind = inv[i];
			kind+=x;
			//cerr<<kind<<" "<<I<<endl;
			ans+=(LL)x*cost;
			int id = ID(kind);
			//assert(id!=i);
			if(i!=id)
			{
				szff[id]++;
				ffcost[id].pb(cost);
				if(szff[id]>=2)
				{
					if(!inq[id])
					{
						q.push(id);
						inq[id] = 1;
					}
				}
			}
			x++;
		}
		ffcost[i].pb(last);
		szff[i]++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<cassert>
#include<cmath>
#include<unordered_map>
#include<algorithm>
#include<queue>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

const int maxn = 200000+10;
const int INF = 1e9+7;

LL ans = 0;
pii info[maxn];
int n;

// OK..Down to 1611 because of my little but deadly mistake
// But the problem is a very beautiful implement of Heap
// I just want to say ji bu ru ren,gan bai xia feng!!!
// We can see that we will only left the maxcost undone for
// some "matter" which have the same a's value
// then,the problem in fact can be solved,we in fact want to
// maintian the sum of some elements's t's value,whose a's value is the same,
// and the max t's value of these element,in one process we delet the element
// which has the max t's value,and add the remaining element's sum of t's
// value,and also then make all the remaining element's a's value add one
// and continuing maintain the sum of the element which has the same a's
// unitl we have iterated all the element and the "maintaining block" is empty
// So,here comes heap,it can delete the max/min element and get the max/min
// element in O(log(n)),then the complexity of time is O(nlogn)!
// Beautiful! When can I be purple....

// by the way the beautiful is created by a red juju
// %%%%%

int main(void)
{
	sci(n);
	_for(i,0,n)sci(info[i].fr);
	_for(i,0,n)sci(info[i].sc);
	priority_queue<int,vector<int>,less<int> > q;
	int x,i;
	sort(info,info+n);
	i = 0;
	LL ans = 0;
	LL sum = 0;
	while(!q.empty()||i<n)
	{
		if(q.empty())
			x = info[i].fr;
		while(x==info[i].fr&&i<n)
		{
			sum+=info[i].sc;
			q.push(info[i++].sc);
		}
		sum-=q.top();q.pop();
		ans+=sum;
		x++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <iomanip>
#include <float.h>

#define ll long long

#define FOR(i,a,b)        for(ll i=(a);i<(b);i++)
#define REP(i,n)        FOR(i,0,n)
#define SORT(v)            sort((v).begin(),(v).end())
#define UN(v)            SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end())
#define CL(a,b)            memset(a,b,sizeof a)
#define pb                push_back

using namespace std;

//#define N 1000009
ll denom = 1e9+7;
ll temp;

int main(int argc, const char * argv[])
{
    ll n; cin >> n;
    vector<pair<ll, ll>> a(n,make_pair(0, 0));
    REP(i, n) cin >> a[i].first;
    REP(i, n) cin >> a[i].second;
    SORT(a);

    if (n == 1) {
        cout << 0;
    } else {
        ll result = 0;
        ll val = 0;
        map<ll, ll> mp;
        ll sum = 0;
        FOR(i, 1, n) {
            if (a[i].first == a[i-1].first) {
                val = a[i].first;
                mp[a[i-1].second]++;
                sum += a[i-1].second;
            } else {
                if (mp.size() > 0) {
                    mp[a[i-1].second]++;
                    sum += a[i-1].second;
                }

                while (mp.size() > 0 && val < a[i].first) {
                    auto it = mp.rbegin();
                    (*it).second--;
                    sum -= (*it).first;
                    result += sum;
                    if ((*it).second == 0)
                        mp.erase((*it).first);
                    val++;
                }
            }
            if (i == n-1 && mp.size() > 0) {
                mp[a[i].second]++;
                sum += a[i].second;
            }
        }

        while (mp.size() > 0) {
            auto it = mp.rbegin();
            (*it).second--;
            sum -= (*it).first;
            result += sum;
            if ((*it).second == 0)
                mp.erase((*it).first);
        }

        cout << result;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	set<int> s;
	map <int, vector<int>> a;
	vector<int> w1(n), w2(n);
	for (int i = 0; i < n; i++) {
		cin >> w1[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> w2[i];
	}
	for (int i = 0; i < n; i++) {
		if (a.find(w1[i]) != a.end()) {
			a[w1[i]].push_back(w2[i]);
		}
		else {
			a.insert({ w1[i],{w2[i]} });
		}
	}
	long long k = 0, m = 0;
	for (auto i : a) {
		for (int j = 0; j < i.second.size(); j++) {
			s.insert(i.second[j]);
			k += i.second[j];
		}
		set<int> ::iterator p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
	}
	while (s.size() > 0) {
		set<int> ::iterator p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
	}
	cout << m;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	multiset<int> s;
	map <int, vector<int>> a;
	vector<int> w1(n), w2(n);
	for (int i = 0; i < n; i++) {
		cin >> w1[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> w2[i];
	}
	for (int i = 0; i < n; i++) {
		if (a.find(w1[i]) != a.end()) {
			a[w1[i]].push_back(w2[i]);
		}
		else {
			a.insert({ w1[i],{w2[i]} });
		}
	}
	long long k = 0, m = 0;
	multiset<int> ::iterator p = s.end();
	for (auto i : a) {
		for (int j = 0; j < i.second.size(); j++) {
			s.insert(i.second[j]);
			k += i.second[j];
		}
		p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
	}
	while (s.size() > 0) {
		p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
	}
	cout << m;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	multiset<int> s;
	map <int, vector<int>> a;
	vector<int> w1(n), w2(n);
	for (int i = 0; i < n; i++) {
		cin >> w1[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> w2[i];
	}
	for (int i = 0; i < n; i++) {
		if (a.find(w1[i]) != a.end()) {
			a[w1[i]].push_back(w2[i]);
		}
		else {
			a.insert({ w1[i],{w2[i]} });
		}
	}
	long long k = 0, m = 0;
	multiset<int> ::iterator p = s.end();
	map<int, vector<int>> ::iterator p1 = a.begin();
	for (auto i : a) {
		for (int j = 0; j < i.second.size(); j++) {
			s.insert(i.second[j]);
			k += i.second[j];
		}
		p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
		p1++;
		if (p1 == a.end()) {
			break;
		}
		else {
			int k1 = i.first + 1;
			while (s.size() > 0 && k1 < (*p1).first) {
				p = s.end();
				p--;
				k -= *p;
				s.erase(p);
				m += k;
				k1++;
			}
		}
	}
	while (s.size() > 0) {
		p = s.end();
		p--;
		k -= *p;
		s.erase(p);
		m += k;
	}
	cout << m;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

int main() {
	int N; cin >> N;
	vector<pair<ll, ll>> v(N);
	for (int i = 0; i < N; i++) {
		ll a; cin >> a;
		v[i].first = a;
	}
	for (int i = 0; i < N; i++) {
		ll b; cin >> b;
		v[i].second = b;
	}

	sort(v.begin(), v.end());
	multiset<int, greater<int>> st;
	ll all = 0;

	ll ans = 0;
	int c = 0;
	ll x = 0;
	while (c < N || st.size() > 0) {
		if (all == 0) {
			x = v[c].first;
		}
		else {
			x++;
			ans += all;
		}

		while (c < N && v[c].first == x) {
			all += v[c].second;
			st.insert((int)v[c].second);
			c++;
		}
		ll k = *st.begin();
		all -= k;
		st.erase(st.begin());
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

int main() {
	int N; cin >> N;
	vector<pair<ll, ll>> v(N);
	for (int i = 0; i < N; i++) {
		ll a; cin >> a;
		v[i].first = a;
	}
	for (int i = 0; i < N; i++) {
		ll b; cin >> b;
		v[i].second = b;
	}

	sort(v.begin(), v.end());
	multiset<ll, greater<ll>> st;
	ll all = 0;

	ll ans = 0;
	int c = 0;
	ll x = 0;
	while (c < N || st.size() > 0) {
		if (all == 0) {
			x = v[c].first;
		}
		else {
			x++;
			ans += all;
		}

		while (c < N && v[c].first == x) {
			all += v[c].second;
			st.insert(v[c].second);
			c++;
		}
		ll k = *st.begin();
		all -= k;
		st.erase(st.begin());
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

int main() {
	int N; cin >> N;
	vector<pair<ll, ll>> v(N);
	for (int i = 0; i < N; i++) {
		ll a; cin >> a;
		v[i].first = a;
	}
	for (int i = 0; i < N; i++) {
		ll b; cin >> b;
		v[i].second = b;
	}

	sort(v.begin(), v.end());
	multiset<ll> st;
	ll all = 0;

	ll ans = 0;
	int c = 0;
	ll x = 0;
	while (c < N || st.size() > 0) {
		if (all == 0) {
			x = v[c].first;
		}
		else {
			x++;
			ans += all;
		}

		while (c < N && v[c].first == x) {
			all += v[c].second;
			st.insert(v[c].second);
			c++;
		}
		auto itr = st.end(); itr--;
		ll k = *itr;
		all -= k;
		st.erase(itr);
	}

	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int main()
{
#ifdef _DEBUG
	freopen("input.txt","rt",stdin);
	freopen("output.txt","wt",stdout);
#endif
	int n;
	cin>>n;
	vector<pair<long long,long long> > A(n);
	for(int i=0;i<n;i++)
	{
		cin>>A[i].first;
	}
	for(int i=0;i<n;i++)
	{
		cin>>A[i].second;
	}
	sort(A.begin(),A.end());
	int g=0;
	set<int> st;

	set<int> ::iterator itse;
	for(int i=0;i<n;i++)
	{
		if(g<A[i].first)
		{
			g=A[i].first;
		}
		else
		{
			g++;
		}
		st.insert(g);
	}

	vector<pair<long long,long long> > B(n);
	for(int i=0;i<n;i++)
	{
		B[i].first=A[i].second;
		B[i].second=A[i].first;
	}
	sort(B.begin(),B.end());
	long long otv=0;
	for(int i=n-1;i>=0;i--)
	{
		itse=st.lower_bound(B[i].second);
		otv+=(*itse-B[i].second)*B[i].first;
		st.erase(itse);
	}
	cout<<otv;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <array>
#include <set>
#include <list>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <iterator>
//#define FILE__ "tree"
#ifdef DEBUG__
#include <chrono>
#define FILE__ "test"
#else
//#definal_valuee FILE__ "sum"
#endif

using namespace std;

#define loop(i,start,end) for(auto i = start;i < end;i ++)
#define loopi(end) loop(i,0,end)
#define loopj(end) loop(j,0,end)

#define FIRST_SOLUTION__
vector<set<int>> t(4 * 100000);
vector<long long> a;
vector<vector<long long>> d;
vector<vector<int>> g;
void build(int v, int vl, int vr)
{
}
int query(int v, int vl, int vr, int ind)
{
    if (vl == vr)
    {
        return (t[v].size() ? *t[v].rbegin() : -1);
    }
    int vm = (vr + vl) / 2;
    if (ind>vm)
    return max(t[v].size() ? *t[v].rbegin() : -1,query(2 * v + 1, vm + 1, vr, ind));
    else return max(t[v].size() ? *t[v].rbegin() : -1, query(2 * v, vl,vm, ind));
}
void update(int v, int vl, int vr, int l, int r, int val)
{
    if (l <= vl && r >= vr)
    {
        if (val > 0)
            t[v].insert(val);
        else t[v].erase(-val);
        return;
    }
    else if (vr<l || vl>r) return;
    int vm = (vl + vr) / 2;
        update(2 * v + 1, vm + 1, vr, l,r,val);
        update(2 * v, vl, vm, l,r,val);
}

#define int long long
#ifdef FIRST_SOLUTION__

vector<int> dp;
void solve()
{
    int n;
    cin >> n;
    map<int,pair<int, multiset<int>>> a;
    vector<int> t(n);
    vector<int> b(n);
    int temp;
    loopi(n)
    {
        cin >> b[i];
    }
    loopi(n) cin >> t[i];
    loopi(n)
    {
        a[b[i]].first += t[i];
        a[b[i]].second.insert(t[i]);
    }
    int as = 0;
    for (auto e : a)
    {
        if (e.second.first != *e.second.second.rbegin())
        {
            a[e.first + 1].first += e.second.first - *e.second.second.rbegin();
            auto it = e.second.second.begin();
            for (auto ry=it; ++ry!=e.second.second.end();)
            {
                a[e.first + 1].second.insert(*it);
                it++;
            }
            as += e.second.first - *e.second.second.rbegin();
        }
    }
    cout << as;
}

#else

int d(int i, vector<int>& dp, vector<pair<int, int>>& memes, int need) {
    if (dp[i] != -1)
        return dp[i];
    else {
        auto l = 0;
        int c = std::numeric_limits<int>::max() - dp.size();
        for (auto j = i - 1; j >= 0; j--) {
            if (memes[j].second == need)
                c = min(c, l + d(j, dp, memes, memes[j].first));
            l++;
        }
        return dp[i] = c;
    }
}

void solve() {
    int n;
    cin >> n;
    vector<int> dp(n + 1,-1);
    vector<pair<int, int>> meme(n+1);
    meme[0] = { 1,1 };
    vector<pair<int,int>> govno(n+1);
    govno[0] = { 1,1 };
    loopi(n)
    {
        int tmp;
        cin >> tmp;
        cin >> meme[i + 1].first;
        if (tmp == 1) { dp[i + 1] = 0; meme[i + 1].second = meme[i + 1].first; }
        else cin >> meme[i + 1].second;
        govno[i+1] = { tmp,meme[i + 1].second };
    }
    vector<int> ngovno(n+1);
    vector<int> ct(n);
    int shit = 0;
    for (int i = n; i >= 0; i--)
    {
        ngovno[i] = ct[meme[i].second-1]+shit;
        if (govno[i].first == 1) shit++; else
        ct[meme[i].first-1]++;

    }
    dp[0] = 0;
    for (int i = n; i >= 0; i--)
    {
        d(i, dp, meme, meme[i].first);
    }
    vector<int> ans(n, numeric_limits<int>::max());
    loopi(n+1)
    {
        int kek = meme[i].second;
        ans[kek-1] = min(ans[kek-1], dp[i] + ngovno[i]);
    }
        loopi(n) cout << (ans[i]== numeric_limits<int>::max() ?-1:ans[i])<<endl;
    return;
}
#endif

void main() {
#ifdef DEBUG__
    auto timestamn = std::chrono::system_clock::now();
#else
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
#endif
#ifdef FILE__
    freopen(FILE__".in", "r", stdin);
    freopen(FILE__".out", "w", stdout);
#endif
    solve();
#ifdef DEBUG__
    cout << endl << "Time elansed: " << chrono::duration_cast<chrono::milliseconds>(std::chrono::system_clock::now() - timestamn).count() << " ms" << endl;
#endif
    return ;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <array>
#include <set>
#include <list>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <iterator>
//#define FILE__ "tree"
#ifdef DEBUG__
#include <chrono>
#define FILE__ "test"
#else
//#definal_valuee FILE__ "sum"
#endif

using namespace std;

#define loop(i,start,end) for(auto i = start;i < end;i ++)
#define loopi(end) loop(i,0,end)
#define loopj(end) loop(j,0,end)

#define FIRST_SOLUTION__
vector<set<int>> t(4 * 100000);
vector<long long> a;
vector<vector<long long>> d;
vector<vector<int>> g;
void build(int v, int vl, int vr)
{
}
int query(int v, int vl, int vr, int ind)
{
    if (vl == vr)
    {
        return (t[v].size() ? *t[v].rbegin() : -1);
    }
    int vm = (vr + vl) / 2;
    if (ind>vm)
    return max(t[v].size() ? *t[v].rbegin() : -1,query(2 * v + 1, vm + 1, vr, ind));
    else return max(t[v].size() ? *t[v].rbegin() : -1, query(2 * v, vl,vm, ind));
}
void update(int v, int vl, int vr, int l, int r, int val)
{
    if (l <= vl && r >= vr)
    {
        if (val > 0)
            t[v].insert(val);
        else t[v].erase(-val);
        return;
    }
    else if (vr<l || vl>r) return;
    int vm = (vl + vr) / 2;
        update(2 * v + 1, vm + 1, vr, l,r,val);
        update(2 * v, vl, vm, l,r,val);
}

#define int long long
#ifdef FIRST_SOLUTION__

vector<int> dp;
void solve()
{
    int n;
    cin >> n;
    map<int,pair<int, multiset<int>>> a;
    vector<int> t(n);
    vector<int> b(n);
    int temp;
    loopi(n)
    {
        cin >> b[i];
    }
    loopi(n) cin >> t[i];
    loopi(n)
    {
        a[b[i]].first += t[i];
        a[b[i]].second.insert(t[i]);
    }
    int as = 0;
    multiset<int> work;
    int c = 0;
    for (auto e : a)
    {
        auto it = e.second.second.begin();
        for (; it != e.second.second.end();)
        {
            work.insert(*it);
            c += *it;
            it++;

        }
        if (work.size())
        {
            as += c - *work.rbegin();
            c -= *(--work.end());
            work.erase(--work.end());

        }
    }
    cout << as;
}

#else

int d(int i, vector<int>& dp, vector<pair<int, int>>& memes, int need) {
    if (dp[i] != -1)
        return dp[i];
    else {
        auto l = 0;
        int c = std::numeric_limits<int>::max() - dp.size();
        for (auto j = i - 1; j >= 0; j--) {
            if (memes[j].second == need)
                c = min(c, l + d(j, dp, memes, memes[j].first));
            l++;
        }
        return dp[i] = c;
    }
}

void solve() {
    int n;
    cin >> n;
    vector<int> dp(n + 1,-1);
    vector<pair<int, int>> meme(n+1);
    meme[0] = { 1,1 };
    vector<pair<int,int>> govno(n+1);
    govno[0] = { 1,1 };
    loopi(n)
    {
        int tmp;
        cin >> tmp;
        cin >> meme[i + 1].first;
        if (tmp == 1) { dp[i + 1] = 0; meme[i + 1].second = meme[i + 1].first; }
        else cin >> meme[i + 1].second;
        govno[i+1] = { tmp,meme[i + 1].second };
    }
    vector<int> ngovno(n+1);
    vector<int> ct(n);
    int shit = 0;
    for (int i = n; i >= 0; i--)
    {
        ngovno[i] = ct[meme[i].second-1]+shit;
        if (govno[i].first == 1) shit++; else
        ct[meme[i].first-1]++;

    }
    dp[0] = 0;
    for (int i = n; i >= 0; i--)
    {
        d(i, dp, meme, meme[i].first);
    }
    vector<int> ans(n, numeric_limits<int>::max());
    loopi(n+1)
    {
        int kek = meme[i].second;
        ans[kek-1] = min(ans[kek-1], dp[i] + ngovno[i]);
    }
        loopi(n) cout << (ans[i]== numeric_limits<int>::max() ?-1:ans[i])<<endl;
    return;
}
#endif

void main() {
#ifdef DEBUG__
    auto timestamn = std::chrono::system_clock::now();
#else
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
#endif
#ifdef FILE__
    freopen(FILE__".in", "r", stdin);
    freopen(FILE__".out", "w", stdout);
#endif
    solve();
#ifdef DEBUG__
    cout << endl << "Time elansed: " << chrono::duration_cast<chrono::milliseconds>(std::chrono::system_clock::now() - timestamn).count() << " ms" << endl;
#endif
    return ;
}


#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;
ll m[301][301]={0};

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin>>n;
    for(ll i=0;i<n;i++){
    	for(ll j=0;j<n;j++){
    		cin>>m[i][j];
    	}
    }
    ll q;
    cin>>q;
    while(q--){
    	ll a,b,c;
    	cin>>a>>b>>c;
    	a--;
    	b--;
    	if(m[a][b]>c){
    		m[a][b]=c;
    		m[b][a]=c;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				if(i==j){
    					continue;
    				}
    				if(m[i][j]>m[i][a]+m[a][j]){
    					m[i][j]=m[i][a]+m[a][j];
    					m[j][i]=m[i][j];
    				}
    				if(m[i][j]>m[i][b]+m[b][j]){
    					m[i][j]=m[i][b]+m[b][j];
    					m[j][i]=m[i][j];
    				}
    			}
    		}
    	}
    	ll sum=0;
    	for(ll i=0;i<n;i++){
    		for(ll j=i+1;j<n;j++){
    			sum+=m[i][j];
    		}
    	}
    	cout<<sum<<" ";
    }

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;
int m[301][301]={0};

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin>>n;
    for(ll i=0;i<n;i++){
    	for(ll j=0;j<n;j++){
    		cin>>m[i][j];
    	}
    }
    ll q;
    cin>>q;
    while(q--){
    	ll a,b,c;
    	cin>>a>>b>>c;
    	a--;
    	b--;
    	if(m[a][b]>c){
    		m[a][b]=c;
    		m[b][a]=c;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				if(m[i][j]>m[i][a]+m[a][j]){
    					m[i][j]=m[i][a]+m[a][j];
    				}
    				if(m[i][j]>m[i][b]+m[b][j]){
    					m[i][j]=m[i][b]+m[b][j];
    				}
    			}
    		}
    	}
    	ll sum=0;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				sum+=m[i][j];
    			}
    		}
    		cout<<sum/2<<" ";
    }

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;
int m[301][301]={0};

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin>>n;
    for(ll i=0;i<n;i++){
    	for(ll j=0;j<n;j++){
    		cin>>m[i][j];
    	}
    }
    ll q;
    cin>>q;
    while(q--){
    	ll a,b,c;
    	cin>>a>>b>>c;
    	a--;
    	b--;
    	if(m[a][b]>c){
    		m[a][b]=c;
    		m[b][a]=c;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				if(m[i][j]>m[i][a]+m[a][j]){
    					m[i][j]=m[i][a]+m[a][j];
    				}
    				if(m[i][j]>m[i][b]+m[b][j]){
    					m[i][j]=m[i][b]+m[b][j];
    				}
    			}
    		}
    		ll sum=0;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				sum+=m[i][j];
    			}
    		}
    		cout<<sum/2<<" ";
    	}
    }

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;
ll m[301][301]={0};

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin>>n;
    for(ll i=0;i<n;i++){
    	for(ll j=0;j<n;j++){
    		cin>>m[i][j];
    	}
    }
    ll q;
    cin>>q;
    while(q--){
    	ll a,b,c;
    	cin>>a>>b>>c;
    	a--;
    	b--;
    	if(m[a][b]>c){
    		m[a][b]=c;
    		m[b][a]=c;
    		for(ll i=0;i<n;i++){
    			for(ll j=0;j<n;j++){
    				if(i==j){
    					continue;
    				}
    				if(m[i][j]>m[i][a]+m[a][j]){
    					m[i][j]=m[i][a]+m[a][j];
    				}
    				if(m[i][j]>m[i][b]+m[b][j]){
    					m[i][j]=m[i][b]+m[b][j];
    				}
    			}
    		}
    	}
    	ll sum=0;
    	for(ll i=0;i<n;i++){
    		for(ll j=0;j<n;j++){
    			sum+=m[i][j];
    		}
    	}
    	cout<<sum/2LL<<" ";
    }

	return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define INF 1000000

typedef long long int ll;

using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n; cin >> n;
    ll dp[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++){
            ll a; cin >> a;
            dp[i][j]=a;
        }
    int k;
    cin >> k;
    ll ans[k];
    for(int r=0; r<k; r++){
            ll x,y,w;
            cin >> x >> y >> w;
            x--;y--;
            if(dp[x][y]>w)
                dp[x][y]=dp[y][x]=w;
            //else continue;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    dp[i][j]=min(dp[i][j], dp[i][x]+dp[x][y] + dp[y][j]);
                    dp[i][j]=min(dp[i][j], dp[i][y]+dp[y][x] + dp[x][j]);
                }
            }
            ans[r]=0;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++)
                    ans[r]+=dp[i][j];
            }
            cout << ans[r]/2 << " ";
    }
}


#include<bits/stdc++.h>
#define endl '\n'
#define INF 1000000

typedef long long int ll;

using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n; cin >> n;
    int dp[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++){
            int a; cin >> a;
            dp[i][j]=a;
        }
    int k;
    cin >> k;
    int ans[k];
    for(int r=0; r<k; r++){
            int x,y,w;
            cin >> x >> y >> w;
            x--;y--;
            if(dp[x][y]>w)
                dp[x][y]=dp[y][x]=w;
            //else continue;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    dp[i][j]=min(dp[i][j], dp[i][x]+dp[x][y] + dp[y][j]);
                    dp[i][j]=min(dp[i][j], dp[i][y]+dp[y][x] + dp[x][j]);
                }
            }
            ans[r]=0;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++)
                    ans[r]+=dp[i][j];
            }
            cout << ans[r]/2 << " ";
    }
}


#include<bits/stdc++.h>
#define endl '\n'
#define INF 1000000

typedef long long int ll;

using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n; cin >> n;
    int dp[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++){
            int a; cin >> a;
            dp[i][j]=a;
        }
    int k;
    cin >> k;
    int ans[k];
    for(int r=0; r<k; r++){
            int x,y,w;
            cin >> x >> y >> w;
            x--;y--;
            dp[x][y]=dp[y][x]=w;
            for(int f=0 ; f<n; f++){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        dp[i][j]=min(dp[i][j], dp[i][f]+dp[f][j]);
                    }
                }
            }
            ans[r]=0;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++)
                    ans[r]+=dp[i][j];
            }
            cout << ans[r]/2 << " ";
    }
}


#include<bits/stdc++.h>
#define endl '\n'
#define INF 1000000

typedef long long int ll;

using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n; cin >> n;
    int dp[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++){
            int a; cin >> a;
            dp[i][j]=a;
        }
    int k;
    cin >> k;
    int ans[k];
    for(int r=0; r<k; r++){
            int x,y,w;
            cin >> x >> y >> w;
            x--;y--;
            //if(dp[x][y]>w)
                dp[x][y]=dp[y][x]=w;
            //else continue;*/
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    dp[i][j]=min(dp[i][j], dp[i][x]+dp[x][y] + dp[y][j]);
                    dp[i][j]=min(dp[i][j], dp[i][y]+dp[y][x] + dp[x][j]);
                }
            }
            ans[r]=0;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++)
                    ans[r]+=dp[i][j];
            }
            cout << ans[r]/2 << " ";
    }
}


#include<bits/stdc++.h>
#define endl '\n'
#define INF 1000000

typedef long long int ll;

using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n; cin >> n;
    int dp[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++){
            int a; cin >> a;
            dp[i][j]=a;
        }
    int k;
    cin >> k;
    int ans[k];
    for(int r=0; r<k; r++){
            int x,y,w;
            cin >> x >> y >> w;
            x--;y--;
            if(dp[x][y]>w)
                dp[x][y]=dp[y][x]=w;
            for(int f=0 ; f<n; f++){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        dp[i][j]=min(dp[i][j], dp[i][f]+dp[f][j]);
                    }
                }
            }
            ans[r]=0;
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++)
                    ans[r]+=dp[i][j];
            }
            cout << ans[r]/2 << " ";
    }
}


//https://codeforces.com/problemset/problem/25/C
//C. Roads in Berland

#include <iostream>
#include <vector>

using namespace std;

void floyd_warshall(vector <vector <int> > & Graph)
{
	int n = Graph.size();
	for(int k = 0; k < n; ++k)
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				Graph[i][j] = Graph[j][i] = min(Graph[i][j], Graph[i][k] + Graph[k][j]);
	return;
}

int main(void)
{
	int n;
	cin >> n;
	vector <vector <int> > arr(n, vector <int>(n, 0));

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

	int k;
	cin >> k;
	while(k--){
		int u, v, c;
		cin >> u >> v >> c;
		--u, --v;

		if(c < arr[u][v]){
			arr[u][v] = arr[v][u] = c;
			floyd_warshall(arr);
		}

		int sum = 0;
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				sum += arr[i][j];

		sum /= 2;
		cout << sum << ' ';
	}
	cout << endl;
	return 0;
}


//https://codeforces.com/problemset/problem/25/C
//C. Roads in Berland

#include <iostream>
#include <vector>

using namespace std;

void floyd_warshall(vector <vector <int> > & Graph, vector <int> modified_nodes)
{
	int n = Graph.size();
	for(auto k : modified_nodes)
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				Graph[i][j] = Graph[j][i] = min(Graph[i][j], Graph[i][k] + Graph[k][j]);
	return;
}

int main(void)
{
	int n;
	cin >> n;
	vector <vector <int> > arr(n, vector <int>(n, 0));

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

	int k;
	cin >> k;
	while(k--){
		int u, v, c;
		cin >> u >> v >> c;
		--u, --v;

		if(c < arr[u][v]){
			vector <int> nodes;
			nodes.push_back(u);
			nodes.push_back(v);
			arr[u][v] = arr[v][u] = c;
			floyd_warshall(arr, nodes);
		}

		int sum = 0;
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				sum += arr[i][j];

		sum /= 2;
		cout << sum << ' ';
	}
	cout << endl;
	return 0;
}


//https://codeforces.com/problemset/problem/25/C
//C. Roads in Berland

#include <iostream>
#include <vector>

using namespace std;

void floyd_warshall(vector <vector <int> > & Graph, vector <int> modified_nodes)
{
	int n = Graph.size();
	for(auto k : modified_nodes)
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				Graph[i][j] = Graph[j][i] = min(Graph[i][j], Graph[i][k] + Graph[k][j]);
	return;
}

int main(void)
{
	int n;
	cin >> n;
	vector <vector <int> > arr(n, vector <int>(n, 0));

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

	int k;
	cin >> k;
	while(k--){
		int u, v, c;
		cin >> u >> v >> c;
		--u, --v;

		if(c < arr[u][v]){
			vector <int> nodes;
			nodes.push_back(u);
			nodes.push_back(v);
			arr[u][v] = arr[v][u] = c;
			floyd_warshall(arr, nodes);
		}

		long long int sum = 0;
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				sum += arr[i][j];

		sum /= 2;
		cout << sum << ' ';
	}
	cout << endl;
	return 0;
}


//https://codeforces.com/problemset/problem/25/C
//C. Roads in Berland

#include <iostream>
#include <vector>

using namespace std;

void floyd_warshall(vector <vector <int> > & Graph)
{
	int n = Graph.size();
	for(int k = 0; k < n; ++k)
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				Graph[i][j] = Graph[j][i] = min(Graph[i][j], Graph[i][k] + Graph[k][j]);
	return;
}

int main(void)
{
	int n;
	cin >> n;
	vector <vector <int> > arr(n, vector <int>(n, 0));

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

	int k;
	cin >> k;
	while(k--){
		int u, v, c;
		cin >> u >> v >> c;
		--u, --v;

		if(c < arr[u][v]){
			arr[u][v] = arr[u][v] = c;
			floyd_warshall(arr);
		}

		int sum = 0;
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				sum += arr[i][j];

		sum /= 2;
		cout << sum << ' ';
	}
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX/10
struct edge{
	int s, e, w;
};

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    long sum = 0;
    int d[305][305];
    for ( int i = 0 ; i < n ; i++ )
        for ( int j = 0 ; j < n ; j++ ){
            cin >> d[i][j];
            if ( i > j )    sum += d[i][j];
        }

	int k,v1,v2,w;
	cin >> k;
	for ( int t = 0 ;t < k ; t++ ){
	    cin >> v1 >> v2 >> w;   // v1 and v2 are 1 indexed
	    v1 = v1-1; v2 = v2-1;
	    if ( d[v1][v2] <= w ){  cout << sum << " "; continue;    }

	    d[v1][v2] = d[v2][v1] = w;

	    for( int i = 0 ; i < n ; i++ )
	        for ( int j = 0 ; j < n ; j++ ){
	            d[i][j] = min(d[i][j], d[i][v1]+d[v1][j]);
	            d[i][j] = min(d[i][j], d[i][v2]+d[v2][j]);
	            d[j][i] = d[i][j];
	        }
        sum = 0;
        for ( int i = 0 ; i < n ; i++ )
            for ( int j = i+1 ; j < n ;j++)
                sum+=d[i][j];

	    cout << sum << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX/10
struct edge{
	int s, e, w;
};

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    long sum = 0;
    int d[305][305];
    for ( int i = 0 ; i < n ; i++ )
        for ( int j = 0 ; j < n ; j++ ){
            cin >> d[i][j];
            if ( i > j )    sum += d[i][j];
        }

	int k,v1,v2,w;
	cin >> k;
	for ( int t = 0 ;t < k ; t++ ){
	    cin >> v1 >> v2 >> w;   // v1 and v2 are 1 indexed
	    v1 = v1-1; v2 = v2-1;
	    if ( d[v1][v2] <= w ){  cout << sum << " "; continue;    }

	    sum += w - d[v1][v2];
	    d[v1][v2] = w;
	    d[v2][v1] = w;

	    for ( int x = 0 ; x < n ; x++ )
	        for ( int i = 0 ; i < n ; i++ )
	            for ( int j = i+1 ; j < n ; j++ ){

	                if ( d[i][j] > d[i][x] + d[x][j] ){
	                    sum += ( d[i][x] + d[x][j] - d[i][j] );
	                    d[i][j] = d[i][x] + d[x][j];
	                    d[j][i] = d[i][j];
	                }

	            }

	    cout << sum << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX/10

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long n;
    cin >> n;
    long sum = 0;
    long d[305][305];
    for ( long i = 0 ; i < n ; i++ )
        for ( long j = 0 ; j < n ; j++ ){
            cin >> d[i][j];
            if ( i > j )    sum += d[i][j];
        }

	long k,v1,v2,w;
	cin >> k;
	for ( long t = 0 ;t < k ; t++ ){
	    cin >> v1 >> v2 >> w;   // v1 and v2 are 1 indexed
	    v1 = v1-1; v2 = v2-1;
	    if ( d[v1][v2] <= w ){  cout << sum << " "; continue;    }

	    d[v1][v2] = d[v2][v1] = w;

	    for( long i = 0 ; i < n ; i++ )
	        for ( long j = 0 ; j < n ; j++ ){
	            d[i][j] = min(d[i][j], d[i][v1]+d[v1][j]);
	            d[i][j] = min(d[i][j], d[i][v2]+d[v2][j]);
	            d[j][i] = d[i][j];
	        }
        sum = 0;
        for ( long i = 0 ; i < n ; i++ )
            for ( long j = i+1 ; j < n ;j++)
                sum+=d[i][j];

	    cout << sum << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX/10
struct edge{
	int s, e, w;
};

int main(){

    int n;
    cin >> n;
    long sum = 0;
    int d[305][305];
    for ( int i = 0 ; i < n ; i++ )
        for ( int j = 0 ; j < n ; j++ ){
            cin >> d[i][j];
            if ( i > j )    sum += d[i][j];
        }

	int k,v1,v2,w;
	cin >> k;
	for ( int t = 0 ;t < k ; t++ ){
	    cin >> v1 >> v2 >> w;   // v1 and v2 are 1 indexed
	    v1 = v1-1; v2 = v2-1;
	    if ( d[v1][v2] <= w ){  cout << sum << " "; continue;    }

	    sum += w - d[v1][v2];
	    d[v1][v2] = w;
	    d[v2][v1] = w;

	    for ( int x = 0 ; x < n ; x++ )
	        for ( int i = 0 ; i < n ; i++ )
	            for ( int j = i+1 ; j < n ; j++ ){

	                if ( d[i][j] > d[i][x] + d[x][j] ){
	                    sum += ( d[i][x] + d[x][j] - d[i][j] );
	                    d[i][j] = d[i][x] + d[x][j];
	                    d[j][i] = d[i][j];
	                }
	            }

	    cout << sum << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX/10
#define ull unsigned long long

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ull n;
    cin >> n;
    ull sum = 0;
    ull d[305][305];
    for ( ull i = 0 ; i < n ; i++ )
        for ( ull j = 0 ; j < n ; j++ ){
            cin >> d[i][j];
            if ( i > j )    sum += d[i][j];
        }

	ull k,v1,v2,w;
	cin >> k;
	for ( ull t = 0 ;t < k ; t++ ){
	    cin >> v1 >> v2 >> w;   // v1 and v2 are 1 indexed
	    v1 = v1-1; v2 = v2-1;
	    if ( d[v1][v2] <= w ){  cout << sum << " "; continue;    }

	    d[v1][v2] = d[v2][v1] = w;

	    for( ull i = 0 ; i < n ; i++ )
	        for ( ull j = 0 ; j < n ; j++ ){
	            d[i][j] = min(d[i][j], d[i][v1]+d[v1][j]);
	            d[i][j] = min(d[i][j], d[i][v2]+d[v2][j]);
	            d[j][i] = d[i][j];
	        }
        sum = 0;
        for ( ull i = 0 ; i < n ; i++ )
            for ( ull j = i+1 ; j < n ;j++)
                sum+=d[i][j];

	    cout << sum << " ";
	}

	return 0;
}


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

/*** Template Begins ***/
#define int ll
typedef long long                   ll;
typedef pair<int,int>               PII;
typedef pair<ll, pair<int, int> >   PIII;
typedef vector<int>                 vi;
typedef vector<pair<int, int> >     vii;

#define endl         '\n'
#define pb           push_back
#define INF          INT_MAX/10
#define F            first
#define S            second
#define mp           make_pair
#define ios          ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define hell         1000000007
#define all(a)       (a).begin(),(a).end()
#define fr(i,a,b)    for(int i=a;i<=b;i++)
#define lp(i,a)      for(int i=0;i< a;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

// Constants //

const int N = 3e2+7;
const int xinc[] = {0, 0, 1, -1};
const int yinc[] = {1, -1, 0, 0};
const long double PI = acos(-1.0);

/*** Template Ends ***/
int n, d[N][N], k, a, b, nd;

void solve(){

    cin >> n;
    fr(i,1,n) fr(j,1,n) cin >> d[i][j];

    cin >> k;
    lp(i,k)
    {
        cin >> a >> b >> nd;

        if (nd < d[a][b])
        {
            d[a][b] = d[b][a] = nd;
            fr(i,1,n) fr(j,1,n)
                d[i][j] = min(d[i][b] + nd + d[a][j], min(d[i][j], d[i][a] + nd + d[b][j]));
        }

        int sum = 0;
        fr(i,1,n) fr(j,i+1,n) sum += d[i][j];
        cout << sum << " ";
    }
    cout << endl;
}

signed main(){

    ios;
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t;
    //cin >> t;
    t = 1;
    while(t--)
        solve();

    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int main()
{
//    std::ios::sync_with_stdio(false);
//    cin.tie(0);
    ll n;
    sl(n);
ll dist[n+1][n+1];
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
        {
            cin>>dist[i][j];
        }
        ll q;
        sl(q);
        while(q--)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<endl;
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int dist[333][333];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    si(n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x;
            si(x);
            dist[i][j]=x;
        }
        int q;
        si(q);
        while(q--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            int sum=0;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    sl(n);
ll dist[n+1][n+1];
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
        {
            cin>>dist[i][j];
        }
        ll q;
        sl(q);
        while(q--)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    sl(n);
ll dist[n+1][n+1];
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
        {
            ll x;
            sl(x);
            dist[i][j]=x;
        }
        ll q;
        sl(q);
        while(q--)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
ll dist[333][333];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    si(n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x;
            si(x);
            dist[i][j]=x;
        }
        int q;
        si(q);
        while(q--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}

        }
        int q;
        si(q);
        while(q--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            int sum=0;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
ll dist[333][333];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    si(n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x;
            si(x);
            dist[i][j]=x;
        }
        int q;
        si(q);
        while(q--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}

            }
            cout<<sum/2<<" ";
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    si(n);
ll dist[n+1][n+1];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x;
            si(x);
            dist[i][j]=x;
        }
        int q;
        si(q);
        while(q--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(int i=1;i<=n;i++)
            {
            for(int j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<" ";
        }
    return 0;
}


//Siddharth Yadav
#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define ll long long
#define pii pair <int ,int >
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define SET(arr,val) memset(arr,val,sizeof arr)
const ll inf=1e15,mod=1e8;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    sl(n);
ll dist[n+1][n+1];
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
        {
            cin>>dist[i][j];
        }
        ll q;
        sl(q);
        while(q--)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                dist[i][j]=min(dist[i][j],min(dist[i][a]+c+dist[b][j],dist[i][b]+c+dist[a][j]));
            }
            }
            ll sum=0;
            for(ll i=1;i<=n;i++)
            {
            for(ll j=1;j<=n;j++)
            {
                sum+=dist[i][j];
            }
            }
            cout<<sum/2<<endl;
        }
    return 0;
}