#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define MOD 1000000007
#define N 2000100

#define scd(x) scanf("%d",&x);
#define prd(x) prllf("%d\n",x);
#define scl(x) scanf("%lld",&x);
#define prl(x) prllf("%lld",x);
#define forall(i,j) memset(i,j,sizeof(i));

#define FOR(i,k,e) for (ll i=e ; i<k ; i++)
#define RFOR(i,k,e) for (ll i=e ; i>=k ; i--)

typedef vector<ll> vi;
typedef vector<ll> vl;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vpi;
typedef vector< pll > vpl;

// ll tree[4*N];
// ll lazy[4*N];

// void update(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return ;
//    }
//  if(si>=i && sj<=j){
//    ll l = sj-si+1;
//    ll temp = tree[index];
//    tree[index] = l - tree[index];
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index+1] + 1;
//      lazy[2*index + 2] = lazy[2*index+2] + 1;

//    }
//    return;

//  }

//   update(i, j, si , (si + sj)/2 , 2*index + 1);
//   update( i, j, (si + sj)/2 +1 , sj , 2*index + 2);
//   tree[index] = tree[2*index+1] + tree[2*index+2];

// }
// ll query(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return 0;
//    }
//  if(si>=i && sj<=j){
//    return tree[index];
//  }

//  return  query(i, j, si , (si + sj)/2 , 2*index + 1)
//      + query( i,  j, (si + sj)/2 +1 , sj , 2*index + 2);

// }
// ll make_tree(ll i,ll j,ll r){
//  if(i==j){
//    tree[r] = euler[i];
//    return tree[r];
//  }
//  tree[r] = make_tree(i,(i+j)/2,2*r+1) + make_tree(((i+j)/2)+1,j,2*r+2);
//  return tree[r];
// }
// void dfs(ll r){
//  vis[r] = 1;
//  FOR(i,adj[r].size(),0){
//    if(vis[adj[r][i]] == 0){
//      dfs(adj[r][i]);
//    }
//  }
// }{}
bool isOpen(char c)
{
    if(c == '(' || c == '{' || c == '[')
        return true;
    return false;
}

bool isBalanced(char popped, char now)
{
    if(popped == '(' && now ==')')
        return true;
    if(popped == '[' && now ==']')
        return true;
    if(popped == '{' && now =='}')
        return true;
    return false;
}
ll ch(string str){
        stack<char> s;

        ll no = 0;
        for(ll i = 0; i < str.length(); i++)
        {
            char now = str[i];
            if(isOpen(now))
                s.push(now);
            else
            {
                char popped;
                if(s.size() > 0)
                {
                    popped = s.top();
                    s.pop();
                }
                else
                {
                    no = 1;
                    break;
                }
                if(isBalanced(popped, now))
                    continue;
                else
                {
                    no = 1;
                    break;
                }
            }
        }
        if(s.size() != 0)
           { no = 1;}
        if(no)
            {
              return 0;
            }
        else
        {
            return 1;
        }

      }

string ch2(string s){
  stack<char>st;
  FOR(i,s.length(),0){
    if(st.size()>0){
      if(s[i]==')'&&st.top()=='('){
        st.pop();
      }
      else{
        st.push(s[i]);
      }
    }
    else{
      st.push(s[i]);
    }
  }
  // cout << s.size() << " ";
              string temp;
              while(!st.empty()){
                temp += st.top();
                st.pop();

              }
              FOR(i,temp.length(),0){
                if(temp[i]==')'){temp[i]='(';}
                else{temp[i]=')';}
              }

              return temp;
}

string change(string s){
  stack<char>st;
  FOR(i,s.length(),0){
    if(st.size()>0){
      if(s[i]==')'&&st.top()=='('){
        st.pop();
      }
      else{
        st.push(s[i]);
      }
    }
    else{
      st.push(s[i]);
    }
  }
  // cout << s.size() << " ";
              string temp;
              while(!st.empty()){
                temp += st.top();
                st.pop();

              }
              reverse(temp.begin(),temp.end());
              return temp;
}
int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  ll n;
  cin >> n;
  vector<string>v;
  FOR(i,n,0){
    string s;
    cin >> s;
    v.push_back(s);
    if(!ch(v[i])){
      v[i] = change(v[i]);
    }
    // cout << v[i] << " ";
  }
  // sort(v.begin(),v.end());
  ll c= 0;
  ll ans=0;
  map<string,ll>mp;
  FOR(i,n,0){

    if(ch(v[i])){
      c++;
    }
    else{
      string res = ch2(v[i]);
      // cout << res << " ";
      mp[res]++;
    }
  }
  FOR(i,n,0){
    if(mp[v[i]]){
      string res = ch2(v[i]);
      if(res[0]=='(')
      {ans += mp[v[i]];}
    }
  }

  ans += c*c ;
  cout << ans;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define MOD 1000000007
#define N 2000100

#define scd(x) scanf("%d",&x);
#define prd(x) prllf("%d\n",x);
#define scl(x) scanf("%lld",&x);
#define prl(x) prllf("%lld",x);
#define forall(i,j) memset(i,j,sizeof(i));

#define FOR(i,k,e) for (ll i=e ; i<k ; i++)
#define RFOR(i,k,e) for (ll i=e ; i>=k ; i--)

typedef vector<ll> vi;
typedef vector<ll> vl;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vpi;
typedef vector< pll > vpl;

// ll tree[4*N];
// ll lazy[4*N];

// void update(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return ;
//    }
//  if(si>=i && sj<=j){
//    ll l = sj-si+1;
//    ll temp = tree[index];
//    tree[index] = l - tree[index];
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index+1] + 1;
//      lazy[2*index + 2] = lazy[2*index+2] + 1;

//    }
//    return;

//  }

//   update(i, j, si , (si + sj)/2 , 2*index + 1);
//   update( i, j, (si + sj)/2 +1 , sj , 2*index + 2);
//   tree[index] = tree[2*index+1] + tree[2*index+2];

// }
// ll query(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return 0;
//    }
//  if(si>=i && sj<=j){
//    return tree[index];
//  }

//  return  query(i, j, si , (si + sj)/2 , 2*index + 1)
//      + query( i,  j, (si + sj)/2 +1 , sj , 2*index + 2);

// }
// ll make_tree(ll i,ll j,ll r){
//  if(i==j){
//    tree[r] = euler[i];
//    return tree[r];
//  }
//  tree[r] = make_tree(i,(i+j)/2,2*r+1) + make_tree(((i+j)/2)+1,j,2*r+2);
//  return tree[r];
// }
// void dfs(ll r){
//  vis[r] = 1;
//  FOR(i,adj[r].size(),0){
//    if(vis[adj[r][i]] == 0){
//      dfs(adj[r][i]);
//    }
//  }
// }{}
bool isOpen(char c)
{
    if(c == '(' || c == '{' || c == '[')
        return true;
    return false;
}

bool isBalanced(char popped, char now)
{
    if(popped == '(' && now ==')')
        return true;
    if(popped == '[' && now ==']')
        return true;
    if(popped == '{' && now =='}')
        return true;
    return false;
}
ll ch(string str){
        stack<char> s;

        ll no = 0;
        for(ll i = 0; i < str.length(); i++)
        {
            char now = str[i];
            if(isOpen(now))
                s.push(now);
            else
            {
                char popped;
                if(s.size() > 0)
                {
                    popped = s.top();
                    s.pop();
                }
                else
                {
                    no = 1;
                    break;
                }
                if(isBalanced(popped, now))
                    continue;
                else
                {
                    no = 1;
                    break;
                }
            }
        }
        if(s.size() != 0)
           { no = 1;}
        if(no)
            {
              return 0;
            }
        else
        {
            return 1;
        }

      }

string ch2(string s){
  stack<char>st;
  FOR(i,s.length(),0){
    if(st.size()>0){
      if(s[i]==')'&&st.top()=='('){
        st.pop();
      }
      else{
        st.push(s[i]);
      }
    }
    else{
      st.push(s[i]);
    }
  }
  // cout << s.size() << " ";
              string temp;
              while(!st.empty()){
                temp += st.top();
                st.pop();

              }
              FOR(i,temp.length(),0){
                if(temp[i]==')'){temp[i]='(';}
                else{temp[i]=')';}
              }

              return temp;
}
int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  ll n;
  cin >> n;
  vector<string>v;
  FOR(i,n,0){
    string s;
    cin >> s;
    v.push_back(s);
  }
  ll c= 0;
  ll ans=0;
  map<string,ll>mp;
  FOR(i,n,0){

    if(ch(v[i])){
      c++;
    }
    else{
      string res = ch2(v[i]);
      if(mp[v[i]]){
        ans++;
      }
      else{
        mp[res] = 1;
      }
    }
  }
  if(c==1){
    ans++;
    cout << ans;return 0;
  }

  ans += c*c ;
  cout << ans;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define MOD 1000000007
#define N 2000100

#define scd(x) scanf("%d",&x);
#define prd(x) prllf("%d\n",x);
#define scl(x) scanf("%lld",&x);
#define prl(x) prllf("%lld",x);
#define forall(i,j) memset(i,j,sizeof(i));

#define FOR(i,k,e) for (ll i=e ; i<k ; i++)
#define RFOR(i,k,e) for (ll i=e ; i>=k ; i--)

typedef vector<ll> vi;
typedef vector<ll> vl;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vpi;
typedef vector< pll > vpl;

// ll tree[4*N];
// ll lazy[4*N];

// void update(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return ;
//    }
//  if(si>=i && sj<=j){
//    ll l = sj-si+1;
//    ll temp = tree[index];
//    tree[index] = l - tree[index];
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index+1] + 1;
//      lazy[2*index + 2] = lazy[2*index+2] + 1;

//    }
//    return;

//  }

//   update(i, j, si , (si + sj)/2 , 2*index + 1);
//   update( i, j, (si + sj)/2 +1 , sj , 2*index + 2);
//   tree[index] = tree[2*index+1] + tree[2*index+2];

// }
// ll query(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return 0;
//    }
//  if(si>=i && sj<=j){
//    return tree[index];
//  }

//  return  query(i, j, si , (si + sj)/2 , 2*index + 1)
//      + query( i,  j, (si + sj)/2 +1 , sj , 2*index + 2);

// }
// ll make_tree(ll i,ll j,ll r){
//  if(i==j){
//    tree[r] = euler[i];
//    return tree[r];
//  }
//  tree[r] = make_tree(i,(i+j)/2,2*r+1) + make_tree(((i+j)/2)+1,j,2*r+2);
//  return tree[r];
// }
// void dfs(ll r){
//  vis[r] = 1;
//  FOR(i,adj[r].size(),0){
//    if(vis[adj[r][i]] == 0){
//      dfs(adj[r][i]);
//    }
//  }
// }{}
bool isOpen(char c)
{
    if(c == '(' || c == '{' || c == '[')
        return true;
    return false;
}

bool isBalanced(char popped, char now)
{
    if(popped == '(' && now ==')')
        return true;
    if(popped == '[' && now ==']')
        return true;
    if(popped == '{' && now =='}')
        return true;
    return false;
}
ll ch(string str){
        stack<char> s;

        ll no = 0;
        for(ll i = 0; i < str.length(); i++)
        {
            char now = str[i];
            if(isOpen(now))
                s.push(now);
            else
            {
                char popped;
                if(s.size() > 0)
                {
                    popped = s.top();
                    s.pop();
                }
                else
                {
                    no = 1;
                    break;
                }
                if(isBalanced(popped, now))
                    continue;
                else
                {
                    no = 1;
                    break;
                }
            }
        }
        if(s.size() != 0)
           { no = 1;}
        if(no)
            {
              return 0;
            }
        else
        {
            return 1;
        }

      }

string ch2(string s){
  stack<char>st;
  FOR(i,s.length(),0){
    if(st.size()>0){
      if(s[i]==')'&&st.top()=='('){
        st.pop();
      }
      else{
        st.push(s[i]);
      }
    }
    else{
      st.push(s[i]);
    }
  }
  // cout << s.size() << " ";
              string temp;
              while(!st.empty()){
                temp += st.top();
                st.pop();

              }
              FOR(i,temp.length(),0){
                if(temp[i]==')'){temp[i]='(';}
                else{temp[i]=')';}
              }

              return temp;
}
int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  ll n;
  cin >> n;
  vector<string>v;
  FOR(i,n,0){
    string s;
    cin >> s;
    v.push_back(s);
  }
  sort(v.begin(),v.end());
  ll c= 0;
  ll ans=0;
  map<string,ll>mp;
  FOR(i,n,0){

    if(ch(v[i])){
      c++;
    }
    else{
      string res = ch2(v[i]);
      // cout << res << " ";
      mp[res] = 1;
      if(mp[v[i]]){
        ans++;
      }
      else{
        mp[v[i]] = 1;
      }
    }
  }
  ans += c*c ;
  cout << ans;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define MOD 1000000007
#define N 2000100

#define scd(x) scanf("%d",&x);
#define prd(x) prllf("%d\n",x);
#define scl(x) scanf("%lld",&x);
#define prl(x) prllf("%lld",x);
#define forall(i,j) memset(i,j,sizeof(i));

#define FOR(i,k,e) for (ll i=e ; i<k ; i++)
#define RFOR(i,k,e) for (ll i=e ; i>=k ; i--)

typedef vector<ll> vi;
typedef vector<ll> vl;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vpi;
typedef vector< pll > vpl;

// ll tree[4*N];
// ll lazy[4*N];

// void update(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return ;
//    }
//  if(si>=i && sj<=j){
//    ll l = sj-si+1;
//    ll temp = tree[index];
//    tree[index] = l - tree[index];
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index+1] + 1;
//      lazy[2*index + 2] = lazy[2*index+2] + 1;

//    }
//    return;

//  }

//   update(i, j, si , (si + sj)/2 , 2*index + 1);
//   update( i, j, (si + sj)/2 +1 , sj , 2*index + 2);
//   tree[index] = tree[2*index+1] + tree[2*index+2];

// }
// ll query(ll i, ll j , ll si , ll sj , ll index){
//  if(lazy[index]!=0){
//    if(lazy[index]%2==1){
//      tree[index] = (sj-si+1) - tree[index];
//    }
//    if(si!=sj){
//      lazy[2*index + 1] = lazy[2*index + 1] +  lazy[index];
//      lazy[2*index + 2] = lazy[2*index + 2] +  lazy[index];
//    }
//    lazy[index] = 0;
//  }
//  if(j<si || i > sj){
//   return 0;
//    }
//  if(si>=i && sj<=j){
//    return tree[index];
//  }

//  return  query(i, j, si , (si + sj)/2 , 2*index + 1)
//      + query( i,  j, (si + sj)/2 +1 , sj , 2*index + 2);

// }
// ll make_tree(ll i,ll j,ll r){
//  if(i==j){
//    tree[r] = euler[i];
//    return tree[r];
//  }
//  tree[r] = make_tree(i,(i+j)/2,2*r+1) + make_tree(((i+j)/2)+1,j,2*r+2);
//  return tree[r];
// }
// void dfs(ll r){
//  vis[r] = 1;
//  FOR(i,adj[r].size(),0){
//    if(vis[adj[r][i]] == 0){
//      dfs(adj[r][i]);
//    }
//  }
// }{}
bool isOpen(char c)
{
    if(c == '(' || c == '{' || c == '[')
        return true;
    return false;
}

bool isBalanced(char popped, char now)
{
    if(popped == '(' && now ==')')
        return true;
    if(popped == '[' && now ==']')
        return true;
    if(popped == '{' && now =='}')
        return true;
    return false;
}
ll ch(string str){
        stack<char> s;

        ll no = 0;
        for(ll i = 0; i < str.length(); i++)
        {
            char now = str[i];
            if(isOpen(now))
                s.push(now);
            else
            {
                char popped;
                if(s.size() > 0)
                {
                    popped = s.top();
                    s.pop();
                }
                else
                {
                    no = 1;
                    break;
                }
                if(isBalanced(popped, now))
                    continue;
                else
                {
                    no = 1;
                    break;
                }
            }
        }
        if(s.size() != 0)
           { no = 1;}
        if(no)
            {
              return 0;
            }
        else
        {
            return 1;
        }

      }

string ch2(string s){
  stack<char>st;
  FOR(i,s.length(),0){
    if(st.size()>0){
      if(s[i]==')'&&st.top()=='('){
        st.pop();
      }
      else{
        st.push(s[i]);
      }
    }
    else{
      st.push(s[i]);
    }
  }
  // cout << s.size() << " ";
              string temp;
              while(!st.empty()){
                temp += st.top();
                st.pop();

              }
              FOR(i,temp.length(),0){
                if(temp[i]==')'){temp[i]='(';}
                else{temp[i]=')';}
              }

              return temp;
}
int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  ll n;
  cin >> n;
  vector<string>v;
  FOR(i,n,0){
    string s;
    cin >> s;
    v.push_back(s);
  }
  sort(v.begin(),v.end());
  ll c= 0;
  ll ans=0;
  map<string,ll>mp;
  FOR(i,n,0){

    if(ch(v[i])){
      c++;
    }
    else{
      string res = ch2(v[i]);
      // cout << res << " ";
      mp[res]++;
    }
  }
  FOR(i,n,0){
    if(mp[v[i]]){
      ans += mp[v[i]];
    }
  }
  ans = ans/2;
  ans += c*c ;
  cout << ans;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,ans=0;
    cin>>n;
    string s[n];
    for(i=0;i<n;i++)
        cin>>s[i];
    ll cnt,complete=0,flg,flg1;
    for(i=0;i<n;i++){
        cnt=0,flg=0,flg1=0;
        for(j=0;j<sz(s[i]);j++){
            if(s[i][j]=='('){
                if(cnt<0){
                    ll cnt1=0;
                    for(k=j;k<sz(s[i]);k++){
                        if(s[i][k]=='(')cnt1++;
                        else if(cnt1>0)cnt1--;
                        else break;
                    }
                    if(k==sz(s[i])&&cnt1==0)break;
                    else{
                        flg1=1;break;
                    }
                }
                cnt++;
            }
            else if(cnt>0)cnt--;
            else{
                flg=1;
                cnt--;
            }
        }
        if(flg==0&&cnt==0){
            //cout<<"i1 "<<i<<endl;
            complete++;continue;
        }
        else if(flg1==0){
            if(cnt>0)g[cnt]++;//cout<<"i2 "<<i<<" "<<cnt<<endl;
            else h[abs(cnt)]++;//cout<<"i3 "<<i<<" "<<cnt<<endl;
        }
    }
    ans+=complete*(complete-1)+complete;
    for(i=1;i<=300005;i++)
        ans+=h[i]*g[i];
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

int main()
{
    ll n,i,j,k;
    cin>>n;
    vector<string> v;
    string s;
    vector<ll> complete,left_close,right_open;
    for(i=0;i<n;i++){
        cin>>s;
        v.pb(s);
    }
    ll cnt;
    for(i=0;i<n;i++){
        cnt=0;
        for(j=0;j<sz(v[i]);j++){
            if(v[i][j]=='(')cnt++;
            else if(cnt>0)cnt--;
            else break;
        }
        if(j==sz(v[i])&&cnt==0)
            complete.pb(i);
    }
    for(i=0;i<n;i++){
        cnt=0;
        if(sz(v[i])==1){
            if(v[i][0]==')')
                left_close.pb(i);
            else right_open.pb(i);
            continue;
        }
        if(v[i][0]==')'){
            for(j=1;j<sz(v[i]);j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==sz(v[i])&&cnt==0){
                left_close.pb(i);
                continue;
            }
        }
        if(v[i][sz(v[i])-1]=='('){
            cnt=0;
            for(j=0;j<sz(v[i])-1;j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==sz(v[i])-1&&cnt==0)
                right_open.pb(i);
        }
    }
    ll ans=sz(complete)*(sz(complete)-1)+sz(complete);
    ans+=(sz(left_close))*(sz(right_open));
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,flg,ans=0,curr;
    cin>>n;
    string s[n];
    for(i=0;i<n;i++){
        cin>>s[i];
    }
    for(i=0;i<n;i++){
        curr=0,flg=0;
        for(auto j:s[i]){
            if(j=='(')curr++;
            else curr--;
            if(curr<0)flg=1;
        }
        if(flg==0)
            g[curr]++;//cout<<"c "<<curr<<endl;
        curr=0,flg=0;
        reverse(all(s[i]));
        for(j=0;j<sz(s[i]);j++){
            if(s[i][j]=='(')s[i][j]=')';
            else s[i][j]='(';
        }
        for(auto j:s[i]){
            if(j=='(')curr++;
            else curr--;
            if(curr<0)flg=1;
        }
        if(flg==0)
            h[curr]++;//cout<<"c "<<curr<<endl;
    }
    for(i=0;i<=300005;i++)
        ans+=g[i]*h[i];
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

int main()
{
    ll n,i,j,k,ans=0;
    cin>>n;
    vector<string> v;
    string s;
    vector<ll> complete,left_close,right_open;
    for(i=0;i<n;i++){
        cin>>s;
        v.pb(s);
    }
    ll cnt;
    for(i=0;i<n;i++){
        cnt=0;
        for(j=0;j<sz(v[i]);j++){
            if(v[i][j]=='(')cnt++;
            else if(cnt>0)cnt--;
            else break;
        }
        if(j==sz(v[i])&&cnt==0)
            complete.pb(i);
    }
    for(i=0;i<n;i++){
        cnt=0;
        if(sz(v[i])==1){
            if(v[i][0]==')')
                left_close.pb(i);
            else right_open.pb(i);
            continue;
        }
        if(v[i][0]==')'){
            for(j=1;j<sz(v[i]);j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==sz(v[i])&&cnt==0){
                left_close.pb(i);
                continue;
            }
        }
        if(v[i][sz(v[i])-1]=='('){
            cnt=0;
            for(j=0;j<sz(v[i])-1;j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==(sz(v[i])-1)&&cnt==0)
                right_open.pb(i);
        }
    }
    ll w;
    if(!complete.empty()){
        w=sz(complete);
        ans+=w*(w-1)+w;
    }
    if(left_close.empty()||right_open.empty())
        cout<<ans<<endl;
    else{
        w=sz(left_close)*sz(right_open);
        ans+=w;
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,ans=0;
    cin>>n;
    string s[n];
    for(i=0;i<n;i++)
        cin>>s[i];
    ll cnt,complete=0,flg,flg1;
    for(i=0;i<n;i++){
        cnt=0,flg=0,flg1=0;
        for(j=0;j<sz(s[i]);j++){
            if(s[i][j]=='('){
                if(cnt<0)flg1=1;
                cnt++;
            }
            else if(cnt>0)cnt--;
            else{
                flg=1;
                cnt--;
            }
        }
        if(flg==0&&cnt==0){
            //cout<<"i1 "<<i<<endl;
            complete++;continue;
        }
        else if(flg1==0){
            if(cnt>0)g[cnt]++;//cout<<"i2 "<<i<<" "<<cnt<<endl;
            else h[abs(cnt)]++;//cout<<"i3 "<<i<<" "<<cnt<<endl;
        }
    }
    ans+=complete*(complete-1)+complete;
    for(i=1;i<=300005;i++)
        ans+=h[i]*g[i];
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,ans=0;
    cin>>n;
    vector<string> v;
    string s;
    vector<ll> complete,left_close,right_open;
    for(i=0;i<n;i++){
        cin>>s;
        v.pb(s);
    }
    ll cnt;
    for(i=0;i<n;i++){
        cnt=0;
        for(j=0;j<sz(v[i]);j++){
            if(v[i][j]=='(')cnt++;
            else if(cnt>0)cnt--;
            else break;
        }
        if(j==sz(v[i])&&cnt==0)
            complete.pb(i);
    }
    //cout<<"yo "<<sz(complete)<<endl;
    for(i=0;i<n;i++){
        cnt=0;
        //cout<<"v "<<v[i]<<endl;
        if(sz(v[i])==1){
            if(v[i][0]==')')
                g[1]++;
            else h[1]++;
            continue;
        }
        if(v[i][0]==')'){
            k=0;
            while(v[i][j]==')')
                k++;
            //cout<<"e1 "<<i<<endl;
            for(j=k;j<sz(v[i]);j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            //cout<<"i,cnt "<<i<<" "<<cnt<<endl;
            if(j==sz(v[i])&&cnt==0){
                g[k]++,left_close.pb(i);
            }
        }
        if(v[i][sz(v[i])-1]=='('){
            cnt=0;
            k=sz(v[i])-1;
            while(v[i][k]=='(')
                k--;
            //cout<<"e2 "<<i<<endl;
            for(j=0;j<=k;j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==(k+1)&&cnt==0)
                h[sz(v[i])-1-k]++,right_open.pb(i);
        }
    }
    ll w;
    if(!complete.empty()){
        w=sz(complete);
        ans+=w*(w-1)+w;
    }
    for(i=1;i<=300000;i++){
        ans+=(h[i]*g[i]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,flg,ans=0,curr;
    cin>>n;
    string s[n];
    for(i=0;i<n;i++){
        cin>>s[i];
    }
    for(i=0;i<n;i++){
        curr=0,flg=0;
        for(auto j:s[i]){
            if(j=='(')curr++;
            else curr--;
            if(curr<0)flg=1;
        }
        if(flg==0)
            g[curr]++;//cout<<"c "<<curr<<endl;
        curr=0,flg=0;
        reverse(all(s[i]));
        for(j=0;j<sz(s[i]);j++){
            if(s[i][j]=='(')s[i][j]=')';
            else s[i][j]='(';
        }
        for(auto j:s[i]){
            if(j=='(')curr++;
            else curr--;
            if(curr<0)flg=1;
        }
        if(flg==0)
            h[curr]++;//cout<<"c "<<curr<<endl;
    }
    for(i=0;i<=300005;i++)
        ans+=g[i]*h[i];
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pii pair<ll,ll>
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pf(n) printf("%lld\n",n)
#define sf(n) scanf("%lld",&n)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define ms(n,i) memset(n,i,sizeof n)
#define sz(x) (int)x.size()
#define pi 3.141592653589793238
#define hell 1000000007

ll g[300005],h[300005];
int main()
{
    ll n,i,j,k,ans=0;
    cin>>n;
    vector<string> v;
    string s;
    vector<ll> complete,left_close,right_open;
    for(i=0;i<n;i++){
        cin>>s;
        v.pb(s);
    }
    ll cnt;
    for(i=0;i<n;i++){
        cnt=0;
        for(j=0;j<sz(v[i]);j++){
            if(v[i][j]=='(')cnt++;
            else if(cnt>0)cnt--;
            else break;
        }
        if(j==sz(v[i])&&cnt==0)
            complete.pb(i);
    }
    //cout<<"yo "<<sz(complete)<<endl;
    for(i=0;i<n;i++){
        cnt=0;
        //cout<<"v "<<v[i]<<endl;
        if(sz(v[i])==1){
            if(v[i][0]==')')
                g[1]++;
            else h[1]++;
            continue;
        }
        if(v[i][0]==')'){
            k=0;
            while(k<sz(v[i])&&v[i][j]==')')
                k++;
            //cout<<"e1 "<<i<<endl;
            for(j=k;j<sz(v[i]);j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            //cout<<"i,cnt "<<i<<" "<<cnt<<endl;
            if(j==sz(v[i])&&cnt==0){
                g[k]++,left_close.pb(i);
            }
        }
        if(v[i][sz(v[i])-1]=='('){
            cnt=0;
            k=sz(v[i])-1;
            while(k>=0&&v[i][k]=='(')
                k--;
            //cout<<"e2 "<<i<<endl;
            for(j=0;j<=k;j++){
                if(v[i][j]=='(')cnt++;
                else if(cnt>0)cnt--;
                else break;
            }
            if(j==(k+1)&&cnt==0)
                h[sz(v[i])-1-k]++,right_open.pb(i);
        }
    }
    ll w;
    if(!complete.empty()){
        w=sz(complete);
        ans+=w*(w-1)+w;
    }
    for(i=1;i<=300000;i++){
        ans+=(h[i]*g[i]);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

int ples[3*100000] = {0};
int mines[3*100000] = {0};

int main(int argc, char ** argv)
{
	int n; cin>>n;
	string s[n];
	for (int j = 0; j < n; ++j)
	{
		string u; cin>>u;
		s[j] = u;
		int final=0;
		int min=0;
		for (int i = 0; i < u.size(); ++i)
		{
			if(u[i]=='(')final++;
			else final--;
			if(final<min)min=final;
		}
		if(min>=0 && final>=0)ples[final]++;
		if(min<=0 && final<=0)mines[(-1)*final]++;
	}
	int d=0;
	for (int i = 0; i < 3.100000; ++i)
	{
		d+=ples[i]*mines[i];
	}
	cout<<d<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

ll ples[3*100000] = {0};
ll mines[3*100000] = {0};

int main(int argc, char ** argv)
{
	ll n; cin>>n;
	string s[n];
	for (int j = 0; j < n; ++j)
	{
		string u; cin>>u;
		s[j] = u;
		ll final=0;
		ll min=0;
		for (int i = 0; i < u.size(); ++i)
		{
			if(u[i]=='(')final++;
			else final--;
			if(final<min)min=final;
		}
		if(min>=0 && final>=0)ples[final]++;
		if(min<=0 && final<=0)mines[(-1)*final]++;
	}
	ll d=0;
	for (int i = 0; i < 3.100000; ++i)
	{
		d+=ples[i]*mines[i];
	}
	cout<<d<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

string mirror_image(string s){
	string p = "";
	int size = s.size();
	for (int i = 0; i < size; ++i)
	{
		if(s[size-i-1] == '(')p+=')';
		else p+='(';
	}
	return p;
}

int balance(string s){
	int ans=0;
	for (int i = 0; i < s.size(); ++i)
	{
		if(s[i]=='(')ans++;
		else ans--;
		if(ans<0)return -1;
	}
	return ans;
}

int main(int argc, char ** argv)
{
	int n; cin>>n;
	string s[n];
	rep(i,0,n)cin>>s[i];
	ll ans=0;
	ll belance[300000]={0};
	for (int i = 0; i < n; ++i)
	{
		int b = balance(s[i]);
		if(b>=0)belance[b]++;
	}
	for (int i = 0; i < n; ++i)
	{
		int q = balance(mirror_image(s[i]));
		if(q != -1)ans += belance[q];
	}
	cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

ll ples[3*100000] = {0};
ll mines[3*100000] = {0};

int main(int argc, char ** argv)
{
	ll n; cin>>n;
	string s[n];
	for (int j = 0; j < n; ++j)
	{
		string u; cin>>u;
		s[j] = u;
		ll final=0;
		ll min=0;
		for (int i = 0; i < u.size(); ++i)
		{
			if(u[i]=='(')final++;
			else final--;
			if(final<min)min=final;
		}
		if(min>=0 && final>=0)ples[final]++;
		if(min<=0 && final<=0)mines[(-1)*final]++;
	}
	ll d=0;
	if(ples[0]!=1)d=ples[0]*ples[0];
	for (int i = 1; i < 3.100000; ++i)
	{
		d+=ples[i]*mines[i];
	}
	cout<<d<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

string mirror_image(string s){
	string p = "";
	int size = s.size();
	for (int i = 0; i < size; ++i)
	{
		if(s[size-i-1] == '(')p+=')';
		else p+='(';
	}
	return p;
}

int balance(string s){
	int ans=0;
	for (int i = 0; i < s.size(); ++i)
	{
		if(s[i]=='(')ans++;
		else ans--;
		if(ans<0)return -1;
	}
	return ans;
}

int main(int argc, char ** argv)
{
	int n; cin>>n;
	string s[n];
	rep(i,0,n)cin>>s[i];
	int ans=0;
	int belance[300000]={0};
	for (int i = 0; i < n; ++i)
	{
		int b = balance(s[i]);
		if(b>=0)belance[b]++;
	}
	for (int i = 0; i < n; ++i)
	{
		int q = balance(mirror_image(s[i]));
		if(q != -1)ans += belance[q];
	}
	cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

int ples[3*100000] = {0};
int mines[3*100000] = {0};

int main(int argc, char ** argv)
{
	int n; cin>>n;
	string s[n];
	for (int j = 0; j < n; ++j)
	{
		string u; cin>>u;
		s[j] = u;
		int final=0;
		int min=0;
		for (int i = 0; i < u.size(); ++i)
		{
			if(u[i]=='(')final++;
			else final--;
		}
		if(final<min)min=final;
		if(min>=0 && final>=0)ples[final]++;
		if(min<=0 && final<=0)mines[final]++;
	}
	int d=0;
	for (int i = 0; i < 3.100000; ++i)
	{
		int j=(-1)*i;
		d+=ples[i]*mines[j];
	}
	cout<<d<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define F first
#define s second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

string mirror_image(string s){
	string p = "";
	int size = size;
	for (int i = 0; i < size; ++i)
	{
		if(s[size-i-1] == '(')p+=')';
		else p+='(';
	}
	return p;
}

int balance(string s){
	int ans=0;
	for (int i = 0; i < s.size(); ++i)
	{
		if(s[i]=='(')ans++;
		else ans--;
		if(ans<0)return -1;
	}
	return ans;
}

int main(int argc, char ** argv)
{
	int n; cin>>n;
	string s[n];
	rep(i,0,n)cin>>s[i];
	int ans=0;
	int belance[300000]={0};
	for (int i = 0; i < n; ++i)
	{
		int b = balance(s[i]);
		if(b>=0)belance[b]++;
	}
	for (int i = 0; i < n; ++i)
	{
		int q = balance(mirror_image(s[i]));
		if(q != -1)ans += belance[q];
	}
	cout<<ans<<endl;
    return 0;
}


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n,"SEX");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {

        lli res=0;
        for(auto j:str[i])
        {
            if(j=='(')
            res++;

            else
            res--;
        }

        if(res==0)
        {
         ans++;
         ans=ans+(mpp[0]*2);
         mpp[res]++;
        }
        else
        {
            ans=ans+mpp[res*-1];
            mpp[res]++;
        }
    }

    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n,"SEX");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {

        lli res=0;
        for(auto j:str[i])
        {
            if(j=='(')
            res++;

            else
            res--;
        }

        if(res==0)
        {
                for(lli j=0;i<(lli)str[i].size();j++)
                {
                    if(j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
                    {
                        str[i].erase(j,1);
                        str[i].erase(j,1);
                        j--;
                    }

                }

                if((lli)str[i].size()==0)
                {
                    ans++;

                    ans=ans+2*mpp[0];

                    mpp[0]++;
                }
        }
        else
        {
                ans=ans+mpp[(res)*-1];
                mpp[res]++;
        }
    }
    // print(mpp[0])
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n,pos=0,neg=0,zero=0;
    cin>>n;
    vector<string> str(n,"MIA_MELONA");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {
        for(lli j=0;j<(lli)str[i].size();)
        {
            if(j>=0&&j<(lli)str[i].size()&&j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
            {
                str[i].erase(j,1);
                str[i].erase(j,1);
                j--;
            }

            else
            {
                j++;
            }
        }
        lli sz=(lli)str[i].size();
        if(sz==0)
        {

                    ans++;
                    ans=ans+2*mpp[sz];
                    mpp[sz]++;
        }
        else
        {
              if(str[i][0]==str[i][sz-1])
              {

                if(str[i][0]=='(')
                {
                    ans=ans+neg;
                    pos++;
                }
                else
                {
                    ans=ans+pos;
                    neg++;
                }
              }
        }
    }
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n,"SEX");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {
        lli res=0;
        for(auto j:str[i])
        {
            if(j=='(')
            res++;

            else
            res--;
        }
        for(lli j=0;j<(lli)str[i].size();)
        {
            if(j>=0&&j<(lli)str[i].size()&&j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
            {
                str[i].erase(j,1);
                str[i].erase(j,1);
                j--;
            }

           else
           j++;
        }

        if(res==0)
        {

                if((lli)str[i].size()==0)
                {
                    ans++;
                    ans=ans+2*mpp[0];
                    mpp[0]++;
                }
        }
        else
        {
                ans=ans+mpp[(res)*-1];
                mpp[res]++;
        }
    }
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n,"SEX");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {
        lli res=0;
        for(auto j:str[i])
        {
            if(j=='(')
            res++;

            else
            res--;
        }
        for(lli j=0;j<(lli)str[i].size();)
        {
            if(j>=0&&j<(lli)str[i].size()&&j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
            {
                str[i].erase(j,1);
                str[i].erase(j,1);
                j--;
            }

           else
           j++;
        }

        if(res==0)
        {

                if((lli)str[i].size()==0)
                {
                    ans++;
                    ans=ans+2*mpp[0];
                    mpp[0]++;
                }
        }
        else
        {
                res=(lli)str[i].size();
                // print(res)

                if(str[i][0]=='(')
                res=res*-1;

                ans=ans+mpp[(res)*-1];
                mpp[res]++;
        }
    }
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n,"SEX");

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {
        lli res=0;
        for(auto j:str[i])
        {
            if(j=='(')
            res++;

            else
            res--;
        }
        for(lli j=0;i<(lli)str[i].size();)
        {
            if(j>=0&&j<(lli)str[i].size()&&j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
            {
                str[i].erase(j,1);
                str[i].erase(j,1);
                j--;
            }

           else
           j++;
        }

        if(res==0)
        {

                if((lli)str[i].size()==0)
                {
                    ans++;
                    ans=ans+2*mpp[0];
                    mpp[0]++;
                }
        }
        else
        {
                ans=ans+mpp[(res)*-1];
                mpp[res]++;
        }
    }
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 1000001
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"YES\n";
#define N cout<<"NO\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
map<lli,lli> mpp;
int main()
{
    fast
    lli n;
    cin>>n;
    vector<string> str(n);

    lli ans=0;

    for(lli i=0;i<n;i++)
    {
        cin>>str[i];
    }

    for(lli i=0;i<n;i++)
    {
        for(lli j=0;j<(lli)str[i].size();)
        {
            if(j>=0&&j<(lli)str[i].size()&&j+1<(lli)str[i].size()&&str[i][j]=='('&&str[i][j+1]==')')
            {
                str[i].erase(j,1);
                str[i].erase(j,1);
                j--;
            }

            else
            {
                j++;
            }
        }
        lli sz=(lli)str[i].size();

        if(sz==0)
        {

                    ans++;
                    ans=ans+2*mpp[sz];
                    mpp[sz]++;
        }
        else
        {
               lli  res=0;
               for(auto j:str[i])
               {
                   if(j=='(') res++;
                   else res--;
               }
              if(res&&str[i][0]==str[i][sz-1])
              {

                if(str[i][0]=='(')
                {
                    ans=ans+mpp[-1*res];
                }
                else
                {
                    ans=ans+mpp[-1*res];

                }

                mpp[res]++;
              }
        }
    }
    cout<<ans;

}
//a+b=a?b+2?(a&b)
//__builtin_popcount(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
#include <string>

using namespace std;

#define loop(i,a,n) for (int i=a;i<n;i++)
#define rloop(i,a,n) for (int i=n-1;i>=a;i--)

#define ll long long

const ll MOD = 1000000007;

ll gcd(ll a, ll b)
{
    if (a == 0)
    return b;

    if (b == 0)
    return a;

    return gcd(b%a, a);
}

ll pow(ll a, ll b)
{
    if (b == 0)
    return 1;

    if (b == 1)
    return a;

    ll half = pow(a, b / 2);

    ll ans = (half * half);

    if (b % 2)
    {
        ans = (ans * a);
    }

    return ans;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
    // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
        res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
    return power(n, p-2, p);
}

int main()
{
    ll n;
    cin >> n;

    ll x;

    map<ll, multiset<ll> > M;

    vector< pair<ll, ll> > V(n);

    loop(i, 0, n)
    {
        cin >> V[i].first;
    }
    loop(i, 0, n)
    {
        cin >> V[i].second;
    }

    loop(i, 0, n)
    {
        M[V[i].first].insert(V[i].second);
    }

    ll ans = 0;

    for(auto it = M.begin(); it != M.end(); ++it)
    {
        multiset<ll> &S = it->second;

        ll len = S.size();

//        cout << it->first << " :: " << len << endl;

        while(S.size() > 1)
        {
            ll top_elem = *(S.begin());

            ans += ((S.size() - 1) * top_elem);

//            cout << (it->first + (S.size() - 1)) << " ::: " << M[it->first + (S.size() - 1)].size() << endl;

            M[it->first + (S.size() - 1)].insert(top_elem);

            S.erase(S.lower_bound(top_elem));
        }
    }

    cout << ans << endl;

    return 0;
}


// Ab kuch kar sprkgoyal, expert banna hai ki nhi tujhe... :/
// Suicide chhod kar

// amit_dwivedi bhaiya ka solution dekh kar samajh aaya

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define ff first
#define ss second
#define endl '\n'
#define all(v) v.begin(), v.end()
#define corona ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{cout << *it << " = " << a << endl;    err(++it, args...);}
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    corona
    int n;  cin>>n;
    int arr[n], brr[n];
    rep(i,0,n)
        cin>>arr[i];
    rep(i,0,n)
        cin>>brr[i];
    map<ll, vector<ll>> m;
    rep(i,0,n) {
        m[arr[i]].push_back(brr[i]);
    }
    multiset<ll, greater<ll>> s;
    ll temp = 0, ans = 0;
    auto it = m.begin();
    while(it != m.end()) {
        for(ll x: it->ss) {
            temp += x;
            s.insert(x);
        }
        ll last = *s.begin();
        temp -= last;
        ans += temp;
        s.erase(s.begin());
        if(s.size() != 0)
            m[it->ff + 1];        // dummy vector
        it ++;
    }
    cout<<ans<<endl;
    return 0;
}


// Ab kuch kar sprkgoyal, expert banna hai ki nhi tujhe... :/
// Suicide chhod kar

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define ff first
#define ss second
#define endl '\n'
#define all(v) v.begin(), v.end()
#define corona ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{cout << *it << " = " << a << endl;    err(++it, args...);}
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    corona
    int n;  cin>>n;
    int arr[n], brr[n];
    rep(i,0,n)  cin>>arr[i];
    rep(i,0,n)  cin>>brr[i];
    multiset<pair<ll, ll>> s;
    map<ll, ll> cnt;
    rep(i,0,n) {
        s.insert({arr[i], brr[i]});
        cnt[arr[i]] ++;
    }
    ll ans = 0;
    while(s.size() > 0) {
        ll p = s.begin()->ff;
        ll t = s.begin()->ss;
        s.erase(s.begin());
        if(s.size() == 0)
            break;
        if(s.begin()->ff != p)
            continue;
        ans += (cnt[p]-1)*t;
        cnt[p]--;
        cnt[p+cnt[p]]++;
        s.insert({p+cnt[p], t});
    }
    cout<<ans<<endl;
    return 0;
}


// Ab kuch kar sprkgoyal, expert banna hai ki nhi tujhe... :/
// Suicide chhod kar

// amit_dwivedi bhaiya ka solution dekh kar samajh aaya

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define ff first
#define ss second
#define endl '\n'
#define all(v) v.begin(), v.end()
#define corona ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{cout << *it << " = " << a << endl;    err(++it, args...);}
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    corona
    int n;  cin>>n;
    int arr[n], brr[n];
    rep(i,0,n)
        cin>>arr[i];
    rep(i,0,n)
        cin>>brr[i];
    map<ll, vector<ll>> m;
    rep(i,0,n) {
        m[arr[i]].push_back(brr[i]);
    }
    multiset<ll> s;
    ll temp = 0, ans = 0;
    auto it = m.begin();
    while(it != m.end()) {
        for(ll x: it->ss) {
            temp += x;
            s.insert(x);
        }
        ll last = *prev(s.end());
        temp -= last;
        ans += temp;
        s.erase(last);
        if(s.size() != 0)
            m[it->ff+1];
        it++;
    }
    cout<<ans<<endl;
    return 0;
}


// Ab kuch kar sprkgoyal, expert banna hai ki nhi tujhe... :/
// Suicide chhod kar

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define ff first
#define ss second
#define endl '\n'
#define all(v) v.begin(), v.end()
#define corona ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{cout << *it << " = " << a << endl;    err(++it, args...);}
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    corona
    int n;  cin>>n;
    int arr[n], brr[n];
    rep(i,0,n)  cin>>arr[i];
    rep(i,0,n)  cin>>brr[i];
    map<int, multiset<int>> m;
    rep(i,0,n)
        m[arr[i]].insert(brr[i]);
    ll ans = 0;
    for(auto &x: m) {
        if(x.ss.size() > 1) {
            while(x.ss.size() != 1) {
                int p = *x.ss.begin();
                x.ss.erase(p);
                ans += p * x.ss.size();
                m[x.ff + x.ss.size()].insert(p);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


// Aim to feel like home in Specialist

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define tachyon ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL)
#define endl '\n'
typedef long long ll;

int main() {
    tachyon;
    int n;  cin>>n;
    ll arr[n], tim[n];
    rep(i,0,n)  cin>>arr[i];
    rep(i,0,n)  cin>>tim[i];
    multiset<pair<ll, ll> > s;
    map<ll, int> m;
    rep(i,0,n) {
        m[arr[i]]++;
        s.insert(make_pair(arr[i], tim[i]));
    }
    map<ll, int>::iterator it = m.begin();
    ll ans = 0;
    for(; it != m.end(); it++) {
        if(it->second == 1) {
            s.erase(s.begin());
            continue;
        }
        // cout<<it->first<<' ';
        // int t = 4;
        while(it->second != 1) {
            m[it->first]--;
            // cout<<it->first<<' '<<it->second<<' ';
            s.insert(make_pair((it->first)+(it->second), s.begin()->second));
            m[(it->first)+(it->second)]++;
            ans += s.begin()->second * (it->second);
            s.erase(s.begin());
            // t--;
            // if(t == 0)  break;
        }
        s.erase(s.begin());
    }
    cout<<ans<<endl;
    return 0;
}


// Ab kuch kar sprkgoyal, expert banna hai ki nhi tujhe... :/
// Suicide chhod kar

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define ff first
#define ss second
#define endl '\n'
#define all(v) v.begin(), v.end()
#define corona ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{cout << *it << " = " << a << endl;    err(++it, args...);}
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    corona
    int n;  cin>>n;
    int arr[n], brr[n];
    rep(i,0,n)  cin>>arr[i];
    rep(i,0,n)  cin>>brr[i];
    map<int, set<int>> m;
    rep(i,0,n)
        m[arr[i]].insert(brr[i]);
    ll ans = 0;
    for(auto &x: m) {
        if(x.ss.size() > 1) {
            while(x.ss.size() != 1) {
                int p = *x.ss.begin();
                x.ss.erase(p);
                ans += p * x.ss.size();
                m[x.ff + x.ss.size()].insert(p);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


// Aim to feel like home in Specialist

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

#define rep(i,st,en) for(__typeof(en) i=(st)-(st>en); i!=(en)-(st>en); i+=1-2*(st>en))
#define tachyon ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL)
#define endl '\n'
typedef long long ll;

int main() {
    tachyon;
    int n;  cin>>n;
    ll arr[n], tim[n];
    rep(i,0,n)  cin>>arr[i];
    rep(i,0,n)  cin>>tim[i];
    multiset<pair<ll, ll> > s;
    map<ll, int> m;
    rep(i,0,n) {
        m[arr[i]]++;
        s.insert(make_pair(arr[i], tim[i]));
    }
    map<ll, int>::iterator it = m.begin();
    ll ans = 0;
    for(; it != m.end(); it++) {
        if(it->second == 1) {
            s.erase(s.begin());
            continue;
        }
        while(it->second != 1) {
            m[it->first]--;
            s.insert(make_pair(s.begin()->first+1, s.begin()->second));
            // cout<<s.begin()->first<<' '<<s.begin()->second<<endl;
            m[s.begin()->first+1]++;
            ans += s.begin() -> second;
            s.erase(s.begin());
        }
        s.erase(s.begin());
    }
    cout<<ans<<endl;
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
pair<int , int> a[N];
pair<ll , pair<int , int>> seg[4 * N];
ll lazy[4 * N];
ll ans;
set<pair<ll , ll>> st[N];
void prop(int node , int s , int e)
{
	if(lazy[node])
	{
		seg[node].first += a[-seg[node].second.second].second * lazy[node];
		if(s != e)
		{
			lazy[node << 1] += lazy[node];
			lazy[node << 1 | 1] += lazy[node];
		}
		lazy[node] = 0;
	}
}
void add(int node , int s , int e , int x , int i , int j)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {x , {-i , -j}};
		return;
	}
	int mid = s + e >> 1;
	if(mid >= i)
		add(node << 1 , s , mid , x , i , j);
	else
		add(node << 1 | 1 , mid + 1 , e , x , i , j);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
void update(int node , int s , int e , int l , int r)
{
	prop(node , s , e);
	if(s > r || e < l)
		return;
	if(l <= s && e <= r)
	{
		lazy[node]++;
		prop(node , s , e);
		return;
	}
	int mid = s + e >> 1;
	update(node << 1 , s , mid , l , r);
	update(node << 1 | 1 , mid + 1 , e , l , r);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
pair<ll , pair<int , int>> get(int node , int s , int e , int l , int r)
{
	prop(node , s , e);
	if(s > r || e < l)
		return {0 , {0 , 0}};
	if(l <= s && e <= r)
		return seg[node];
	int mid = s + e >> 1;
	return max(get(node << 1 , s , mid , l , r) , get(node << 1 | 1 , mid + 1 , e , l , r));
}
void remove(int node , int s , int e , int i)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {0 , {0 , 0}};
		return;
	}
	int mid = s + e >> 1;
	if(i <= mid)
		remove(node << 1 , s , mid , i);
	else
		remove(node << 1 | 1 , mid + 1 , e , i);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		st[a[i].first].insert({a[i].second , i});
		if(a[i].first == a[i - 1].first)
		{
			add(1 , 0 , n - 1 , st[a[i].first].begin()->first , ind++ , st[a[i].first].begin()->second);
			st[a[i].first].erase(st[a[i].first].begin());
		}
		else
		{
			if(a[i].first == a[i - 1].first + 1)
				update(1 , 0 , n - 1 , 0 , ind - 1);
			else
			{
				auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
				ans += x.first;
				remove(1 , 0 , n - 1 , -x.second.first);
			}
		}
	}
	auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
	int y = x.first;
	while(y > 0)
	{
		ans += x.first;
		remove(1 , 0 , n - 1 , -x.second.first);
		auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
		y = x.first;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
ll ans;
pair<int , int> a[N];
set<pair<int , int> , greater<pair<int , int>>>st;
set<int> b[N];
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		b[a[i].first].insert(a[i].second);
		if(a[i].first == a[i - 1].first)
		{
			st.insert({*b[a[i].first].begin() , -i});
			b[a[i].first].erase(b[a[i].first].begin());
		}
		else
		{
			if(st.size() && a[i].first > a[i - 1].first + 1)
			{
				auto x = st.begin();
				st.erase(x);
				int y = x->first , z = -x->second;
				ans += 1ll * y * (i - z);
			}
		}
	}
	int cnt = n;
	while(st.size())
	{
		auto x = st.begin();
		st.erase(x);
		int y = x->first , z = -x->second;
		ans += 1ll * y * (cnt - z);
		cnt++;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
ll ans;
pair<int , int> a[N];
set<pair<int , int> , greater<pair<int , int>>>st;
map<int , set<int>>b;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		b[a[i].first].insert(a[i].second);
		if(a[i].first == a[i - 1].first)
		{
			st.insert({*b[a[i].first].begin() , -a[i].first});
			b[a[i].first].erase(b[a[i].first].begin());
		}
		else
		{
			if(a[i].first > a[i - 1].first + 1)
			{
				ll j = a[i - 1].first + 1;
				while(j < a[i].first && st.size())
				{
					auto x = st.begin();
					st.erase(x);
					int y = x->first , z = -x->second;
					ans += 1ll * y * (j - z);
				}
			}
		}
	}
	int cnt = a[n - 1].first + 1;
	while(st.size())
	{
		auto x = st.begin();
		st.erase(x);
		int y = x->first , z = -x->second;
		ans += 1ll * y * (cnt - z);
		cnt++;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n;
pair<int , int> a[N];
multiset<int> t;
ll sum , ans;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 0;i < n;i++)
	{
		sum += 1ll * a[i].second;
		t.insert(a[i].second);
		int j = a[i].first;
		int k = a[i + 1].first;
		if(i == n - 1)
			k = 1e10;
		while(t.size() && j < k)
		{
			auto it = t.end();
			--it;
			sum -= *it;
			ans += sum;
			t.erase(it);
			j++;
		}
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
pair<int , int> a[N];
pair<ll , pair<int , int>> seg[4 * N];
ll lazy[4 * N];
ll ans;
map<ll , set<pair<ll , ll>>> st;
void prop(int node , int s , int e)
{
	if(lazy[node])
	{
		seg[node].first += a[-seg[node].second.second].second * lazy[node];
		if(s != e)
		{
			lazy[node << 1] += lazy[node];
			lazy[node << 1 | 1] += lazy[node];
		}
		lazy[node] = 0;
	}
}
void add(int node , int s , int e , int x , int i , int j)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {x , {-i , -j}};
		return;
	}
	int mid = s + e >> 1;
	if(mid >= i)
		add(node << 1 , s , mid , x , i , j);
	else
		add(node << 1 | 1 , mid + 1 , e , x , i , j);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
void update(int node , int s , int e , int l , int r)
{
	prop(node , s , e);
	if(s > r || e < l)
		return;
	if(l <= s && e <= r)
	{
		lazy[node]++;
		prop(node , s , e);
		return;
	}
	int mid = s + e >> 1;
	update(node << 1 , s , mid , l , r);
	update(node << 1 | 1 , mid + 1 , e , l , r);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
pair<ll , pair<int , int>> get(int node , int s , int e , int l , int r)
    		{
	prop(node , s , e);
	if(s > r || e < l)
		return {0 , {0 , 0}};
	if(l <= s && e <= r)
		return seg[node];
	int mid = s + e >> 1;
	return max(get(node << 1 , s , mid , l , r) , get(node << 1 | 1 , mid + 1 , e , l , r));
    		}
void remove(int node , int s , int e , int i)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {0 , {0 , 0}};
		return;
	}
	int mid = s + e >> 1;
	if(i <= mid)
		remove(node << 1 , s , mid , i);
	else
		remove(node << 1 | 1 , mid + 1 , e , i);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		st[a[i].first].insert({a[i].second , i});
		if(a[i].first == a[i - 1].first)
		{
			add(1 , 0 , N - 1 , st[a[i].first].begin()->first , ind++ , st[a[i].first].begin()->second);
			st[a[i].first].erase(st[a[i].first].begin());
		}
		else
		{
			if(a[i].first == a[i - 1].first + 1)
				update(1 , 0 , N - 1 , 0 , ind - 1);
			else
			{
				auto x = get(1 , 0 , N - 1 , 0 , ind - 1);
				ans += x.first;
				remove(1 , 0 , N - 1 , -x.second.first);
			}
		}
	}
	auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
	int y = x.first;
	while(y > 0)
	{
		ans += x.first;
		remove(1 , 0 , n - 1 , -x.second.first);
		auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
		y = x.first;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
pair<int , int> a[N];
pair<ll , pair<int , int>> seg[4 * N];
ll lazy[4 * N];
ll ans;
set<pair<ll , ll>> st[N];
void prop(int node , int s , int e)
{
	if(lazy[node])
	{
		seg[node].first += a[-seg[node].second.second].second * lazy[node];
		if(s != e)
		{
			lazy[node << 1] += lazy[node];
			lazy[node << 1 | 1] += lazy[node];
		}
		lazy[node] = 0;
	}
}
void add(int node , int s , int e , int x , int i , int j)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {x , {-i , -j}};
		return;
	}
	int mid = s + e >> 1;
	if(mid >= i)
		add(node << 1 , s , mid , x , i , j);
	else
		add(node << 1 | 1 , mid + 1 , e , x , i , j);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
void update(int node , int s , int e , int l , int r)
{
	prop(node , s , e);
	if(s > r || e < l)
		return;
	if(l <= s && e <= r)
	{
		lazy[node]++;
		prop(node , s , e);
		return;
	}
	int mid = s + e >> 1;
	update(node << 1 , s , mid , l , r);
	update(node << 1 | 1 , mid + 1 , e , l , r);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
pair<ll , pair<int , int>> get(int node , int s , int e , int l , int r)
{
	prop(node , s , e);
	if(s > r || e < l)
		return {0 , {0 , 0}};
	if(l <= s && e <= r)
		return seg[node];
	int mid = s + e >> 1;
	return max(get(node << 1 , s , mid , l , r) , get(node << 1 | 1 , mid + 1 , e , l , r));
}
void remove(int node , int s , int e , int i)
{
	prop(node , s , e);
	if(s == e)
	{
		seg[node] = {0 , {0 , 0}};
		return;
	}
	int mid = s + e >> 1;
	if(i <= mid)
		remove(node << 1 , s , mid , i);
	else
		remove(node << 1 | 1 , mid + 1 , e , i);
	seg[node] = max(seg[node << 1] , seg[node << 1 | 1]);
}
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		st[a[i].first].insert({a[i].second , i});
		if(a[i].first == a[i - 1].first)
		{
			add(1 , 0 , N - 1 , st[a[i].first].begin()->first , ind++ , st[a[i].first].begin()->second);
			st[a[i].first].erase(st[a[i].first].begin());
		}
		else
		{
			if(a[i].first == a[i - 1].first + 1)
				update(1 , 0 , N - 1 , 0 , ind - 1);
			else
			{
				auto x = get(1 , 0 , N - 1 , 0 , ind - 1);
				ans += x.first;
				remove(1 , 0 , N - 1 , -x.second.first);
			}
		}
	}
	auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
	int y = x.first;
	while(y > 0)
	{
		ans += x.first;
		remove(1 , 0 , n - 1 , -x.second.first);
		auto x = get(1 , 0 , n - 1 , 0 , ind - 1);
		y = x.first;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
ll ans;
pair<int , int> a[N];
set<pair<int , int> , greater<pair<int , int>>>st;
map<int , set<int>>b;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		b[a[i].first].insert(a[i].second);
		if(a[i].first == a[i - 1].first)
		{
			st.insert({*b[a[i].first].begin() , -a[i].first});
			b[a[i].first].erase(b[a[i].first].begin());
		}
		else
		{
			ll j = a[i - 1].first + 1;
			while(j < a[i].first && st.size())
			{
				auto x = st.begin();
				st.erase(x);
				int y = x->first , z = -x->second;
				ans += 1ll * y * (j - z);
				j++;
			}
		}
	}
	int cnt = a[n - 1].first + 1;
	while(st.size())
	{
		auto x = st.begin();
		st.erase(x);
		int y = x->first , z = -x->second;
		ans += 1ll * y * (cnt - z);
		cnt++;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n;
pair<int , int> a[N];
multiset<int> t;
ll sum , ans;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 0;i < n;i++)
	{
		sum += 1ll * a[i].second;
		t.insert(a[i].second);
		int j = a[i].first;
		int k = a[i + 1].first;
		if(i == n - 1)
			k = 1e9;
		while(t.size() && j < k)
		{
			auto it = t.end();
			--it;
			sum -= *it;
			ans += sum;
			t.erase(it);
			j++;
		}
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
ll ans;
pair<int , int> a[N];
set<pair<int , int> , greater<pair<int , int>>>st;
map<int , set<int>>b;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	for(int i = 1;i < n;i++)
	{
		b[a[i].first].insert(a[i].second);
		if(a[i].first == a[i - 1].first)
		{
			st.insert({*b[a[i].first].begin() , -i});
			b[a[i].first].erase(b[a[i].first].begin());
		}
		else
		{
			if(st.size() && a[i].first > a[i - 1].first + 1)
			{
				auto x = st.begin();
				st.erase(x);
				int y = x->first , z = -x->second;
				ans += 1ll * y * (i - z);
			}
		}
	}
	int cnt = n;
	while(st.size())
	{
		auto x = st.begin();
		st.erase(x);
		int y = x->first , z = -x->second;
		ans += 1ll * y * (cnt - z);
		cnt++;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define abdelrahman010 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n , ind;
ll ans;
pair<int , int> a[N];
set<pair<int , int> , greater<pair<int , int>>>st;
map<int , set<int>>b;
int main()
{
	abdelrahman010
	cin >> 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 , a + n);
	b[a[0].first].insert(a[0].second);
	for(int i = 1;i < n;i++)
	{
		b[a[i].first].insert(a[i].second);
		if(a[i].first == a[i - 1].first)
		{
			st.insert({*b[a[i].first].begin() , -a[i].first});
			b[a[i].first].erase(b[a[i].first].begin());
		}
		else
		{
			ll j = a[i - 1].first + 1;
			while(j < a[i].first && st.size())
			{
				auto x = st.begin();
				st.erase(x);
				int y = x->first , z = -x->second;
				ans += 1ll * y * (j - z);
				j++;
			}
		}
	}
	int cnt = a[n - 1].first + 1;
	while(st.size())
	{
		auto x = st.begin();
		st.erase(x);
		int y = x->first , z = -x->second;
		ans += 1ll * y * (cnt - z);
		cnt++;
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

int n;
int a[200005];
int b[200005];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	for (auto &i : st) {
		sort(i.second.begin(), i.second.end());
		i.second.pop_back();
		int nxt = i.first + 1;
		while (i.second.size()) {
			ans += 1LL * i.second.back() * (nxt - i.first);
			st[nxt].push_back(i.second.back());
			i.second.pop_back();
			nxt++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd > b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j,i.first });
		}
		have.erase(--have.end());
		int pos = i.first + 1;
		while (!st.count(pos) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd > b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j, i.first });
		}
		int pos = i.first;
		while ((!st.count(pos) || pos == i.first) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
			pos++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

int n;
int a[200005];
int b[200005];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 200000;
	set <int> used;
	unordered_map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
		//a[i] = i / 19999;
		used.insert(a[i]);
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
		//b[i] = i + 1;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	for (auto &i : used) {
		sort(st[i].begin(), st[i].end());
		st[i].pop_back();
		int nxt = i + 1;
		while (st[i].size()) {
			ans += 1LL * st[i].back() * (nxt - i);
			st[nxt].push_back(st[i].back());
			st[i].pop_back();
			nxt++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd > b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	multiset <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j, i.first });
		}
		int pos = i.first;
		while ((!st.count(pos) || pos == i.first) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
			pos++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

int n;
int a[200005];
int b[200005];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 200000;
	unordered_map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
		//a[i] = i / 19999;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
		//b[i] = i + 1;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	for (auto &i : st) {
		sort(i.second.begin(), i.second.end());
		i.second.pop_back();
		int nxt = i.first + 1;
		while (i.second.size()) {
			ans += 1LL * i.second.back() * (nxt - i.first);
			st[nxt].push_back(i.second.back());
			i.second.pop_back();
			nxt++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd < b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j, i.first });
		}
		int pos = i.first;
		while ((!st.count(pos) || pos == i.first) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
			pos++;
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd > b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j,i.first });
		}
		ans += 1LL * (--have.end())->frst * (i.first - (--have.end())->scnd);
		have.erase(--have.end());
		int pos = i.first + 1;
		while (!st.count(pos) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

int n;
int a[200005];
int b[200005];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pair <int, int>> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j,i.first });
		}
		have.erase(--have.end());
		int pos = i.first + 1;
		while (!st.count(pos) && have.size()) {
			pair <int, int> cur = *(--have.end());
			ans += 1LL * cur.first * (pos - cur.second);
			have.erase(--have.end());
		}
	}
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32100000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>
#include <unordered_set>

using namespace std;

#define inf 1000000007
#define eps 1e-9
#define mp(a, b) make_pair(a, b)
#define llinf 1000000000000000007LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

struct pt {
	int frst, scnd;
};

bool operator <(const pt &a, const pt &b) {
	return a.frst < b.frst || (a.frst == b.frst && a.scnd < b.scnd);
}
int n;
int a[200005];
int b[200005];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(9);
	srand(time(0));
	cin >> n;
	//n = 50000;
	map <int, vector <int>> st;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		//a[i] = rand() % inf;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		//b[i] = rand() % inf;
	}

	for (int i = 0; i < n; i++) {
		st[a[i]].push_back(b[i]);
	}

	ll ans = 0LL;
	set <pt> have;
	for (auto &i : st) {
		for (auto &j : i.second) {
			have.insert({ j,i.first });
		}
		ans += 1LL * (--have.end())->frst * (i.first - (--have.end())->scnd);
		have.erase(--have.end());
		int pos = i.first + 1;
		while (!st.count(pos) && have.size()) {
			pt cur = *(--have.end());
			ans += 1LL * cur.frst * (pos - cur.scnd);
			have.erase(--have.end());
		}
	}
	cout << ans;
	return 0;
}