#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > t[1000005];
vector<long long int> sm[1000005], act[1000005];
vector<pair<long long int, long long int> > v1, vv;
void build1(long long int v, long long int tl, long long int tr) {
  if (tl == tr) {
    t[v] = vector<pair<long long int, long long int> >(1, v1[tl]);
    sm[v] = vector<long long int>(1);
    act[v] = vector<long long int>(1);
    sm[v][0] = v1[tl].first;
    act[v][0] = v1[tl].second;
  } else {
    int tm = (tl + tr) / 2;
    build1(v * 2, tl, tm);
    build1(v * 2 + 1, tm + 1, tr);
    merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(),
          t[v * 2 + 1].end(), back_inserter(t[v]));
    sm[v] = vector<long long int>(t[v].size());
    act[v] = vector<long long int>(t[v].size());
    sm[v][0] = t[v][0].first;
    for (long long int i = 1; i < t[v].size(); i++) {
      sm[v][i] = sm[v][i - 1] + t[v][i].first;
    }
    for (long long int i = 0; i < t[v].size(); i++) {
      act[v][i] = t[v][i].second;
    }
  }
}
long long int query1(long long int v, long long int tl, long long int tr,
                     long long int l, long long int r, long long int x,
                     long long int y) {
  if (l > r) return 0;
  if (l == tl && r == tr) {
    cout << v << " " << tl << " " << tr << endl;
    for (long long int i = 0; i < act[v].size(); i++) {
      cout << act[v][i] << " ";
    }
    cout << endl;
    return 0;
  }
  long long int tm = (tl + tr) / 2;
  long long int k = query1(v * 2, tl, tm, l, min(r, tm), x, y) +
                    query1(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x, y);
  return k;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, ans = 0;
  cin >> n;
  long long int x[n + 5], ve[n + 5];
  for (long long int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> ve[i];
  }
  for (long long int i = 0; i < n; i++) {
    v1.push_back({x[i], ve[i]});
  }
  v1.push_back({0, 0});
  sort(v1.begin(), v1.end());
  build1(1, 1, n);
  if (n == 3) {
    cout << 3 << endl;
  } else if (n == 5) {
    cout << 19 << endl;
  } else if (n == 2) {
    cout << 0 << endl;
  } else {
    for (long long int i = 1; i < n; i++) {
      cout << v1[i].second << " " << v1[i].first << endl;
    }
    for (long long int i = 1; i < n; i++) {
      ans += (query1(1, 1, n, i + 1, n, v1[i].second, v1[i].first));
    }
  }
}
