#include <bits/stdc++.h>
long long R = 7 + 1e9, NUMTESTCASE;
const int NN = 10 + 9e5;
const double pi = acos(-1.0);
int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1},
    DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
long long n, Draft[NN], Coor[NN], Speed[NN], Ans, Flag, Lazy[NN], Max[NN],
    Min[NN];
pair<long long, long long> Status[NN], Tree[NN];
map<long long, long long> Draft2;
vector<pair<pair<long long, long long>, long long>> Calc;
map<long long, set<long long, greater<long long>>> CommSpeed;
void Build(int Node = 1, int st = 1, int en = n) {
  if (st == en) {
    Min[Node] = Max[Node] = Speed[st];
    Tree[Node] = Status[Node];
    return;
  }
  Build(2 * Node, st, (st + en) / 2);
  Build(2 * Node + 1, (st + en) / 2 + 1, en);
  Max[Node] = max(Max[2 * Node], Max[2 * Node + 1]);
  Min[Node] = min(Min[2 * Node], Min[2 * Node + 1]);
}
void Solve(int End, int Spinit, int Node = 1, int st = 1, int en = n) {
  if (st > End) return;
  if (Min[Node] - Spinit >= 0) return;
  if (Max[Node] - Spinit < 0) return void(Calc.push_back({{st, en}, End + 1}));
  Solve(End, Spinit, 2 * Node, st, (st + en) / 2);
  Solve(End, Spinit, 2 * Node + 1, (st + en) / 2 + 1, en);
}
int main() {
  cin >> n;
  for (int i = (1); i <= (n); ++i) scanf("%I64d", &Draft[i]);
  for (int i = (1); i <= (n); ++i) {
    scanf("%I64d", &Flag);
    Draft2[Draft[i]] = Flag;
    CommSpeed[Flag].insert(Draft[i]);
  }
  n &= 0;
  for (auto Itter : Draft2) {
    n++;
    Coor[n] = Itter.first;
    Speed[n] = Itter.second;
    Status[n] = {Itter.first, Itter.second};
  }
  for (auto Itter : CommSpeed) {
    Flag = 0;
    while (Itter.second.size()) {
      Ans +=
          *Itter.second.begin() * (long long)(Itter.second.size() - 1 - Flag);
      Flag++;
      Itter.second.erase(Itter.second.begin());
    }
  }
  Build();
  for (int i = (2); i <= (n); ++i) Solve(i - 1, Speed[i]);
  memset(Draft, 0, sizeof(Draft));
  for (auto u : Calc) {
    Ans += u.second * (u.first.second - u.first.first + 1);
    Draft[u.first.first]++;
    Draft[u.first.second + 1]--;
  }
  for (int i = (1); i <= (n); ++i) Draft[i] += Draft[i - 1];
  for (int i = (1); i <= (n); ++i) Ans -= Draft[i] * Coor[i];
  cout << Ans;
  return 0;
}
